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

View File

@@ -2,4 +2,5 @@ DB_PATH=db.sqlite
ESI_CLIENT_ID=your-client-id ESI_CLIENT_ID=your-client-id
ESI_REDIRECT_URI=http://localhost:3000/callback ESI_REDIRECT_URI=http://localhost:3000/callback
ESI_SCOPES=esi-planets.manage_planets.v1 ESI_SCOPES=esi-planets.manage_planets.v1
HTTP_SERVER_PORT=3000 HTTP_SERVER_PORT=3000
ESI_REFRESH_INTERVAL=P10M

57
main.go
View File

@@ -3,40 +3,20 @@ package main
import ( import (
"context" "context"
"flag" "flag"
"fmt"
"os" "os"
"os/signal" "os/signal"
"strings"
logger "git.site.quack-lab.dev/dave/cylogger" logger "git.site.quack-lab.dev/dave/cylogger"
"github.com/fasthttp/router" "github.com/fasthttp/router"
"github.com/joho/godotenv"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
type Options struct {
ClientID string
RedirectURI string
Scopes []string
DBPath string
Port string
}
var options Options
func main() { func main() {
flag.Parse() flag.Parse()
logger.InitFlag() logger.InitFlag()
logger.Info("Starting Eve PI") logger.Info("Starting Eve PI")
var err error
options, err = LoadOptions()
if err != nil {
logger.Error("Failed to load options %v", err)
return
}
// Create SSO instance // Create SSO instance
sso, err := NewSSO( sso, err := NewSSO(
options.ClientID, options.ClientID,
@@ -97,40 +77,3 @@ func main() {
logger.Info("SIGINT received, shutting down web server gracefully...") logger.Info("SIGINT received, shutting down web server gracefully...")
logger.Info("Web server shut down cleanly") logger.Info("Web server shut down cleanly")
} }
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
}

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
}