diff --git a/.air.toml b/.air.toml index 0747c04..d8c5605 100644 --- a/.air.toml +++ b/.air.toml @@ -12,7 +12,7 @@ exclude_file = [] exclude_regex = ["_test.go", "build/.*"] exclude_unchanged = false follow_symlink = false -full_bin = "export $(cat .env | xargs) ; make init-db ; cd build ; ./hugo-cms" +full_bin = "export $(cat .env | xargs) ; make prep-dev init-db ; cd build ; ./hugo-cms" include_dir = [] include_ext = ["go", "tpl", "tmpl", "html", "gohtml", "css", "js"] include_file = [] diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a7e67cd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM golang:1.23.3 AS builder + +WORKDIR /usr/src/app + +COPY go.mod go.sum ./ +RUN go mod download && go mod verify + +COPY . . +RUN make compile + + +FROM scratch + +COPY --from=builder /usr/src/app/build/ /. +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ + +WORKDIR / + +ENV PORT=3000 + +CMD ["/hugo-cms"] \ No newline at end of file diff --git a/Makefile b/Makefile index 99bbcd1..c7f2386 100644 --- a/Makefile +++ b/Makefile @@ -5,12 +5,15 @@ clean: .Phony: prep prep: - -docker-compose up -d mkdir -p build .Phony: compile compile: prep - go build -o ./build/hugo-cms + CGO_ENABLED=0 go build -o ./build/hugo-cms + +.Phony: prep-dev +prep-dev: + docker-compose up -d .Phony: init-db init-db: diff --git a/main.go b/main.go index f6194b2..0badf3f 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,12 @@ func main() { } defer dbp.Close() + log.Println("Connected to database") + if err := dbp.Migrate(context.Background()); err != nil { + log.Fatal(err) + } + log.Println("Database migrated") + userService := users.NewService(dbp) if *flagUser != "" { @@ -83,13 +89,6 @@ func main() { postHandlers := handlers.Post{Post: postService} authHandlers := handlers.AuthHandler{UserService: userService} - log.Println("Connected to database") - if err := dbp.Migrate(context.Background()); err != nil { - log.Fatal(err) - } - - log.Println("Database migrated") - tmplEngine := html.NewFileSystem(http.FS(templates.FS), ".html") tmplEngine.Funcmap["markdown"] = func(s string) (template.HTML, error) { var buf bytes.Buffer