From bdf7d4f4680ac9ac5f6f8b93593ad57d93414740 Mon Sep 17 00:00:00 2001 From: KartoffelChips <104089082+KartoffelChipss@users.noreply.github.com> Date: Mon, 30 Mar 2026 22:02:39 +0200 Subject: [PATCH] Moved github PAT and storage path to env vars --- .gitignore | 2 +- Dockerfile | 1 + app/handlers/static.go | 2 +- app/handlers/update.go | 7 ++++--- app/routes/routes.go | 7 ++++--- config.example.yaml | 4 ---- docker-compose.yml | 2 +- internal/config/config.go | 14 +------------- internal/envconfig/envconfig.go | 26 +++++++++++++++++++++----- main.go | 2 +- 10 files changed, 35 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 59bd15c..18b44e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .env config/config.yaml -mydeploys +storage quay config \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 3a5da3f..eddd730 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,6 +27,7 @@ USER appuser ENV PORT=4321 ENV CONFIG_DIR=/config +ENV STORAGE_PATH=/storage EXPOSE 4321 diff --git a/app/handlers/static.go b/app/handlers/static.go index c19db80..1dddb27 100644 --- a/app/handlers/static.go +++ b/app/handlers/static.go @@ -12,7 +12,7 @@ func NewStaticHandler(storagePath string, siteMap map[string]config.SiteConfig) return func(c fiber.Ctx) error { site, ok := siteMap[c.Hostname()] if !ok { - return c.SendStatus(fiber.StatusNotFound) + return c.Status(fiber.StatusNotFound).SendString("Site not found") } urlPath := filepath.Clean(c.Path()) diff --git a/app/handlers/update.go b/app/handlers/update.go index 7d3640b..623871c 100644 --- a/app/handlers/update.go +++ b/app/handlers/update.go @@ -5,6 +5,7 @@ import ( "quay/app/github" "quay/app/models" "quay/internal/config" + "quay/internal/envconfig" "strings" "crypto/subtle" @@ -12,7 +13,7 @@ import ( "github.com/gofiber/fiber/v3" ) -func NewUpdateSiteHandler(cfg *config.Config) fiber.Handler { +func NewUpdateSiteHandler(cfg *config.Config, envCfg *envconfig.EnvConfig) fiber.Handler { return func(c fiber.Ctx) error { sitename := c.Query("site") if sitename == "" { @@ -58,7 +59,7 @@ func NewUpdateSiteHandler(cfg *config.Config) fiber.Handler { }) } - sitePath := filepath.Join(cfg.Global.StoragePath, siteConfig.Name) + sitePath := filepath.Join(envCfg.StoragePath, siteConfig.Name) if _, err := filepath.Abs(sitePath); err != nil { return c.Status(500).JSON(models.APIError{ Error: "Failed to resolve site path", @@ -69,7 +70,7 @@ func NewUpdateSiteHandler(cfg *config.Config) fiber.Handler { siteConfig.Owner, siteConfig.Repo, siteConfig.Branch, - cfg.Global.GithubPat, + envCfg.GithubPat, sitePath, ) diff --git a/app/routes/routes.go b/app/routes/routes.go index af60d2b..71e4e7c 100644 --- a/app/routes/routes.go +++ b/app/routes/routes.go @@ -5,17 +5,18 @@ import ( "path/filepath" "quay/app/handlers" "quay/internal/config" + "quay/internal/envconfig" "github.com/gofiber/fiber/v3" ) -func Register(app *fiber.App, cfg *config.Config) { +func Register(app *fiber.App, cfg *config.Config, envCfg *envconfig.EnvConfig) { api := app.Group("/api") api.Get("/health", handlers.HealthCheck) - api.Post("/update", handlers.NewUpdateSiteHandler(cfg)) + api.Post("/update", handlers.NewUpdateSiteHandler(cfg, envCfg)) - storagePath, err := filepath.Abs(cfg.Global.StoragePath) + storagePath, err := filepath.Abs(envCfg.StoragePath) if err != nil { log.Fatalf("Failed to resolve storage path: %v", err) } diff --git a/config.example.yaml b/config.example.yaml index 9a34d60..aea2f43 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -2,10 +2,6 @@ # This file defines the global settings and the sites to be deployed. # You can use ${VARIABLE_NAME} to reference environment variables for sensitive information. -global: - github_pat: "${GITHUB_PAT}" - storage_path: /var/www/sites - sites: - repo: my-lib-docs owner: yourname diff --git a/docker-compose.yml b/docker-compose.yml index 2cd8f8c..0c5f7e0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,7 +10,7 @@ services: - '8080:4321' volumes: - ./config:/config - - ./mydeploys:/deploys + - ./storage:/storage restart: unless-stopped healthcheck: test: diff --git a/internal/config/config.go b/internal/config/config.go index 721aed1..2c7025d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,11 +6,6 @@ import ( "gopkg.in/yaml.v3" ) -type GlobalConfig struct { - GithubPat string `yaml:"github_pat"` - StoragePath string `yaml:"storage_path"` -} - type SiteConfig struct { Name string `yaml:"name"` Repo string `yaml:"repo"` @@ -23,8 +18,7 @@ type SiteConfig struct { } type Config struct { - Global GlobalConfig `yaml:"global"` - Sites []SiteConfig `yaml:"sites"` + Sites []SiteConfig `yaml:"sites"` } type Error struct { @@ -37,12 +31,6 @@ func (c Error) Error() string { } func validateConfig(config *Config) error { - if config.Global.GithubPat == "" { - return &Error{Field: "global.github_pat", Message: "GitHub PAT is required"} - } - if config.Global.StoragePath == "" { - return &Error{Field: "global.storage_path", Message: "Storage path is required"} - } for i, site := range config.Sites { if site.Name == "" { return &Error{Field: "sites[" + string(i) + "].name", Message: "Name is required"} diff --git a/internal/envconfig/envconfig.go b/internal/envconfig/envconfig.go index f4a66fc..625d7a0 100644 --- a/internal/envconfig/envconfig.go +++ b/internal/envconfig/envconfig.go @@ -1,10 +1,14 @@ package envconfig -import "os" +import ( + "os" +) type EnvConfig struct { - Port string - ConfigDir string + Port string + ConfigDir string + GithubPat string + StoragePath string } func Load() EnvConfig { @@ -18,8 +22,20 @@ func Load() EnvConfig { configDir = "./" } + githubPat := os.Getenv("GITHUB_PAT") + if githubPat == "" { + githubPat = "" + } + + storagePath := os.Getenv("STORAGE_PATH") + if storagePath == "" { + storagePath = "./storage" + } + return EnvConfig{ - Port: port, - ConfigDir: configDir, + Port: port, + ConfigDir: configDir, + GithubPat: githubPat, + StoragePath: storagePath, } } diff --git a/main.go b/main.go index 0542b61..51e3106 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ func main() { app := fiber.New() fiberconfig.Setup(app) - routes.Register(app, cfg) + routes.Register(app, cfg, &envCfg) log.Fatal(app.Listen(":" + envCfg.Port)) }