Added bootstrap cli and compose.yaml #15
@@ -1,3 +0,0 @@
|
||||
DTTMR_OTLP_ENDPOINT=172.17.0.1:4317
|
||||
DTTMR_DATABASE_URL=postgres://postgres:postgres@172.17.0.1:5432/postgres?sslmode=disable&timezone=utc
|
||||
DTTMR_JWT_SECRET=V*$#Jt9OlYW0gEB6PyUU$qLYbJ^NC7LZ
|
||||
@@ -0,0 +1,17 @@
|
||||
# PostgreSQL Config
|
||||
POSTGRES_USER=dttmr_user
|
||||
POSTGRES_PASSWORD=super_secret_db_password
|
||||
POSTGRES_DB=dttmr_db
|
||||
|
||||
# App Config
|
||||
DTTMR_DATABASE_URL=postgres://dttmr_user:super_secret_db_password@postgres:5432/dttmr_db?sslmode=disable&timezone=utc
|
||||
DTTMR_OTLP_ENDPOINT=otel-collector:4317
|
||||
DTTMR_ENVIRONMENT=production
|
||||
DTTMR_JWT_SECRET=super_secret_jwt_key
|
||||
DTTMR_PORT=8080
|
||||
|
||||
# Bootstrap Admin
|
||||
ADMIN_EMAIL=admin@example.com
|
||||
ADMIN_USERNAME=admin
|
||||
ADMIN_PASSWORD=changeme
|
||||
|
||||
+12
-6
@@ -1,22 +1,28 @@
|
||||
FROM golang:1.26 AS build
|
||||
FROM golang:1.26-alpine AS build
|
||||
|
||||
RUN apk add --no-cache make git
|
||||
|
||||
WORKDIR /app
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY "./internal" "./internal"
|
||||
COPY "./cmd" "./cmd"
|
||||
COPY "Makefile" "./Makefile"
|
||||
# So make can fetch tag and commit
|
||||
COPY "./.git" "./.git"
|
||||
|
||||
RUN make build
|
||||
|
||||
FROM debian:latest
|
||||
FROM alpine:latest
|
||||
|
||||
RUN apk --no-cache add ca-certificates
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY .env.docker .env
|
||||
COPY --from=build /app/bin/dttmr-api /app/dttmr-api
|
||||
COPY --from=build /app/bin/api /usr/local/bin/api
|
||||
COPY --from=build /app/bin/bootstrap /usr/local/bin/bootstrap
|
||||
|
||||
EXPOSE 8080
|
||||
CMD ["/app/dttmr-api"]
|
||||
CMD ["api"]
|
||||
|
||||
@@ -4,9 +4,9 @@ MAIN_DIR := ./cmd/api
|
||||
BIN_DIR := ./bin
|
||||
|
||||
# Dynamically pull version and commit hash from Git
|
||||
VERSION := $(shell git describe --tags --always --dirty)
|
||||
COMMIT := $(shell git rev-parse --short HEAD)
|
||||
BUILD_TIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
VERSION ?= $(shell git describe --tags --always --dirty="-dev" 2>/dev/null || echo "dev")
|
||||
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "none")
|
||||
BUILD_TIME ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
# Linker flags to strip symbols (-s -w) and inject build data into the binary
|
||||
LDFLAGS := -w -s \
|
||||
@@ -16,7 +16,7 @@ LDFLAGS := -w -s \
|
||||
|
||||
# Targets
|
||||
|
||||
.PHONY: all help build run test test-cover lint fmt mod clean docker
|
||||
.PHONY: all help build build-api build-bootstrap run test test-cover lint fmt mod clean docker
|
||||
|
||||
all: help
|
||||
|
||||
@@ -27,15 +27,23 @@ help:
|
||||
@echo "Targets:"
|
||||
@sed -n 's/^##//p' $(MAKEFILE_LIST) | column -t -s ':' | sed -e 's/^/ /'
|
||||
|
||||
## build: Compile the binary
|
||||
build:
|
||||
@echo "Building $(APP_NAME) version $(VERSION)..."
|
||||
@CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o $(BIN_DIR)/$(APP_NAME) $(MAIN_DIR)
|
||||
## build: Compile all binaries
|
||||
build: build-api build-bootstrap
|
||||
|
||||
## build-api: Compile the api binary
|
||||
build-api:
|
||||
@echo "Building api..."
|
||||
go build -ldflags="$(LDFLAGS)" -o $(BIN_DIR)/api $(MAIN_DIR)
|
||||
|
||||
## build-bootstrap: Compile the bootstrap binary
|
||||
build-bootstrap:
|
||||
@echo "Building bootstrap cli..."
|
||||
go build -ldflags="$(LDFLAGS)" -o $(BIN_DIR)/bootstrap ./cmd/bootstrap
|
||||
|
||||
## run: Run the application directly
|
||||
run: build
|
||||
run: build-api
|
||||
@echo "Starting $(APP_NAME)..."
|
||||
@$(BIN_DIR)/$(APP_NAME)
|
||||
@$(BIN_DIR)/api
|
||||
|
||||
## test: Run tests with race detector
|
||||
test:
|
||||
|
||||
+1
-5
@@ -21,7 +21,7 @@ import (
|
||||
|
||||
var (
|
||||
Version = "dev"
|
||||
Commit = "none" // In the Makefile, we called this 'Commit' instead of 'Hash'
|
||||
Commit = "none"
|
||||
BuildTime = "unknown"
|
||||
)
|
||||
|
||||
@@ -98,10 +98,6 @@ func run() error {
|
||||
}
|
||||
}()
|
||||
|
||||
if err := database.RunMigrations(db); err != nil {
|
||||
slog.Error("failed to run migrations", slog.Any("error", err))
|
||||
}
|
||||
|
||||
srv := makeServer(db, cfg)
|
||||
go func() {
|
||||
slog.Info("starting http server", "addr", srv.Addr)
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"flag"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/robindittmar/dttmr-api/internal/config"
|
||||
"github.com/robindittmar/dttmr-api/internal/database"
|
||||
"github.com/robindittmar/dttmr-api/internal/domain"
|
||||
"github.com/robindittmar/dttmr-api/internal/repository"
|
||||
)
|
||||
|
||||
var (
|
||||
Version = "dev"
|
||||
Commit = "none"
|
||||
BuildTime = "unknown"
|
||||
)
|
||||
|
||||
func main() {
|
||||
time.Local, _ = time.LoadLocation("UTC")
|
||||
|
||||
_ = godotenv.Load(".env")
|
||||
setupLogging()
|
||||
|
||||
email := flag.String("email", "admin@example.com", "Admin email address")
|
||||
name := flag.String("name", "admin", "Admin name")
|
||||
password := flag.String("password", "", "Admin password")
|
||||
cfg := config.Load()
|
||||
|
||||
slog.Info("starting bootstrap",
|
||||
slog.String("version", Version),
|
||||
slog.String("commit", Commit),
|
||||
slog.String("build_time", BuildTime),
|
||||
)
|
||||
defer slog.Info("bootstrap complete!")
|
||||
|
||||
db, err := database.New(context.Background(), cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
slog.Error("failed to initialize database", slog.Any("error", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
defer func() {
|
||||
err := db.Close()
|
||||
if err != nil {
|
||||
slog.Error("failed to close database connection", slog.Any("error", err))
|
||||
}
|
||||
}()
|
||||
|
||||
if err := database.RunMigrations(db); err != nil {
|
||||
slog.Error("failed to run migrations", slog.Any("error", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if *password != "" {
|
||||
err := seedAdminUser(db, *email, *name, *password)
|
||||
if err != nil {
|
||||
slog.Error("failed to seed admin user", slog.Any("error", err))
|
||||
} else {
|
||||
slog.Info("admin user seeded", slog.String("email", *email), slog.String("name", *name))
|
||||
}
|
||||
} else {
|
||||
slog.Info("skipping admin user seeding, no password provided")
|
||||
}
|
||||
}
|
||||
|
||||
func seedAdminUser(db *sql.DB, email string, name string, password string) error {
|
||||
userRepo := repository.NewUserRepo(db)
|
||||
userService := domain.NewUserService(userRepo)
|
||||
|
||||
_, err := userService.CreateUser(context.Background(), email, name, password)
|
||||
return err
|
||||
}
|
||||
|
||||
func setupLogging() {
|
||||
baseHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelInfo,
|
||||
})
|
||||
|
||||
logger := slog.New(baseHandler)
|
||||
slog.SetDefault(logger)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16-alpine
|
||||
container_name: dttmr-postgres
|
||||
restart: unless-stopped
|
||||
env_file:
|
||||
- .env
|
||||
volumes:
|
||||
- /srv/dttmr-api/postgresql:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
|
||||
otel-collector:
|
||||
image: otel/opentelemetry-collector:latest
|
||||
container_name: dttmr-otel
|
||||
restart: unless-stopped
|
||||
command: ["--config=/etc/otelcol/config.yaml"]
|
||||
volumes:
|
||||
- ./docker/otel-config.yaml:/etc/otelcol/config.yaml
|
||||
ports:
|
||||
- "4317:4317"
|
||||
|
||||
bootstrap:
|
||||
build: .
|
||||
container_name: dttmr-bootstrap
|
||||
env_file:
|
||||
- .env
|
||||
command: ["bootstrap", "--email=${ADMIN_EMAIL}", "--name=${ADMIN_USERNAME}", "--password=${ADMIN_PASSWORD}"]
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
|
||||
api:
|
||||
build: .
|
||||
container_name: dttmr-api
|
||||
restart: unless-stopped
|
||||
env_file:
|
||||
- .env
|
||||
ports:
|
||||
- "8080:8080"
|
||||
depends_on:
|
||||
otel-collector:
|
||||
condition: service_started
|
||||
bootstrap:
|
||||
condition: service_completed_successfully
|
||||
@@ -1,33 +0,0 @@
|
||||
services:
|
||||
otel-collector:
|
||||
image: otel/opentelemetry-collector:latest
|
||||
command: ["--config=/etc/otel-collector-config.yaml"]
|
||||
volumes:
|
||||
- ./otel-collector-config.yaml:/etc/otel-collector-config.yaml
|
||||
ports:
|
||||
- "4317:4317"
|
||||
|
||||
jaeger:
|
||||
image: jaegertracing/all-in-one:latest
|
||||
environment:
|
||||
- COLLECTOR_OTLP_ENABLED=true
|
||||
ports:
|
||||
- "16686:16686"
|
||||
|
||||
prometheus:
|
||||
image: prom/prometheus:latest
|
||||
volumes:
|
||||
- ./prometheus.yml:/etc/prometheus/prometheus.yml
|
||||
ports:
|
||||
- "9090:9090"
|
||||
|
||||
postgres:
|
||||
image: postgres:latest
|
||||
volumes:
|
||||
- ./postgresql-data:/var/lib/postgresql/data
|
||||
ports:
|
||||
- "15432:5432"
|
||||
environment:
|
||||
- POSTGRES_USER=postgres
|
||||
- POSTGRES_PASSWORD=postgres
|
||||
- POSTGRES_DB=dttmr
|
||||
@@ -1,23 +0,0 @@
|
||||
receivers:
|
||||
otlp:
|
||||
protocols:
|
||||
grpc:
|
||||
endpoint: 0.0.0.0:4317
|
||||
|
||||
exporters:
|
||||
otlp/jaeger:
|
||||
endpoint: jaeger:4317
|
||||
tls:
|
||||
insecure: true
|
||||
|
||||
prometheus:
|
||||
endpoint: 0.0.0.0:8889
|
||||
|
||||
service:
|
||||
pipelines:
|
||||
traces:
|
||||
receivers: [otlp]
|
||||
exporters: [otlp/jaeger]
|
||||
metrics:
|
||||
receivers: [otlp]
|
||||
exporters: [prometheus]
|
||||
@@ -0,0 +1,15 @@
|
||||
receivers:
|
||||
otlp:
|
||||
protocols:
|
||||
grpc:
|
||||
endpoint: 0.0.0.0:4317
|
||||
|
||||
exporters:
|
||||
debug:
|
||||
verbosity: basic
|
||||
|
||||
service:
|
||||
pipelines:
|
||||
traces:
|
||||
receivers: [otlp]
|
||||
exporters: [debug]
|
||||
@@ -1,7 +0,0 @@
|
||||
global:
|
||||
scrape_interval: 5s
|
||||
|
||||
scrape_configs:
|
||||
- job_name: 'otel-collector'
|
||||
static_configs:
|
||||
- targets: ['otel-collector:8889']
|
||||
Reference in New Issue
Block a user