diff --git a/main.go b/main.go index 197a24f..7863f72 100644 --- a/main.go +++ b/main.go @@ -1,121 +1,83 @@ package main import ( + "context" "log" "net/http" "os" - "strings" - "time" - "context" "os/signal" + "sync" "syscall" + "time" "github.com/gorilla/mux" - "golang.org/x/time/rate" ) -func noListing(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - if strings.HasSuffix(req.URL.Path, "/") { - http.NotFound(w, req) - return - } - next.ServeHTTP(w, req) - }) -} - -func securityHeaders(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - w.Header().Set("X-Content-Type-Options", "nosniff") - w.Header().Set("X-Frame-Options", "DENY") - w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin") - next.ServeHTTP(w, req) - }) -} - -var webhookLimiter = rate.NewLimiter(rate.Every(10*time.Second), 1) - -func rateLimit(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - if !webhookLimiter.Allow() { - log.Println("webhook rate limit exceeded") - w.WriteHeader(http.StatusTooManyRequests) - return - } - next.ServeHTTP(w, req) - }) -} - -func wkdHeaders(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - w.Header().Set("Content-Type", "application/octet-stream") - w.Header().Set("Access-Control-Allow-Origin", "*") - next.ServeHTTP(w, req) - }) -} - -func healthHandler(w http.ResponseWriter, req *http.Request) { - w.WriteHeader(http.StatusOK) - w.Write([]byte("ok")) -} - func main() { cfg := loadConfig() - if cfg.EnableWebhook { - cloneOrOpen(cfg) + modules := []Module { + // Module werden hier registriert, sobald sie existieren, zB.: + // &webserver.Module{}, + // &keyserver.Module{}, + // &webhook.Module{}, + // &resourcemonitor.Module{}, } - contentFS := http.FileServer(http.Dir(cfg.ContentDir)) - keysFS := http.FileServer(http.Dir(cfg.KeysDir)) - - r := mux.NewRouter() r.Use(securityHeaders) - - api := r.PathPrefix("/api/").Subrouter() - api.HandleFunc("/healthz", healthHandler).Methods("GET", "HEAD") - - if cfg.EnableWebhook { - r.Handle("/webhook", rateLimit(webhookHandler(cfg))).Methods("POST") + var active []Module + for _, m := range modules { + log.Printf("module %s: disabled, skipping", m.Name()) + continue } - // Eigener Bereich für .well-known: kein Listing, WKD-Header - wkd := r.PathPrefix("/.well-known/").Subrouter() - wkd.Use(noListing) - wkd.Use(wkdHeaders) - wkd.PathPrefix("/").Handler(keysFS) + ctx, cancel := context.WithCancel(context.Background()) - // Alles andere: normaler Fileserver, Listing erlaubt, hier kommt später zB /downloads/ rein - r.PathPrefix("/").Handler(contentFS) + var wg sync.WaitGroup + for :, m := range active { + wg.Add(1) + go func(m Module) { + defer wg.Done() + if err := m.Start(ctx, cfg); err != nil { + log.Printf("module %s: exited with error: %v" m.Name(), err) + } + }(m) + } - srv := &http.Server{ - Addr: ":" + cfg.Port, + srv := &http.Server { + Addr: ":" + cfg.Port, Handler: r, } - // Server in eigener Goroutine starten, damit main() weiterlaufen kann go func() { - log.Printf("Listening on :%s, content=%s keys=%s (webhook: %v)", cfg.Port, cfg.ContentDir, cfg.KeysDir, cfg.EnableWebhook) + log.Printf("Listening on :%s, cfg.Port) if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { log.Fatalf("server error: %v", err) } }() - // Auf SIGTERM/SIGINT warten stop := make(chan os.Signal, 1) - signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT) + signal.Notify(stop, syscall.SIGTERM, syscall.SIGINIT) <-stop - log.Println("Shutdown signal received, draining connections...") + log.Pritln("Shutdown siganl received, draining connections...") + cancel() // signals all moduke Start() goroutines to stop via ctx - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() + shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second) + defer shutdownCancel() - if err := srv.Shutdown(ctx); err != nil { - log.Printf("graceful shutdown failed: %v", err) - } else { - log.Println("Server shut down cleanly") + if err := srv.Shutdown(shutdownCtx); err != nil { + log.Printf("graceful shutdown failed %v", err) } + + for _, m := range active { + if err := srv.Shutdown(shutdownCtx); err != nil { + log.Printf("module %s: stop error: %v", m.Name(), err) + } + } + + wg.Wait() + log.Println("Server shut down cleanly") } diff --git a/module.go b/module.go new file mode 100644 index 0000000..d82d5be --- /dev/null +++ b/module.go @@ -0,0 +1,39 @@ +package main + +import ( + "context" + "net/http" + + "github.com/gorilla/mux" +) + +// Module represents a self-contained feature of Varde. +// Each module is independently enabled/disabled via config and +// runs in the same process, sharing the HTTP router where relevant. +type Module interface { + // Name returns a short, unique identifier used in logs and config. + Name() + + // Enabled reports whether this module should be started + // based on the loaded configuration. + Enabled(cfg *Config) bool + + // RegisterRoutes attaches this module's HTTP routes to the shared router, if any. + // Modules without HTTP routes should leave this empty. + RegisterRoutes(r *musx.Router, cfg *Config) + + // Start runs any background work for this module (eg. periodic checks, repo cloning). + // Should block until ctx is cancelled, or return immediately if the module has no background work. + Start(ctx context.Context, cfg *Config) error + + // Stop performs graceful cleanup. Called druing shutdown for every module that was successfully started. + Stop(ctx context.Context) error +} + +// baseModule provides no-op defaults so modules only need to implement the methods relevant to them. +type baseModule struct{} + +func (baseModule) RegisterRoutes(r *mux.Router, cfg *Config){} +func (baseModule) Start(cxt context.Context, cfg *Config) error {return nil} + +var _ = http.MethodGet // placeholder import guard, remove once a module uses net/http directly here