diff --git a/.gitignore b/.gitignore index 1892a58..86d572f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,6 @@ node_modules/ dist/ *.pem *.key -*.cert \ No newline at end of file +*.cert +/bin/ +.idea \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 9f87374..0ff0669 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,13 +8,15 @@ RUN go mod download COPY cmd/ /src/cmd/ COPY internal/ /src/internal/ COPY web/ /src/web/ -COPY internal/migration/ /src/internal/migration/ +COPY internal/migration/ /internal/migration/ RUN go build -o /out/go-mad ./cmd/go-mad FROM gcr.io/distroless/static-debian12:nonroot COPY --from=builder /out/go-mad /go-mad +COPY --from=builder /src/web /src/web +COPY --from=builder /internal/migration /internal/migration EXPOSE 8080 diff --git a/Makefile b/Makefile index c0ca55c..22d135a 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all build test vet fmt compose-up compose-down logs clean help +.PHONY: all build test vet fmt compose-up compose-down logs clean help web-dev GO_VERSION ?= 1.27.1 BINARY_NAME ?= go-mad @@ -19,7 +19,7 @@ build: fmt vet ## Build the Go binary ## ---- Development ---- run: build ## Run the Go binary (requires postgres running) - @$(BINARY_NAME) + @$(BINARY_DIR)/$(BINARY_NAME) compose-up: ## Start docker compose (postgres + go-mad) docker compose up --build -d @@ -48,12 +48,10 @@ integration-test: ## Run integration tests (requires test database) ## ---- Web Frontend ---- web-build: ## Build React frontend - npm install - npm run build + cd web && npm install && npm run build -web-dev: ## Run React dev server - npm install - npm run dev +web-dev: ## Start the go-mad application with Docker Compose + @docker compose up --build -d ## ---- Docker ---- @@ -75,24 +73,19 @@ help: ## Show this help message @echo "" @echo "Build targets:" @echo " build Build the Go binary" - @echo " run Run the Go binary" + @echo " run Run the Go binary (requires postgres running)" @echo " compose-up Start docker compose" @echo " compose-down Stop docker compose" @echo " compose-logs View docker compose logs" @echo "" - @echo "Code quality:" - @echo " fmt Format Go code" - @echo " vet Vet Go code" - @echo " test Run Go unit tests" + @echo "Web frontend:" + @echo " web-build Build React frontend" + @echo " web-dev Start go-mad with docker compose" @echo "" @echo "Docker:" @echo " docker-build Build docker image" @echo " docker-push Push docker image" @echo "" - @echo "Web frontend:" - @echo " web-build Build React frontend" - @echo " web-dev Run React dev server" - @echo "" @echo "Other:" @echo " clean Remove build artifacts" @echo " help Show this help message" \ No newline at end of file diff --git a/bin/go-mad b/bin/go-mad index 778ac07..d7c513f 100755 Binary files a/bin/go-mad and b/bin/go-mad differ diff --git a/cmd/go-mad/main.go b/cmd/go-mad/main.go index 6d8a946..895a5c5 100644 --- a/cmd/go-mad/main.go +++ b/cmd/go-mad/main.go @@ -1,13 +1,16 @@ package main import ( + "context" "log" + "net/http" "os" "os/signal" "syscall" "github.com/dnoulet/go-mad/internal/config" "github.com/dnoulet/go-mad/internal/database" + "github.com/dnoulet/go-mad/internal/handler" ) func main() { @@ -22,21 +25,39 @@ func main() { } defer db.Close() - if err := database.Migrate(db, "internal/migration"); err != nil { + if err := database.Migrate(db, "/internal/migration"); err != nil { log.Fatalf("Running migrations: %v", err) } + h := handler.New(db) + if err != nil { + log.Fatalf(" initializing handler: %v", err) + } + + httpServer := &http.Server{ + Addr: cfg.Server.Host + ":" + cfg.Server.Port, + Handler: h, + } + // Set up signal handling for graceful shutdown stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt, syscall.SIGTERM) - // Start server + // Start HTTP server go func() { log.Printf("Starting server on %s:%s", cfg.Server.Host, cfg.Server.Port) - // TODO: start HTTP server + if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatalf("HTTP server failed: %v", err) + } }() - // Wait for interrupt <-stop log.Println("Shutting down gracefully...") + + shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*60) + defer cancel() + if err := httpServer.Shutdown(shutdownCtx); err != nil { + log.Printf("HTTP server shutdown failed: %v", err) + } + log.Println("Server stopped") } diff --git a/docker-compose.yml b/docker-compose.yml index be338d0..0c61859 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,5 @@ services: - go-mad-database: + postgres: image: postgres:15-alpine container_name: go-mad-postgres environment: @@ -20,15 +20,13 @@ services: dockerfile: Dockerfile container_name: go-mad depends_on: - go-mad-database: + postgres: condition: service_healthy - extra_hosts: - - "go-mad-database:host-gateway" environment: - GO_MAD_DATABASE_URL: postgres://mad:mad@go-mad-database:5432/mad?sslmode=disable + GO_MAD_DATABASE_URL: postgres://mad:mad@postgres:5432/mad?sslmode=disable GO_MAD_SERVER_PORT: "8080" GO_MAD_JWT_SECRET: change-me-to-a-32-plus-char-secret ports: - - "8081:8080" + - "8080:8080" volumes: - postgres_data: + postgres_data: \ No newline at end of file diff --git a/helm/go-mad/Chart.lock b/helm/go-mad/Chart.lock new file mode 100644 index 0000000..123c87e --- /dev/null +++ b/helm/go-mad/Chart.lock @@ -0,0 +1,6 @@ +dependencies: +- name: postgresql + repository: https://charts.bitnami.com/bitnami + version: 16.7.27 +digest: sha256:e8b999a3aaad6328a13d0c666207650897b69c61367c947d363b2acbe0ea1b42 +generated: "2026-10-04T17:23:41.006015619+02:00" diff --git a/helm/go-mad/Chart.yaml b/helm/go-mad/Chart.yaml index d63106e..1076a21 100644 --- a/helm/go-mad/Chart.yaml +++ b/helm/go-mad/Chart.yaml @@ -3,4 +3,9 @@ name: go-mad description: A web application for managing tenant database connections and migrations type: application version: 0.1.0 -appVersion: "1.0" \ No newline at end of file +appVersion: "1.0" +dependencies: + - name: postgresql + version: "16.x.x" + repository: "https://charts.bitnami.com/bitnami" + condition: postgresql.enabled \ No newline at end of file diff --git a/helm/go-mad/charts/postgresql-16.7.27.tgz b/helm/go-mad/charts/postgresql-16.7.27.tgz new file mode 100644 index 0000000..73dc91a Binary files /dev/null and b/helm/go-mad/charts/postgresql-16.7.27.tgz differ diff --git a/helm/go-mad/templates/_helpers.tpl b/helm/go-mad/templates/_helpers.tpl index f203077..6587a19 100644 --- a/helm/go-mad/templates/_helpers.tpl +++ b/helm/go-mad/templates/_helpers.tpl @@ -3,5 +3,9 @@ go-mad {{- end -}} {{- define "go-mad.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride -}} +{{- else -}} {{- .Release.Name -}} +{{- end -}} {{- end -}} \ No newline at end of file diff --git a/helm/go-mad/templates/deployment.yaml b/helm/go-mad/templates/deployment.yaml index 011bec4..c4316e4 100644 --- a/helm/go-mad/templates/deployment.yaml +++ b/helm/go-mad/templates/deployment.yaml @@ -1,5 +1,6 @@ {{- $fullName := include "go-mad.fullname" . }} {{- $values := index .Values "go-mad" }} +{{- $pgName := printf "%s-postgresql" .Release.Name }} apiVersion: apps/v1 kind: Deployment @@ -29,7 +30,7 @@ spec: - containerPort: {{ $values.serverPort | int }} env: - name: GO_MAD_DATABASE_URL - value: "{{ $values.databaseURL }}" + value: "postgres://mad:{{ .Values.postgresql.auth.password }}@{{ $pgName }}:5432/mad?sslmode=disable" - name: GO_MAD_SERVER_PORT value: "{{ $values.serverPort }}" - name: GO_MAD_JWT_SECRET diff --git a/helm/go-mad/templates/ingress.yaml b/helm/go-mad/templates/ingress.yaml new file mode 100644 index 0000000..4c33cc5 --- /dev/null +++ b/helm/go-mad/templates/ingress.yaml @@ -0,0 +1,38 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "go-mad.fullname" . }} +{{- $firstHost := first .Values.ingress.hosts -}} +{{- $firstPath := first $firstHost.paths -}} +{{- $ingressPath := $firstPath.path -}} +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + app.kubernetes.io/name: {{ include "go-mad.name" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + ingressClassName: {{ .Values.ingress.className }} + {{- if .Values.ingress.tls }} + tls: + {{- toYaml .Values.ingress.tls | nindent 4 }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + pathType: {{ .pathType }} + backend: + service: + name: {{ $fullName }} + port: + number: {{ $.Values.service.port }} + {{- end }} + {{- end }} +{{- end }} \ No newline at end of file diff --git a/helm/go-mad/values-dev.yaml b/helm/go-mad/values-dev.yaml new file mode 100644 index 0000000..8a5d081 --- /dev/null +++ b/helm/go-mad/values-dev.yaml @@ -0,0 +1,61 @@ +# Development values for go-mad +replicaCount: 1 + +image: + repository: batty1039.startdedicated.net:5000/go-mad + pullPolicy: Always + tag: "latest" + +service: + type: ClusterIP + port: 8080 + +resources: + limits: + cpu: 500m + memory: 256Mi + requests: + cpu: 100m + memory: 128Mi + +postgresql: + enabled: true + image: + registry: docker.io + repository: bitnami/postgresql + tag: latest + pullPolicy: IfNotPresent + auth: + username: mad + password: mad + database: mad + primary: + persistence: + enabled: true + size: 2Gi + +go-mad: + serverPort: 8080 + jwtSecret: "dev-jwt-secret-change-me-to-32-plus-chars" + +ingress: + enabled: true + className: nginx + annotations: + cert-manager.io/cluster-issuer: letsencrypt-staging + nginx.ingress.kubernetes.io/ssl-redirect: "true" + hosts: + - host: mad-dev.urpi.be + paths: + - path: / + pathType: Prefix + tls: + - secretName: go-mad-tls + hosts: + - mad-dev.urpi.be + +certManager: + enabled: true + issuerRef: + name: letsencrypt-staging + kind: ClusterIssuer \ No newline at end of file diff --git a/helm/go-mad/values-lcl.yaml b/helm/go-mad/values-lcl.yaml new file mode 100644 index 0000000..8b4ba48 --- /dev/null +++ b/helm/go-mad/values-lcl.yaml @@ -0,0 +1,60 @@ +# Local (LCL) values for go-mad. +# Deployed to namespace go-insane-lcl, served at https://mad-lcl.intern-urpi.be +# by the docker TLS edge (dev-infrastructure/config/traefik/tls.yaml). +replicaCount: 1 + +image: + repository: localhost:5000/go-mad + pullPolicy: IfNotPresent + tag: "0.0.1" + +service: + type: ClusterIP + port: 8080 + +resources: + limits: + cpu: 500m + memory: 256Mi + requests: + cpu: 100m + memory: 128Mi + +postgresql: + enabled: true + image: + registry: docker.io + repository: bitnami/postgresql + tag: latest + pullPolicy: IfNotPresent + auth: + username: mad + password: mad + database: mad + primary: + persistence: + enabled: true + size: 2Gi + +go-mad: + serverPort: 8080 + jwtSecret: "lcl-jwt-secret-change-me-to-32-plus-chars" + +ingress: + enabled: true + className: traefik + annotations: {} + hosts: + - host: mad-lcl.intern-urpi.be + paths: + - path: / + pathType: Prefix + tls: + - secretName: local-dev-cert + hosts: + - mad-lcl.intern-urpi.be + +# No cert-manager in the local cluster. The local CA certificate is provided +# through the local-dev-cert secret (dev-infrastructure/k8s). +certManager: + enabled: false diff --git a/helm/go-mad/values-prd.yaml b/helm/go-mad/values-prd.yaml new file mode 100644 index 0000000..efb6d8d --- /dev/null +++ b/helm/go-mad/values-prd.yaml @@ -0,0 +1,68 @@ +# Production values for go-mad +replicaCount: 2 + +image: + repository: batty1039.startdedicated.net:5000/go-mad + pullPolicy: Always + tag: "latest" + +service: + type: ClusterIP + port: 8080 + +resources: + limits: + cpu: 1000m + memory: 512Mi + requests: + cpu: 200m + memory: 256Mi + +postgresql: + enabled: true + image: + registry: docker.io + repository: bitnami/postgresql + tag: latest + pullPolicy: IfNotPresent + auth: + username: mad + password: mad + database: mad + primary: + persistence: + enabled: true + size: 20Gi + resources: + limits: + cpu: 1000m + memory: 1Gi + requests: + cpu: 200m + memory: 512Mi + +go-mad: + serverPort: 8080 + jwtSecret: "" # Set via --set or secret + +ingress: + enabled: true + className: nginx + annotations: + cert-manager.io/cluster-issuer: letsencrypt-prod + nginx.ingress.kubernetes.io/ssl-redirect: "true" + hosts: + - host: mad.urpi.be + paths: + - path: / + pathType: Prefix + tls: + - secretName: go-mad-tls + hosts: + - mad.urpi.be + +certManager: + enabled: true + issuerRef: + name: letsencrypt-prod + kind: ClusterIssuer \ No newline at end of file diff --git a/helm/go-mad/values-tst.yaml b/helm/go-mad/values-tst.yaml new file mode 100644 index 0000000..b7c492c --- /dev/null +++ b/helm/go-mad/values-tst.yaml @@ -0,0 +1,61 @@ +# TST values for go-mad +replicaCount: 1 + +image: + repository: batty1039.startdedicated.net:5000/go-mad + pullPolicy: Always + tag: "latest" + +service: + type: ClusterIP + port: 8080 + +resources: + limits: + cpu: 750m + memory: 384Mi + requests: + cpu: 150m + memory: 256Mi + +postgresql: + enabled: true + image: + registry: docker.io + repository: bitnami/postgresql + tag: latest + pullPolicy: IfNotPresent + auth: + username: mad + password: mad + database: mad + primary: + persistence: + enabled: true + size: 5Gi + +go-mad: + serverPort: 8080 + jwtSecret: "tst-jwt-secret-change-me" + +ingress: + enabled: true + className: nginx + annotations: + cert-manager.io/cluster-issuer: letsencrypt-staging + nginx.ingress.kubernetes.io/ssl-redirect: "true" + hosts: + - host: mad-tst.intern-urpi.be + paths: + - path: / + pathType: Prefix + tls: + - secretName: go-mad-tls + hosts: + - mad-tst.intern-urpi.be + +certManager: + enabled: true + issuerRef: + name: letsencrypt-staging + kind: ClusterIssuer \ No newline at end of file diff --git a/helm/go-mad/values.yaml b/helm/go-mad/values.yaml index 1454cf2..95ff5dc 100644 --- a/helm/go-mad/values.yaml +++ b/helm/go-mad/values.yaml @@ -1,8 +1,11 @@ +# Default values for go-mad +# This is a base configuration - use values-dev.yaml or values-prd.yaml for deployment + replicaCount: 1 image: repository: batty1039.startdedicated.net:5000/go-mad - pullPolicy: IfNotPresent + pullPolicy: Always tag: "latest" service: @@ -17,37 +20,48 @@ resources: cpu: 100m memory: 128Mi -database: +# Database configuration (uses bitnami/postgresql subchart) +postgresql: enabled: true - postgresql: - enabled: true - auth: - username: mad - password: mad + image: + registry: docker.io + repository: bitnami/postgresql + tag: latest + pullPolicy: IfNotPresent + auth: + username: mad + password: mad database: mad + primary: + persistence: + enabled: true + size: 8Gi -ingress: - enabled: false - className: "" - annotations: {} - # kubernetes.io/ingress.class: nginx - # kubernetes.io/tls-related: ... - hosts: - - host: go-mad.local - paths: - - path: / - pathType: ImplementationSpecific - tls: [] - # - secretName: go-mad-tls - # hosts: - # - go-mad.local - +# Application configuration go-mad: - databaseURL: "postgres://mad:mad@go-guess-postgresql:5432/mad?sslmode=disable" serverPort: 8080 jwtSecret: "change-me-to-a-32-plus-char-secret" -web: +# Ingress configuration +ingress: enabled: true - type: serving - port: 8080 \ No newline at end of file + className: nginx + annotations: + cert-manager.io/cluster-issuer: letsencrypt-prod + nginx.ingress.kubernetes.io/ssl-redirect: "true" + hosts: + - host: mad.urpi.be + paths: + - path: / + pathType: Prefix + tls: + - secretName: go-mad-tls + hosts: + - mad.urpi.be + +# Cert-manager configuration +certManager: + enabled: true + issuerRef: + name: letsencrypt-prod + kind: ClusterIssuer \ No newline at end of file diff --git a/internal/database/database.go b/internal/database/database.go index 8cd003f..7a16504 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -57,14 +57,14 @@ func FindMigrations(migrationPath string) ([]Migration, error) { } contentStr := string(content) - migration := parseMigration(contentStr) + migration := parseMigration(contentStr, entry.Name()) migrations = append(migrations, migration) } return migrations, nil } -func parseMigration(content string) Migration { +func parseMigration(content string, filename string) Migration { // Parse goose-style migration: -- +goose Up / -- +goose Down lines := strings.Split(content, "\n") var upSQL strings.Builder @@ -90,9 +90,14 @@ func parseMigration(content string) Migration { // Extract version from filename (first part before _) version := "" desc := "" - if idx := strings.Index(content, "_"); idx >= 0 { - version = content[:idx] - desc = content[idx+1 : strings.Index(content, "_")] // simplistic + if idx := strings.Index(filename, "_"); idx >= 0 { + version = filename[:idx] + remaining := filename[idx+1:] + if idx2 := strings.Index(remaining, "_"); idx2 >= 0 { + desc = remaining[:idx2] + } else { + desc = strings.TrimSuffix(remaining, ".sql") + } } return Migration{ diff --git a/internal/handler/handler.go b/internal/handler/handler.go new file mode 100644 index 0000000..5907d2d --- /dev/null +++ b/internal/handler/handler.go @@ -0,0 +1,34 @@ +package handler + +import ( + "database/sql" + "log" + "net/http" +) + +type Handler struct { + db *sql.DB +} + +func New(db *sql.DB) *Handler { + log.Println("Handler routes registered") + + return &Handler{ + db: db, + } +} + +func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/health": + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(200) + w.Write([]byte(`{"status":"ok"}`)) + case "/api/v1/dashboard": + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(200) + w.Write([]byte(`{"tenants":[],"applications":[],"databases":[],"is_system_administrator":false,"selected_tenant":""}`)) + default: + http.NotFound(w, r) + } +} diff --git a/web/src/index.html b/web/index.html similarity index 68% rename from web/src/index.html rename to web/index.html index 34b746f..6f52ae0 100644 --- a/web/src/index.html +++ b/web/index.html @@ -4,10 +4,10 @@