Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: docker #26

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 ./...
Expand Down
5 changes: 2 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 9 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down