diff options
| -rw-r--r-- | Dockerfile | 18 | ||||
| -rw-r--r-- | cmd/main.go | 21 | ||||
| -rw-r--r-- | docker-compose.yml | 14 |
3 files changed, 51 insertions, 2 deletions
diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a5d236c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# build +FROM golang:1.25.4-alpine AS builder +WORKDIR /app +COPY go.mod go.sum ./ +RUN go mod download +COPY . . +RUN CGO_ENABLED=0 GOOS=linux go build -o main ./cmd/main.go + +# run +FROM alpine:latest +WORKDIR /app +COPY --from=builder /app/main . +COPY --from=builder /app/views ./views +COPY --from=builder /app/css ./css +COPY --from=builder /app/js ./js + +EXPOSE 8080 +CMD ["./main"] diff --git a/cmd/main.go b/cmd/main.go index cd8d4fe..24ba0c9 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,14 +1,31 @@ package main import ( + "fmt" "log" + "os" "git.mithe.dk/web-portfolio/internal" ) func main() { - server := internal.NewSever() - if err := server.Start("localhost:42069"); err != nil { + port := os.Getenv("PORT") + if port == "" { + port = "42069" + } + + // In Docker, listen on 0.0.0.0; locally use localhost + host := os.Getenv("HOST") + if host == "" { + host = "0.0.0.0" + } + + addr := fmt.Sprintf("%s:%s", host, port) + + server := internal.NewServer() + log.Printf("Starting server on %s", addr) + + if err := server.Start(addr); err != nil { log.Fatal(err) } } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0709354 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +services: + web: + build: + context: . + dockerfile: Dockerfile + ports: + - "8080:8080" + environment: + - PORT=8080 + volumes: + - ./views:/app/views + - ./css:/app/css + - ./js:/app/js + restart: unless-stopped |