diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-07-28 19:41:04 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-07-28 19:41:04 +0900 |
| commit | 7bd55ee264f7eefda6c1f71865a2c6287d7e20fa (patch) | |
| tree | 269110eeb9250da7cc0f80a948ea647f69ca9be5 /backend | |
| parent | 0b09103e894a5f7c6865e5bdd20992457cec3c9f (diff) | |
| download | phperkaigi-2025-albatross-7bd55ee264f7eefda6c1f71865a2c6287d7e20fa.tar.gz phperkaigi-2025-albatross-7bd55ee264f7eefda6c1f71865a2c6287d7e20fa.tar.zst phperkaigi-2025-albatross-7bd55ee264f7eefda6c1f71865a2c6287d7e20fa.zip | |
refactor(backend): move config-related code to config.go
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/config.go | 44 | ||||
| -rw-r--r-- | backend/main.go | 49 |
2 files changed, 47 insertions, 46 deletions
diff --git a/backend/config.go b/backend/config.go new file mode 100644 index 0000000..c083542 --- /dev/null +++ b/backend/config.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "os" +) + +type Config struct { + dbHost string + dbPort string + dbUser string + dbPassword string + dbName string +} + +func NewConfigFromEnv() (*Config, error) { + dbHost, exists := os.LookupEnv("ALBATROSS_DB_HOST") + if !exists { + return nil, fmt.Errorf("ALBATROSS_DB_HOST not set") + } + dbPort, exists := os.LookupEnv("ALBATROSS_DB_PORT") + if !exists { + return nil, fmt.Errorf("ALBATROSS_DB_PORT not set") + } + dbUser, exists := os.LookupEnv("ALBATROSS_DB_USER") + if !exists { + return nil, fmt.Errorf("ALBATROSS_DB_USER not set") + } + dbPassword, exists := os.LookupEnv("ALBATROSS_DB_PASSWORD") + if !exists { + return nil, fmt.Errorf("ALBATROSS_DB_PASSWORD not set") + } + dbName, exists := os.LookupEnv("ALBATROSS_DB_NAME") + if !exists { + return nil, fmt.Errorf("ALBATROSS_DB_NAME not set") + } + return &Config{ + dbHost: dbHost, + dbPort: dbPort, + dbUser: dbUser, + dbPassword: dbPassword, + dbName: dbName, + }, nil +} diff --git a/backend/main.go b/backend/main.go index 708c5f2..46c7ed8 100644 --- a/backend/main.go +++ b/backend/main.go @@ -5,7 +5,6 @@ import ( "fmt" "log" "net/http" - "os" "strconv" "github.com/jackc/pgx/v5" @@ -17,46 +16,6 @@ import ( "github.com/nsfisis/iosdc-2024-albatross/backend/db" ) -type Config struct { - dbHost string - dbPort string - dbUser string - dbPassword string - dbName string -} - -var config *Config - -func loadEnv() (*Config, error) { - dbHost, exists := os.LookupEnv("ALBATROSS_DB_HOST") - if !exists { - return nil, fmt.Errorf("ALBATROSS_DB_HOST not set") - } - dbPort, exists := os.LookupEnv("ALBATROSS_DB_PORT") - if !exists { - return nil, fmt.Errorf("ALBATROSS_DB_PORT not set") - } - dbUser, exists := os.LookupEnv("ALBATROSS_DB_USER") - if !exists { - return nil, fmt.Errorf("ALBATROSS_DB_USER not set") - } - dbPassword, exists := os.LookupEnv("ALBATROSS_DB_PASSWORD") - if !exists { - return nil, fmt.Errorf("ALBATROSS_DB_PASSWORD not set") - } - dbName, exists := os.LookupEnv("ALBATROSS_DB_NAME") - if !exists { - return nil, fmt.Errorf("ALBATROSS_DB_NAME not set") - } - return &Config{ - dbHost: dbHost, - dbPort: dbPort, - dbUser: dbUser, - dbPassword: dbPassword, - dbName: dbName, - }, nil -} - const ( gameTypeGolf = "golf" ) @@ -121,16 +80,14 @@ func handleGolfPost(w http.ResponseWriter, r *http.Request) { func main() { var err error - config, err = loadEnv() + config, err := NewConfigFromEnv() if err != nil { - fmt.Printf("Error loading env %v", err) - return + log.Fatalf("Error loading env %v", err) } openApiSpec, err := api.GetSwaggerWithPrefix("/api") if err != nil { - fmt.Printf("Error loading OpenAPI spec\n: %s", err) - return + log.Fatalf("Error loading OpenAPI spec\n: %s", err) } ctx := context.Background() |
