From f4a96db9423d326f1a217ad9edd8d889c10bd535 Mon Sep 17 00:00:00 2001 From: nihonium Date: Thu, 9 Oct 2025 14:45:41 +0300 Subject: [PATCH] refact: renamed server to backend --- Dockerfiles/Dockerfile_backend | 13 ++++++++++++ Dockerfiles/Dockerfile_server | 20 ------------------ modules/{server => backend}/config.toml | 0 modules/{server => backend}/go.mod | 7 ++++--- modules/{server => backend}/go.sum | 8 +++++++ modules/{server => backend}/helpers.go | 0 modules/{server => backend}/main.go | 21 +++++++++++++++++++ .../{server => backend}/templates/index.html | 0 modules/backend/types.go | 12 +++++++++++ modules/server/types.go | 6 ------ 10 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 Dockerfiles/Dockerfile_backend delete mode 100644 Dockerfiles/Dockerfile_server rename modules/{server => backend}/config.toml (100%) rename modules/{server => backend}/go.mod (86%) rename modules/{server => backend}/go.sum (90%) rename modules/{server => backend}/helpers.go (100%) rename modules/{server => backend}/main.go (65%) rename modules/{server => backend}/templates/index.html (100%) create mode 100644 modules/backend/types.go delete mode 100644 modules/server/types.go diff --git a/Dockerfiles/Dockerfile_backend b/Dockerfiles/Dockerfile_backend new file mode 100644 index 0000000..1e170fc --- /dev/null +++ b/Dockerfiles/Dockerfile_backend @@ -0,0 +1,13 @@ +FROM ubuntu:22.04 + +WORKDIR /app +COPY --chmod=755 modules/server/nyanimedb /app +COPY modules/server/templates /app/templates +EXPOSE 8080 +ENTRYPOINT ["/app/nyanimedb"] + + +# FROM debian:buster-slim +# COPY --from=builder /go/src/app/main /go/bin/main +# ENV PATH="/go/bin:${PATH}" +# CMD ["main"] \ No newline at end of file diff --git a/Dockerfiles/Dockerfile_server b/Dockerfiles/Dockerfile_server deleted file mode 100644 index 87014ce..0000000 --- a/Dockerfiles/Dockerfile_server +++ /dev/null @@ -1,20 +0,0 @@ -FROM ubuntu:22.04 - -WORKDIR /app -COPY --chmod=755 nyanimedb /app -COPY templates /app/templates -EXPOSE 8080 -ENTRYPOINT ["/app/nyanimedb"] - -# FROM golang:1.25 AS builder - -# ARG VERSION=dev - -# WORKDIR /go/src/app -# COPY main.go . -# RUN go build -o main -ldflags=-X=main.version=${VERSION} main.go - -# FROM debian:buster-slim -# COPY --from=builder /go/src/app/main /go/bin/main -# ENV PATH="/go/bin:${PATH}" -# CMD ["main"] \ No newline at end of file diff --git a/modules/server/config.toml b/modules/backend/config.toml similarity index 100% rename from modules/server/config.toml rename to modules/backend/config.toml diff --git a/modules/server/go.mod b/modules/backend/go.mod similarity index 86% rename from modules/server/go.mod rename to modules/backend/go.mod index cbdfb8b..9c9e8fc 100644 --- a/modules/server/go.mod +++ b/modules/backend/go.mod @@ -6,19 +6,20 @@ require ( github.com/bytedance/sonic v1.14.0 // indirect github.com/bytedance/sonic/loader v0.3.0 // indirect github.com/cloudwego/base64x v0.1.6 // indirect - github.com/gabriel-vasile/mimetype v1.4.8 // indirect + github.com/gabriel-vasile/mimetype v1.4.9 // indirect + github.com/gin-contrib/cors v1.7.6 // indirect github.com/gin-contrib/sse v1.1.0 // indirect github.com/gin-gonic/gin v1.11.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.27.0 // indirect - github.com/goccy/go-json v0.10.2 // indirect + github.com/goccy/go-json v0.10.5 // indirect github.com/goccy/go-yaml v1.18.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.3.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/quic-go/qpack v0.5.1 // indirect diff --git a/modules/server/go.sum b/modules/backend/go.sum similarity index 90% rename from modules/server/go.sum rename to modules/backend/go.sum index cb0d745..5cc008d 100644 --- a/modules/server/go.sum +++ b/modules/backend/go.sum @@ -8,6 +8,10 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8= +github.com/gabriel-vasile/mimetype v1.4.9 h1:5k+WDwEsD9eTLL8Tz3L0VnmVh9QxGjRmjBvAG7U/oYY= +github.com/gabriel-vasile/mimetype v1.4.9/go.mod h1:WnSQhFKJuBlRyLiKohA/2DtIlPFAbguNaG7QCHcyGok= +github.com/gin-contrib/cors v1.7.6 h1:3gQ8GMzs1Ylpf70y8bMw4fVpycXIeX1ZemuSQIsnQQY= +github.com/gin-contrib/cors v1.7.6/go.mod h1:Ulcl+xN4jel9t1Ry8vqph23a60FwH9xVLd+3ykmTjOk= github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk= @@ -20,6 +24,8 @@ github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHO github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw= github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= @@ -33,6 +39,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= diff --git a/modules/server/helpers.go b/modules/backend/helpers.go similarity index 100% rename from modules/server/helpers.go rename to modules/backend/helpers.go diff --git a/modules/server/main.go b/modules/backend/main.go similarity index 65% rename from modules/server/main.go rename to modules/backend/main.go index 775540b..651d9cc 100644 --- a/modules/server/main.go +++ b/modules/backend/main.go @@ -5,7 +5,9 @@ import ( "net/http" "os" "reflect" + "time" + "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/pelletier/go-toml/v2" log "github.com/sirupsen/logrus" @@ -29,6 +31,15 @@ func main() { r.LoadHTMLGlob("templates/*") + r.Use(cors.New(cors.Config{ + AllowOrigins: []string{"*"}, // allow all origins, change to specific domains in production + AllowMethods: []string{"GET", "POST", "PUT", "DELETE"}, + AllowHeaders: []string{"Origin", "Content-Type", "Accept"}, + ExposeHeaders: []string{"Content-Length"}, + AllowCredentials: true, + MaxAge: 12 * time.Hour, + })) + r.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index.html", gin.H{ "title": "Welcome Page", @@ -36,6 +47,16 @@ func main() { }) }) + r.GET("/api", func(c *gin.Context) { + items := []Item{ + {ID: 1, Title: "First Item", Description: "This is the description of the first item."}, + {ID: 2, Title: "Second Item", Description: "This is the description of the second item."}, + {ID: 3, Title: "Third Item", Description: "This is the description of the third item."}, + } + + c.JSON(http.StatusOK, items) + }) + r.Run(":8080") } diff --git a/modules/server/templates/index.html b/modules/backend/templates/index.html similarity index 100% rename from modules/server/templates/index.html rename to modules/backend/templates/index.html diff --git a/modules/backend/types.go b/modules/backend/types.go new file mode 100644 index 0000000..20d3158 --- /dev/null +++ b/modules/backend/types.go @@ -0,0 +1,12 @@ +package main + +type Config struct { + Mode string + LogLevel string `toml:"LogLevel" env:"LOG_LEVEL"` +} + +type Item struct { + ID int `json:"id"` + Title string `json:"title"` + Description string `json:"description"` +} diff --git a/modules/server/types.go b/modules/server/types.go deleted file mode 100644 index 539babf..0000000 --- a/modules/server/types.go +++ /dev/null @@ -1,6 +0,0 @@ -package main - -type Config struct { - Mode string - LogLevel string `toml:"LogLevel" env:"LOG_LEVEL"` -}