diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-11-08 16:44:28 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-11-08 16:44:28 +0900 |
| commit | 8082931484f147189186974d0dfd7a45b8e9563b (patch) | |
| tree | 1b297613259a6e9fde11c871c62768e6972e26fc | |
| parent | 248c169c757fe9aeca0defc67cc8297e07a876d0 (diff) | |
| download | feedaka-8082931484f147189186974d0dfd7a45b8e9563b.tar.gz feedaka-8082931484f147189186974d0dfd7a45b8e9563b.tar.zst feedaka-8082931484f147189186974d0dfd7a45b8e9563b.zip | |
refactor(backend): Centralize environment variable loading in config.go
| -rw-r--r-- | backend/auth/session.go | 16 | ||||
| -rw-r--r-- | backend/cmd_serve.go | 16 | ||||
| -rw-r--r-- | backend/config.go | 35 | ||||
| -rw-r--r-- | backend/main.go | 9 | ||||
| -rw-r--r-- | compose.yaml | 1 | ||||
| -rw-r--r-- | justfile | 2 |
6 files changed, 50 insertions, 29 deletions
diff --git a/backend/auth/session.go b/backend/auth/session.go index f480bff..eaf42e7 100644 --- a/backend/auth/session.go +++ b/backend/auth/session.go @@ -3,7 +3,6 @@ package auth import ( "errors" "net/http" - "os" "github.com/gorilla/sessions" "github.com/labstack/echo-contrib/session" @@ -18,22 +17,15 @@ const ( ) var ( - ErrNoSession = errors.New("no session found") - ErrNoUserIDInSession = errors.New("no user_id in session") - ErrNoSessionSecretEnvVar = errors.New("FEEDAKA_SESSION_SECRET environment variable is not set") + ErrNoSession = errors.New("no session found") + ErrNoUserIDInSession = errors.New("no user_id in session") ) type SessionConfig struct { store *sessions.CookieStore } -func NewSessionConfig() (*SessionConfig, error) { - secret := os.Getenv("FEEDAKA_SESSION_SECRET") - if secret == "" { - return nil, ErrNoSessionSecretEnvVar - } - useNonSecureCookie := os.Getenv("FEEDAKA_DEV_NON_SECURE_COOKIE") == "1" - +func NewSessionConfig(secret string, useNonSecureCookie bool) *SessionConfig { store := sessions.NewCookieStore([]byte(secret)) store.Options = &sessions.Options{ Path: "/", @@ -45,7 +37,7 @@ func NewSessionConfig() (*SessionConfig, error) { return &SessionConfig{ store: store, - }, nil + } } func (c *SessionConfig) GetStore() *sessions.CookieStore { diff --git a/backend/cmd_serve.go b/backend/cmd_serve.go index bb3d274..1421696 100644 --- a/backend/cmd_serve.go +++ b/backend/cmd_serve.go @@ -138,12 +138,7 @@ func scheduled(ctx context.Context, d time.Duration, fn func()) { }() } -func runServe(database *sql.DB) { - port := os.Getenv("FEEDAKA_PORT") - if port == "" { - port = "8080" - } - +func runServe(database *sql.DB, config *Config) { err := db.ValidateSchemaVersion(database) if err != nil { log.Fatal(err) @@ -151,10 +146,7 @@ func runServe(database *sql.DB) { queries := db.New(database) - sessionConfig, err := auth.NewSessionConfig() - if err != nil { - log.Fatal(err) - } + sessionConfig := auth.NewSessionConfig(config.SessionSecret, config.DevNonSecureCookie) e := echo.New() @@ -233,8 +225,8 @@ func runServe(database *sql.DB) { } }() - log.Printf("Server starting on port %s...\n", port) - err = e.Start(":" + port) + log.Printf("Server starting on port %s...\n", config.Port) + err = e.Start(":" + config.Port) if err != nil && err != http.ErrServerClosed { log.Printf("Server error: %v\n", err) } diff --git a/backend/config.go b/backend/config.go new file mode 100644 index 0000000..c66100e --- /dev/null +++ b/backend/config.go @@ -0,0 +1,35 @@ +package main + +import ( + "errors" + "os" +) + +var ( + ErrNoSessionSecretEnvVar = errors.New("FEEDAKA_SESSION_SECRET environment variable is not set") +) + +type Config struct { + Port string + SessionSecret string + DevNonSecureCookie bool +} + +func LoadConfig() (*Config, error) { + port := os.Getenv("FEEDAKA_PORT") + sessionSecret := os.Getenv("FEEDAKA_SESSION_SECRET") + devNonSecureCookie := os.Getenv("FEEDAKA_DEV_NON_SECURE_COOKIE") + + if port == "" { + port = "8080" + } + if sessionSecret == "" { + return nil, ErrNoSessionSecretEnvVar + } + + return &Config{ + Port: port, + SessionSecret: sessionSecret, + DevNonSecureCookie: devNonSecureCookie == "1", + }, nil +} diff --git a/backend/main.go b/backend/main.go index aa5dc75..714d496 100644 --- a/backend/main.go +++ b/backend/main.go @@ -12,12 +12,15 @@ import ( //go:generate go tool gqlgen generate func main() { + config, err := LoadConfig() + if err != nil { + log.Fatal(err) + } + // Parse command line flags var migrate = flag.Bool("migrate", false, "Run database migrations") var createUser = flag.Bool("create-user", false, "Create a new user") flag.Parse() - - var err error database, err := sql.Open("sqlite3", "data/feedaka.db") if err != nil { log.Fatal(err) @@ -29,6 +32,6 @@ func main() { } else if *createUser { runCreateUser(database) } else { - runServe(database) + runServe(database, config) } } diff --git a/compose.yaml b/compose.yaml index b65ea57..ba00ad9 100644 --- a/compose.yaml +++ b/compose.yaml @@ -8,7 +8,6 @@ services: - '127.0.0.1:8002:8080' environment: TZ: Asia/Tokyo - FEEDAKA_PORT: '8080' env_file: - .env restart: always @@ -2,7 +2,7 @@ list: @just -l serve: - FEEDAKA_PORT=8080 ./backend/feedaka + ./backend/feedaka build: generate cd frontend && npm run build |
