Rip options out into separate file

This commit is contained in:
2025-10-10 20:39:18 +02:00
parent a296a4c684
commit 75b1045983
3 changed files with 68 additions and 58 deletions

66
options.go Normal file
View File

@@ -0,0 +1,66 @@
package main
import (
"fmt"
"os"
"strings"
logger "git.site.quack-lab.dev/dave/cylogger"
"github.com/joho/godotenv"
)
var options Options
func init() {
var err error
options, err = LoadOptions()
if err != nil {
logger.Error("Failed to load options %v", err)
return
}
}
type Options struct {
ClientID string
RedirectURI string
Scopes []string
DBPath string
Port string
}
func LoadOptions() (Options, error) {
// Load environment variables strictly from .env file (fail if there's an error loading)
if err := godotenv.Load(); err != nil {
return Options{}, fmt.Errorf("error loading .env file: %w", err)
}
clientID := os.Getenv("ESI_CLIENT_ID")
if clientID == "" {
return Options{}, fmt.Errorf("ESI_CLIENT_ID is required in .env file")
}
redirectURI := os.Getenv("ESI_REDIRECT_URI")
if redirectURI == "" {
return Options{}, fmt.Errorf("ESI_REDIRECT_URI is required in .env file")
}
rawScopes := os.Getenv("ESI_SCOPES")
if rawScopes == "" {
return Options{}, fmt.Errorf("ESI_SCOPES is required in .env file")
}
scopes := strings.Fields(rawScopes)
dbPath := os.Getenv("DB_PATH")
if dbPath == "" {
return Options{}, fmt.Errorf("DB_PATH is required in .env file")
}
port := os.Getenv("HTTP_SERVER_PORT")
if port == "" {
return Options{}, fmt.Errorf("HTTP_SERVER_PORT is required in .env file")
}
return Options{
ClientID: clientID,
RedirectURI: redirectURI,
Scopes: scopes,
DBPath: dbPath,
Port: port,
}, nil
}