chore: initial commit of Go API gateway
Baseline for the Kong replacement on api.riotpiao.com. Brings the working tree under version control for the first time: gateway source, the task board that drives the agent runs, test fixtures, and K8s manifests. Anchor the gateway ignore rule to the repo root. Unanchored, "gateway" also matched the cmd/gateway/ source directory, so the program entrypoint was excluded from every commit. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/Riotpiaole/homelab-frontend/internal/config"
|
||||
"github.com/Riotpiaole/homelab-frontend/internal/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Load configuration
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Determine if auth is enabled by checking if any route requires it
|
||||
authEnabled := false
|
||||
for _, route := range cfg.Routes {
|
||||
if route.Upstream.AuthRequired {
|
||||
authEnabled = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Create a basic handler (will be replaced with real routing later)
|
||||
upstreamHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
fmt.Fprintf(w, "not found")
|
||||
})
|
||||
|
||||
// Create server with health checker
|
||||
srv := server.New(cfg.ListenAddr, cfg.ShutdownTimeout, nil)
|
||||
|
||||
// Initialize health checker with config validity and auth status
|
||||
healthChecker := server.NewHealthChecker(true, authEnabled)
|
||||
srv.SetHealthChecker(healthChecker)
|
||||
|
||||
// Create router that handles health endpoints and passes others to upstream
|
||||
router := server.NewRouter(healthChecker, upstreamHandler)
|
||||
srv.SetHandler(router)
|
||||
|
||||
// Set up signal handling
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
|
||||
|
||||
// Start server in a goroutine
|
||||
var serverErr error
|
||||
go func() {
|
||||
log.Printf("gateway listening on %s", srv.Addr())
|
||||
serverErr = srv.ListenAndServe()
|
||||
if serverErr != nil && serverErr != http.ErrServerClosed {
|
||||
log.Printf("server error: %v", serverErr)
|
||||
}
|
||||
}()
|
||||
|
||||
// Wait for shutdown signal
|
||||
sig := <-sigChan
|
||||
log.Printf("received signal: %v", sig)
|
||||
|
||||
// Gracefully shutdown the server
|
||||
if err := srv.Shutdown(context.Background()); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "shutdown error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
log.Printf("gateway shutdown complete")
|
||||
os.Exit(0)
|
||||
}
|
||||
Reference in New Issue
Block a user