Skip to content

Commit 890ddae

Browse files
feat: Added docker compose with automated migrations and hot reloads
1 parent 707b6c3 commit 890ddae

File tree

7 files changed

+164
-1
lines changed

7 files changed

+164
-1
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,27 @@ A Go service that:
3737

3838
## Getting Started
3939

40+
### Option 1: Manual Setup
41+
4042
Check the individual README files in each project directory for specific setup instructions:
4143
- [Frontend Setup](/app/README.md)
4244
- [Backend Setup](/api/README.md)
4345

46+
### Option 2: Docker Compose
47+
48+
Run all services with Docker Compose:
49+
50+
```bash
51+
docker-compose up -d
52+
```
53+
54+
This will start:
55+
- PostgreSQL database on port 5432
56+
- Next.js app on port 3000 (with automatic database migration)
57+
- Go API on port 8080
58+
59+
Both app and API services support hot reload for development.
60+
4461
## Video Tutorial
4562

4663
This project accompanies a tutorial video by Dreams of Code explaining the implementation details and best practices for integrating Better-Auth with Go.

api/.air.toml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
root = "."
2+
testdata_dir = "testdata"
3+
tmp_dir = "tmp"
4+
5+
[build]
6+
args_bin = []
7+
bin = "./tmp/main"
8+
cmd = "go build -o ./tmp/main ."
9+
delay = 1000
10+
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
11+
exclude_file = []
12+
exclude_regex = ["_test.go"]
13+
exclude_unchanged = false
14+
follow_symlink = false
15+
full_bin = ""
16+
include_dir = []
17+
include_ext = ["go", "tpl", "tmpl", "html"]
18+
include_file = []
19+
kill_delay = "0s"
20+
log = "build-errors.log"
21+
poll = false
22+
poll_interval = 0
23+
rerun = false
24+
rerun_delay = 500
25+
send_interrupt = false
26+
stop_on_root = false
27+
28+
[color]
29+
app = ""
30+
build = "yellow"
31+
main = "magenta"
32+
runner = "green"
33+
watcher = "cyan"
34+
35+
[log]
36+
main_only = false
37+
time = false
38+
39+
[misc]
40+
clean_on_exit = false
41+
42+
[screen]
43+
clear_on_rebuild = false
44+
keep_scroll = true

api/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM golang:1.24.2-alpine
2+
3+
WORKDIR /app
4+
5+
# Install air for hot reload
6+
RUN go install github.com/air-verse/air@latest
7+
8+
# Copy go mod and sum files
9+
COPY go.mod go.sum ./
10+
11+
# Download dependencies
12+
RUN go mod download
13+
14+
# Copy source code
15+
COPY . .
16+
17+
EXPOSE 8080
18+
19+
# Use air for hot reload
20+
CMD ["air", "-c", ".air.toml"]

api/auth/auth.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"net/http"
7+
"os"
78

89
"github.com/lestrrat-go/jwx/v3/jwk"
910
"github.com/lestrrat-go/jwx/v3/jwt"
@@ -20,7 +21,12 @@ var (
2021
)
2122

2223
func UserFromRequest(r *http.Request) (User, error) {
23-
keyset, err := jwk.Fetch(r.Context(), "http://localhost:3000/api/auth/jwks")
24+
authAPIURL := os.Getenv("AUTH_API_URL")
25+
if authAPIURL == "" {
26+
authAPIURL = "http://localhost:3000/api/auth"
27+
}
28+
29+
keyset, err := jwk.Fetch(r.Context(), authAPIURL+"/jwks")
2430
if err != nil {
2531
return User{}, fmt.Errorf("fetch jwks: %w", err)
2632
}

app/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM oven/bun:1-alpine
2+
3+
WORKDIR /app
4+
5+
# Install build dependencies for native modules
6+
RUN apk add --no-cache python3 make g++
7+
8+
# Copy package files
9+
COPY package.json bun.lockb* ./
10+
11+
# Install dependencies
12+
RUN bun install
13+
14+
# Copy source code
15+
COPY . .
16+
17+
# Make entrypoint executable
18+
RUN chmod +x /app/entrypoint.sh
19+
20+
EXPOSE 3000
21+
22+
CMD ["/app/entrypoint.sh"]

app/entrypoint.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
set -e
3+
4+
echo "Running database migration..."
5+
bun run db:migrate
6+
7+
echo "Starting development server..."
8+
bun run dev
9+

docker-compose.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
services:
2+
postgres:
3+
image: postgres:15
4+
environment:
5+
POSTGRES_USER: authly_user
6+
POSTGRES_PASSWORD: authly_password
7+
POSTGRES_DB: authly_db
8+
ports:
9+
- "5432:5432"
10+
volumes:
11+
- postgres_data:/var/lib/postgresql/data
12+
healthcheck:
13+
test: ["CMD-SHELL", "pg_isready -U authly_user -d authly_db"]
14+
interval: 5s
15+
timeout: 5s
16+
retries: 5
17+
18+
app:
19+
build: ./app
20+
ports:
21+
- "3000:3000"
22+
environment:
23+
DATABASE_URL: "postgresql://authly_user:authly_password@postgres:5432/authly_db"
24+
GO_API_URL: "http://api:8080"
25+
volumes:
26+
- ./app:/app
27+
- /app/node_modules
28+
depends_on:
29+
postgres:
30+
condition: service_healthy
31+
restart: unless-stopped
32+
33+
api:
34+
build: ./api
35+
ports:
36+
- "8080:8080"
37+
environment:
38+
AUTH_API_URL: "http://app:3000/api/auth"
39+
volumes:
40+
- ./api:/app
41+
restart: unless-stopped
42+
43+
volumes:
44+
postgres_data:
45+

0 commit comments

Comments
 (0)