devops: deployment setup and docs
All checks were successful
Deploy / build-and-deploy (push) Successful in 1m0s

This commit is contained in:
juancwu 2026-02-09 20:48:44 +00:00
commit 6e00b7387e
6 changed files with 491 additions and 3 deletions

View file

@ -1,15 +1,23 @@
package main
import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"git.juancwu.dev/juancwu/budgit/internal/app"
"git.juancwu.dev/juancwu/budgit/internal/config"
"git.juancwu.dev/juancwu/budgit/internal/routes"
)
// version is set at build time via -ldflags.
var version = "dev"
func main() {
cfg := config.Load()
@ -26,10 +34,40 @@ func main() {
}()
handler := routes.SetupRoutes(a)
slog.Info("server starting", "host", cfg.Host, "port", cfg.Port, "env", cfg.AppEnv, "url", fmt.Sprintf("http://%s:%s", cfg.Host, cfg.Port))
err = http.ListenAndServe(":"+cfg.Port, handler)
if err != nil {
// Health check bypasses all middleware
finalHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && r.URL.Path == "/healthz" {
if err := a.DB.Ping(); err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("db: unreachable"))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok" + " - version: " + version))
return
}
handler.ServeHTTP(w, r)
})
srv := &http.Server{
Addr: ":" + cfg.Port,
Handler: finalHandler,
}
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
<-sigCh
slog.Info("shutting down gracefully")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
srv.Shutdown(ctx)
}()
slog.Info("server starting", "version", version, "host", cfg.Host, "port", cfg.Port, "env", cfg.AppEnv, "url", fmt.Sprintf("http://%s:%s", cfg.Host, cfg.Port))
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
slog.Error("server failed", "error", err)
panic(err)
}