diff --git a/app/middleware/api_host_guard.go b/app/middleware/api_host_guard.go new file mode 100644 index 0000000..d6a69cc --- /dev/null +++ b/app/middleware/api_host_guard.go @@ -0,0 +1,12 @@ +package middleware + +import "github.com/gofiber/fiber/v3" + +func APIHostGuard(allowedHost string) fiber.Handler { + return func(c fiber.Ctx) error { + if c.Hostname() != allowedHost { + return c.Status(fiber.StatusNotFound).SendString("Not Found") + } + return c.Next() + } +} diff --git a/app/routes/routes.go b/app/routes/routes.go index fb0f4cb..753c26e 100644 --- a/app/routes/routes.go +++ b/app/routes/routes.go @@ -6,6 +6,8 @@ import ( "path/filepath" "quay/app/cachedrepo" "quay/app/handlers" + "quay/app/middleware" + "quay/app/models" "quay/internal/config" "quay/internal/database" "quay/internal/envconfig" @@ -19,7 +21,7 @@ func Register(app *fiber.App, cfg *config.Config, envCfg *envconfig.EnvConfig, d updateSiteHandler := handlers.NewUpdateSiteHandler(cfg, envCfg) siteHandler := handlers.NewSiteHandler(siteRepository) - api := app.Group("/api/v1") + api := app.Group("/api/v1", middleware.APIHostGuard(envCfg.ApiHost)) api.Get("/health", handlers.HealthCheck) api.Post("/update", updateSiteHandler.PostUpdate) @@ -48,6 +50,12 @@ func Register(app *fiber.App, cfg *config.Config, envCfg *envconfig.EnvConfig, d api.Put("/headers/:id", siteHandler.PutHeader) api.Delete("/headers/:id", siteHandler.DeleteHeader) + api.Use(func(c fiber.Ctx) error { + return c.Status(fiber.StatusNotFound).JSON(&models.APIError{ + Message: "Endpoint not found", + }) + }) + storagePath, err := filepath.Abs(envCfg.StoragePath) if err != nil { log.Fatalf("Failed to resolve storage path: %v", err) diff --git a/internal/envconfig/envconfig.go b/internal/envconfig/envconfig.go index 565a24d..781a8c9 100644 --- a/internal/envconfig/envconfig.go +++ b/internal/envconfig/envconfig.go @@ -5,11 +5,11 @@ import ( ) type EnvConfig struct { - Port string - ConfigDir string - GithubPat string - StoragePath string - DatabasePath string + Port string + ConfigDir string + GithubPat string + StoragePath string + ApiHost string } func Load() EnvConfig { @@ -33,10 +33,16 @@ func Load() EnvConfig { storagePath = "./storage" } + apiHost := os.Getenv("API_HOST") + if apiHost == "" { + apiHost = "localhost" + } + return EnvConfig{ Port: port, ConfigDir: configDir, GithubPat: githubPat, StoragePath: storagePath, + ApiHost: apiHost, } }