hatch-surf/cmd/hatch/main.go
Riley Zhang 12775f836c Phase 4: Production readiness - graceful shutdown, health checks, load testing, pprof
- Graceful shutdown: SIGINT/SIGTERM handling with 5s timeout
- Readiness probe (/readyz): pings database before reporting ready
- Connection pool: documented MaxOpenConns(1) for SQLite single-writer constraint
- Load test tool: cmd/loadtest with configurable concurrency/total requests
- Shell script: scripts/load-test.sh for quick load testing
- pprof endpoints: /debug/pprof/* when HATCH_DEBUG env var is set

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-06-24 05:26:42 +02:00

70 lines
1.4 KiB
Go

package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/elfoundation/hatch/internal/handler"
"github.com/elfoundation/hatch/internal/store"
"github.com/go-chi/chi/v5"
)
func main() {
// CLI mode: if a subcommand is provided, handle it and exit.
if cliMain() {
return
}
// Server mode: start the HTTP server.
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
dbPath := os.Getenv("HATCH_DB_PATH")
repo, err := store.Open(dbPath)
if err != nil {
log.Fatalf("hatch: open store: %v", err)
}
defer repo.Close()
h := handler.New(repo)
h.Debug = os.Getenv("HATCH_DEBUG") != ""
r := chi.NewRouter()
h.RegisterRoutes(r)
addr := fmt.Sprintf(":%s", port)
srv := &http.Server{
Addr: addr,
Handler: r,
}
// Start server in a goroutine.
go func() {
log.Printf("hatch starting on %s", addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("hatch server error: %v", err)
}
}()
// Wait for interrupt signal for graceful shutdown.
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("shutting down server...")
// Create a deadline for shutdown.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("server forced to shutdown: %v", err)
}
log.Println("server exited")
}