Rip options out into separate file
This commit is contained in:
@@ -2,4 +2,5 @@ DB_PATH=db.sqlite
|
||||
ESI_CLIENT_ID=your-client-id
|
||||
ESI_REDIRECT_URI=http://localhost:3000/callback
|
||||
ESI_SCOPES=esi-planets.manage_planets.v1
|
||||
HTTP_SERVER_PORT=3000
|
||||
HTTP_SERVER_PORT=3000
|
||||
ESI_REFRESH_INTERVAL=P10M
|
||||
57
main.go
57
main.go
@@ -3,40 +3,20 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
|
||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||
|
||||
"github.com/fasthttp/router"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
ClientID string
|
||||
RedirectURI string
|
||||
Scopes []string
|
||||
DBPath string
|
||||
Port string
|
||||
}
|
||||
|
||||
var options Options
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
logger.InitFlag()
|
||||
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
|
||||
sso, err := NewSSO(
|
||||
options.ClientID,
|
||||
@@ -97,40 +77,3 @@ func main() {
|
||||
logger.Info("SIGINT received, shutting down web server gracefully...")
|
||||
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
66
options.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user