Added api host guard
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user