Skip to content

Commit b1d9abd

Browse files
committed
setup dockerfile
1 parent 37565e5 commit b1d9abd

File tree

8 files changed

+46
-29
lines changed

8 files changed

+46
-29
lines changed

.air.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ post_cmd = []
1616
# Binary file yields from `cmd`.
1717
bin = "tmp/server.exe"
1818
# Customize binary, can setup environment variables when run your app.
19-
full_bin = "dlv exec --accept-multiclient --log --headless --continue --listen :2345 --api-version 2 ./tmp/server.exe"
19+
full_bin = "dlv exec --accept-multiclient --log --headless --continue --listen :2345 --api-version 2 ./tmp/server.exe -- --config ./config/config.json "
2020
# Watch these filename extensions.
2121
include_ext = ["go", "tpl", "tmpl", "html"]
2222
# Ignore these filename extensions or directories.

.vscode/launch.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
"mode": "auto",
1212
"cwd": "${workspaceFolder}",
1313
"envFile": "${workspaceFolder}/.env",
14-
"program": "${workspaceFolder}/cmd/server"
14+
"program": "${workspaceFolder}/cmd/server",
15+
"args": [
16+
"--config=./config/config.json"
17+
]
1518
},
1619
{
1720
"name": "tailwind-watch",

Dockerfile

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
################
22
# BUILD BINARY #
33
################
4-
# golang:1.18.2-alpine3.16
54
FROM golang:1.21-bullseye as builder
65

7-
ENV SUPABASE=https://mogqxgbfrxdeluejpgqw.supabase.co|eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im1vZ3F4Z2JmcnhkZWx1ZWpwZ3F3Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDEzMDUxODEsImV4cCI6MjAxNjg4MTE4MX0.7c0xsQYSfc8984DJ84Ew-SOdzZvpzeL9qOTeAUYuGYg
8-
ENV CRYPTO_KEY=CrS2t2TrMph4IYi43alYzatKo3V69tPr
9-
ENV CRYPTO_IV_PAD=i4CrMp59rIYWa42V
10-
11-
# Install git + SSL ca certificates.
12-
# Git is required for fetching the dependencies.
13-
# Ca-certificates is required to call HTTPS endpoints.
14-
#RUN apk update && apk add --no-cache git ca-certificates tzdata && update-ca-certificates
15-
166
WORKDIR /src
177
COPY . .
188

@@ -39,8 +29,13 @@ COPY --from=builder /etc/passwd /etc/passwd
3929

4030
# Copy the executable.
4131
COPY --from=builder /server/server /server/server
42-
COPY --from=builder /src/config/config.json /server/config/config.json
32+
COPY --from=builder /src/config/config.json.docker /server/config/config.json
4333
COPY --from=builder /src/logs /server/logs
4434
COPY --from=builder /src/web /server/web
45-
CMD [ "/server/server" ]
35+
36+
ENV SUPABASE=
37+
ENV CRYPTO_KEY=
38+
ENV CRYPTO_IV_PAD=
39+
40+
CMD [ "/server/server", "--config", "/server/config/config.json" ]
4641

cmd/server/main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"errors"
5+
"flag"
56
"io"
67
"log/slog"
78
"os"
@@ -15,7 +16,7 @@ import (
1516

1617
func getLogFileName(appConfig *config.AppConfig) (result string) {
1718
currentDate := time.Now()
18-
return strings.Join([]string{appConfig.Log.Path, "/", currentDate.Format(appconstant.TimestampFormat), "_", appConfig.Log.FileName}, "")
19+
return strings.Join([]string{appConfig.Log.FolderPath, "/", currentDate.Format(appconstant.TimestampFormat), "_", appConfig.Log.FileName}, "")
1920
}
2021

2122
func useSlog(appConfig *config.AppConfig) (logFile *os.File, err error) {
@@ -33,7 +34,10 @@ func useSlog(appConfig *config.AppConfig) (logFile *os.File, err error) {
3334
}
3435

3536
func main() {
36-
if config, err := config.NewAppConfig("./config/config.json"); err != nil {
37+
configPath := flag.String("config", "./config.json", "Config file path")
38+
flag.Parse()
39+
40+
if config, err := config.NewAppConfig(*configPath); err != nil {
3741
panic(err)
3842
} else {
3943
if fileLog, err := useSlog(config); err == nil {

config/app_config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
)
77

88
type LogSetting struct {
9-
Path string
10-
FileName string
9+
FolderPath string
10+
FileName string
1111
}
1212

1313
type WebServerSetting struct {

config/config.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"log": {
3-
"path": "./logs",
3+
"folderPath": "./logs",
44
"fileName": "go-note.log"
55
},
66
"web": {
77
"title": "Note-X",
8-
"host": "localhost:8888",
9-
"basePath": "/"
8+
"host": ":8888",
9+
"basePath": ""
1010
}
1111
}

config/config.json.docker

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"log": {
3+
"folderPath": "/server/logs",
4+
"fileName": "go-note.log"
5+
},
6+
"web": {
7+
"title": "Note-X",
8+
"host": ":8888",
9+
"basePath": "/server"
10+
}
11+
}

internal/app/web_server.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log/slog"
66
"net/http"
77
"os"
8+
"path"
89

910
"github.com/ghostrepo00/go-note/config"
1011
"github.com/ghostrepo00/go-note/internal/pkg/model"
@@ -22,26 +23,29 @@ func NewWebServer(config *config.AppConfig) *webServer {
2223
return &webServer{config}
2324
}
2425

25-
func createMyRender() multitemplate.Renderer {
26+
func createMyRender(appConfig *config.AppConfig) multitemplate.Renderer {
2627
r := multitemplate.NewRenderer()
27-
r.AddFromFiles("index", "web/template/shared/base.html", "web/template/home/index.html", "web/template/shared/error_list.html")
28-
r.AddFromFiles("index_partial", "web/template/home/index.html", "web/template/shared/error_list.html")
29-
r.AddFromFiles("error_list", "web/template/shared/error_list.html")
30-
r.AddFromFiles("error", "web/template/shared/base.html", "web/template/shared/error.html")
28+
r.AddFromFiles("index",
29+
path.Join(appConfig.Web.BasePath, "web/template/shared/base.html"),
30+
path.Join(appConfig.Web.BasePath, "web/template/home/index.html"),
31+
path.Join(appConfig.Web.BasePath, "web/template/shared/error_list.html"))
32+
r.AddFromFiles("index_partial", path.Join(appConfig.Web.BasePath, "web/template/home/index.html"), path.Join(appConfig.Web.BasePath, "web/template/shared/error_list.html"))
33+
r.AddFromFiles("error_list", path.Join(appConfig.Web.BasePath, "web/template/shared/error_list.html"))
34+
r.AddFromFiles("error", path.Join(appConfig.Web.BasePath, "web/template/shared/base.html"), path.Join(appConfig.Web.BasePath, "web/template/shared/error.html"))
3135
return r
3236
}
3337

3438
func ConfigureWebRouter(appConfig *config.AppConfig, dbClient *supabase.Client) *gin.Engine {
3539
router := gin.Default()
36-
router.HTMLRender = createMyRender()
40+
router.HTMLRender = createMyRender(appConfig)
3741
router.Use(cors.Default())
3842
router.Use(gin.CustomRecovery(func(c *gin.Context, err any) {
3943
slog.Error("Unhandled exception", "error", err)
4044
c.HTML(http.StatusInternalServerError, "error", gin.H{"Status": 500, "Message": "Internal Error"})
4145
}))
4246

43-
router.Static("/assets", "web/assets")
44-
router.StaticFile("/favicon.ico", "web/favicon.ico")
47+
router.Static("/assets", path.Join(appConfig.Web.BasePath, "web/assets"))
48+
router.StaticFile("/favicon.ico", path.Join(appConfig.Web.BasePath, "web/favicon.ico"))
4549

4650
var crypto CryptoService = NewCryptoService(os.Getenv("CRYPTO_KEY"), os.Getenv("CRYPTO_IV_PAD"))
4751
var service AppService = NewAppService(appConfig, dbClient, crypto)

0 commit comments

Comments
 (0)