package config

This commit is contained in:
henk
2026-05-15 14:22:16 +02:00
parent 1254f70a5f
commit 107c23a29d
7 changed files with 180 additions and 2 deletions

View File

@@ -0,0 +1,11 @@
package server
import (
"net/http"
)
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"ok"}`))
}

72
internal/server/server.go Normal file
View File

@@ -0,0 +1,72 @@
package server
import (
"context"
"fmt"
"log/slog"
"net/http"
"git.systemact.nl/henk/mars-terraform-registry/internal/config"
)
type Server struct {
httpServer *http.Server
cfg *config.Config
}
func NewServer(ctx context.Context, cfgLoader config.ConfigLoader) (*Server, error) {
cfg, err := cfgLoader.LoadConfig()
if err != nil {
return nil, fmt.Errorf("failed to load config: %w", err)
}
srv := &Server{
cfg: cfg,
}
err = srv.setup()
return srv, err
}
func (s *Server) setup() error {
mux := http.NewServeMux()
mux.HandleFunc("/health", s.handleHealth)
s.httpServer = &http.Server{
Handler: mux,
}
return nil
}
func (s *Server) Run(ctx context.Context) error {
slog.Info("Starting AgentGopher gateway", "address", s.cfg.Server.BindAddress)
errCh := make(chan error, 1)
go func() {
if s.cfg.TLS.Enabled {
errCh <- s.httpServer.ListenAndServeTLS(s.cfg.TLS.CertFile, s.cfg.TLS.KeyFile)
} else {
errCh <- s.httpServer.ListenAndServe()
}
}()
select {
case <-ctx.Done():
slog.Info("Shutdown signal received")
return s.Shutdown(context.Background())
case err := <-errCh:
return err
}
}
func (s *Server) Shutdown(ctx context.Context) error {
var errs []error
if err := s.httpServer.Shutdown(ctx); err != nil {
errs = append(errs, fmt.Errorf("http server shutdown: %w", err))
}
if len(errs) > 0 {
return fmt.Errorf("shutdown errors: %v", errs)
}
return nil
}