From 4fbde01a27624e046fa86ac4681652a1be1febd8 Mon Sep 17 00:00:00 2001 From: Tom Nagle Date: Mon, 10 Jun 2024 20:48:08 +1000 Subject: [PATCH] feat: docker --- Dockerfile | 26 ++++++++++++++++++++++++++ Makefile | 30 ++++++++++++++++++++++++++++++ cmd/main.go | 5 ++--- docker-compose.yml | 15 +++++++++++++++ internal/config/config.go | 13 +++++++++---- 5 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..edbcf55 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM golang:1.22-alpine AS base + +WORKDIR /app + +# Install dependencies required for the build process +RUN apk add --no-cache make curl + +RUN curl -L https://github.com/tailwindlabs/tailwindcss/releases/download/v3.4.4/tailwindcss-linux-arm64 -o tailwindcss \ + && chmod +x tailwindcss + +RUN go install github.com/a-h/templ/cmd/templ@latest + +FROM base AS development + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . ./ + +RUN make build + +FROM base AS production + +COPY --from=development --chown=golang:golang /app /app + +USER golang diff --git a/Makefile b/Makefile index d1ee529..087c7b2 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +IMAGE_ID ?= app +NETWORK ?= app + .PHONY: tailwind-watch tailwind-watch: ./tailwindcss -i ./static/css/input.css -o ./static/css/style.css --watch @@ -18,12 +21,39 @@ templ-watch: dev: go build -o ./tmp/$(APP_NAME) ./cmd/$(APP_NAME)/main.go && air +.PHONY: start +start: + go run cmd/*.go + +.PHONY: create-network +create-network: + docker network ls | grep -q -w $(NETWORK) || docker network create -d bridge $(NETWORK) + +.PHONY: build-image +build-image: + make create-network + docker images | grep $(NETWORK) -q && [ "${FORCE}" != "true" ] \ + && echo "\nImage exists! Skipping... (use FORCE=true to force the image to rebuild)" \ + || docker build --target development -f ./Dockerfile . -t $(NETWORK) + .PHONY: build build: make tailwind-build make templ-generate go build -ldflags "-X main.Environment=production" -o ./bin/$(APP_NAME) ./cmd/$(APP_NAME)/main.go +.PHONY: start +start: + go run cmd/*.go + +.PHONY: down +down: + docker compose -p $(NETWORK) down + +.PHONY: up +up: + docker compose -p $(NETWORK) up + .PHONY: vet vet: go vet ./... diff --git a/cmd/main.go b/cmd/main.go index 0db45a4..8f1ef9d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -32,12 +32,11 @@ func init() { } func main() { - logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) r := chi.NewRouter() - cfg := config.MustLoadConfig() - + logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: cfg.LogLevel})) db := database.MustOpen(cfg.DatabaseName) + passwordhash := passwordhash.NewHPasswordHash() userStore := dbstore.NewUserStore( diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..88dbabe --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +version: "3.8" +networks: + default: + name: ${NETWORK:-app} + external: true + +services: + app: + image: ${IMAGE_ID:-app} + command: make start + volumes: + - ../:/app + working_dir: /app + environment: + - LOG_LEVEL=debug \ No newline at end of file diff --git a/internal/config/config.go b/internal/config/config.go index a7bfd7c..cba6601 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,11 +1,16 @@ package config -import "github.com/kelseyhightower/envconfig" +import ( + "log/slog" + + "github.com/kelseyhightower/envconfig" +) type Config struct { - Port string `envconfig:"PORT" default:":4000"` - DatabaseName string `envconfig:"DATABASE_NAME" default:"goth.db"` - SessionCookieName string `envconfig:"SESSION_COOKIE_NAME" default:"session"` + Port string `envconfig:"PORT" default:":4000"` + DatabaseName string `envconfig:"DATABASE_NAME" default:"goth.db"` + SessionCookieName string `envconfig:"SESSION_COOKIE_NAME" default:"session"` + LogLevel slog.Level `envconfig:"LOG_LEVEL" default:"info"` } func loadConfig() (*Config, error) {