- Replace global variable pattern with proper dependency injection - Add uber-go/fx for automatic dependency resolution - Refactor all services and handlers to use constructor injection - Eliminate fragile initialization order dependencies - Improve testability and modularity - Add structured logging with zap Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
129 lines
3.2 KiB
Go
129 lines
3.2 KiB
Go
package config
|
|
|
|
import (
|
|
"log/slog"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
AdminPassword string
|
|
Instance string
|
|
Tag string
|
|
|
|
JwtSecret string
|
|
JwtIssuer string
|
|
JwtAudience string
|
|
|
|
DBEngine string
|
|
DBHost string
|
|
DBPort string
|
|
DBUser string
|
|
DBPassword string
|
|
DBName string
|
|
|
|
ImageKitId string
|
|
|
|
GiteaOauthInstance string
|
|
GiteaOauthClientID string
|
|
GiteaOauthClientSecret string
|
|
GiteaOauthAllowedEmails []string
|
|
}
|
|
|
|
func Load() *Config {
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
slog.Warn("Error loading .env file - Using environment variables instead")
|
|
}
|
|
|
|
// Get mastodon instance
|
|
instance := os.Getenv("CAOM_INSTANCE")
|
|
if instance == "" {
|
|
instance = "https://mstdn.party"
|
|
}
|
|
// Get mastodon tag
|
|
tag := os.Getenv("CAOM_TAG")
|
|
if tag == "" {
|
|
tag = "catsofmastodon"
|
|
}
|
|
// Get admin password (Its a single user/admin app so its just fine)
|
|
adminPassword := os.Getenv("CAOM_ADMIN_PASSWORD")
|
|
if adminPassword == "" {
|
|
slog.Warn("No admin password provided, using default password 'catsaregood'")
|
|
adminPassword = "catsaregood"
|
|
}
|
|
|
|
// Jwt params
|
|
secret := os.Getenv("CAOM_JWT_SECRET")
|
|
if secret == "" {
|
|
panic("No jwt secret provided")
|
|
}
|
|
issuer := os.Getenv("CAOM_JWT_ISSUER")
|
|
if issuer == "" {
|
|
slog.Info("No jwt issuer provided, using default issuer 'CatsOfMastodonBotGo'")
|
|
issuer = "CatsOfMastodonBotGo"
|
|
}
|
|
audience := os.Getenv("CAOM_JWT_AUDIENCE")
|
|
if audience == "" {
|
|
slog.Info("No jwt audience provided, using default audience 'CatsOfMastodonBotGo'")
|
|
audience = "CatsOfMastodonBotGo"
|
|
}
|
|
|
|
dbEngine := os.Getenv("CAOM_DB_ENGINE")
|
|
dbHost := os.Getenv("CAOM_DB_HOST")
|
|
dbPort := os.Getenv("CAOM_DB_PORT")
|
|
dbUser := os.Getenv("CAOM_DB_USER")
|
|
dbPassword := os.Getenv("CAOM_DB_PASSWORD")
|
|
dbName := os.Getenv("CAOM_DB_NAME")
|
|
|
|
giteaOauthInstance := os.Getenv("CAOM_GITEA_OAUTH_INSTANCE")
|
|
giteaOauthClientID := os.Getenv("CAOM_GITEA_OAUTH_CLIENT_ID")
|
|
giteaOauthClientSecret := os.Getenv("CAOM_GITEA_OAUTH_CLIENT_SECRET")
|
|
|
|
giteaOauthAllowedEmails := os.Getenv("CAOM_GITEA_OAUTH_ALLOWED_EMAILS")
|
|
var giteaOauthAllowedEmailsParsed []string
|
|
if giteaOauthAllowedEmails != "" {
|
|
giteaOauthAllowedEmailsParsed = strings.Split(giteaOauthAllowedEmails, ",")
|
|
}
|
|
|
|
if dbEngine == "" || dbHost == "" || dbPort == "" || dbUser == "" || dbPassword == "" || dbName == "" {
|
|
slog.Info("No database connection provided, using sqlite")
|
|
dbEngine = "sqlite"
|
|
dbHost = ""
|
|
dbPort = ""
|
|
dbUser = ""
|
|
dbPassword = ""
|
|
dbName = "caom.db"
|
|
}
|
|
|
|
imageKitId := os.Getenv("CAOM_IMAGEKIT_ID")
|
|
if imageKitId == "" {
|
|
slog.Info("No imagekit id provided, not using imagekit.io")
|
|
}
|
|
// Initialize AppContext
|
|
return &Config{
|
|
AdminPassword: adminPassword,
|
|
Instance: instance,
|
|
Tag: tag,
|
|
|
|
JwtSecret: secret,
|
|
JwtIssuer: issuer,
|
|
JwtAudience: audience,
|
|
|
|
DBEngine: dbEngine,
|
|
DBHost: dbHost,
|
|
DBPort: dbPort,
|
|
DBUser: dbUser,
|
|
DBPassword: dbPassword,
|
|
DBName: dbName,
|
|
|
|
ImageKitId: imageKitId,
|
|
|
|
GiteaOauthInstance: giteaOauthInstance,
|
|
GiteaOauthClientID: giteaOauthClientID,
|
|
GiteaOauthClientSecret: giteaOauthClientSecret,
|
|
GiteaOauthAllowedEmails: giteaOauthAllowedEmailsParsed,
|
|
}
|
|
} |