flightlesssomething/config.go

75 lines
2.3 KiB
Go
Raw Permalink Normal View History

2024-07-04 22:29:32 +02:00
package flightlesssomething
import (
"errors"
"flag"
"github.com/csmith/envflag"
)
type Config struct {
Bind string
DataDir string
SessionSecret string
2024-07-04 22:29:32 +02:00
DiscordClientID string
DiscordClientSecret string
DiscordRedirectURL string
OpenAIURL string
OpenAIApiKey string
OpenAIModel string
2024-07-04 22:29:32 +02:00
Version bool
}
func NewConfig() (*Config, error) {
config := &Config{}
2024-07-04 22:29:32 +02:00
flag.StringVar(&config.Bind, "bind", "0.0.0.0:8080", "Bind address and port")
flag.StringVar(&config.DataDir, "data-dir", "/data", "Path where data would be stored")
flag.StringVar(&config.SessionSecret, "session-secret", "", "Session secret")
2024-07-04 22:29:32 +02:00
flag.StringVar(&config.DiscordClientID, "discord-client-id", "", "Discord OAuth2 client ID (see https://discord.com/developers/applications)")
flag.StringVar(&config.DiscordClientSecret, "discord-client-secret", "", "Discord OAuth2 client secret (see https://discord.com/developers/applications)")
flag.StringVar(&config.DiscordRedirectURL, "discord-redirect-url", "", "Discord OAuth2 redirect URL (<scheme>://<domain>/login/callback)")
2024-07-04 22:29:32 +02:00
flag.StringVar(&config.OpenAIURL, "openai-url", "https://api.openai.com/v1", "OpenAI API URL")
flag.StringVar(&config.OpenAIModel, "openai-model", "gpt-4o", "OpenAI model ID")
flag.StringVar(&config.OpenAIApiKey, "openai-api-key", "", "OpenAI API Key (leave empty to disable OpenAI integration)")
flag.BoolVar(&config.Version, "version", false, "prints version of the application")
envflag.Parse(envflag.WithPrefix("FS_"))
2024-07-04 22:29:32 +02:00
if config.Version {
return config, nil
}
if config.DataDir == "" {
return nil, errors.New("missing data-dir argument")
}
if config.DiscordClientID == "" {
return nil, errors.New("missing discord-client-id argument")
}
if config.DiscordClientSecret == "" {
return nil, errors.New("missing discord-client-secret argument")
}
if config.DiscordRedirectURL == "" {
return nil, errors.New("missing discord-redirect-url argument")
}
2024-07-12 22:57:42 +02:00
if config.SessionSecret == "" {
return nil, errors.New("missing session-secret argument")
}
if config.OpenAIApiKey != "" {
if config.OpenAIModel == "" {
return nil, errors.New("missing openai-model argument")
}
if config.OpenAIURL == "" {
return nil, errors.New("missing openai-url argument")
}
}
2024-07-04 22:29:32 +02:00
return config, nil
}