Refactor environment variable handling in main.go
All checks were successful
CI / build (push) Successful in 54s
All checks were successful
CI / build (push) Successful in 54s
- Updated command-line flag definitions to use a helper function for retrieving environment variables, allowing for more flexible configuration. - Changed default values for Gitea server URL, config path, UA blocklist path, and server port to be set via environment variables. - Modified CORS middleware to accept allowed origins from an environment variable, enhancing security and configurability.
This commit is contained in:
18
main.go
18
main.go
@@ -37,10 +37,10 @@ var (
|
||||
|
||||
func main() {
|
||||
flag.StringVar(&giteaToken, "t", os.Getenv("GITEA_TOKEN"), "Gitea API Token")
|
||||
flag.StringVar(&giteaServer, "s", "https://git.quad4.io", "Gitea Server URL")
|
||||
flag.StringVar(&configPath, "c", config.DefaultConfigPath, "Path to software.txt (local or remote)")
|
||||
uaBlocklistPath := flag.String("ua-blocklist", "ua-blocklist.txt", "Path to ua-blocklist.txt (optional)")
|
||||
port := flag.String("p", "8080", "Server port")
|
||||
flag.StringVar(&giteaServer, "s", getEnv("GITEA_SERVER", "https://git.quad4.io"), "Gitea Server URL")
|
||||
flag.StringVar(&configPath, "c", getEnv("CONFIG_PATH", config.DefaultConfigPath), "Path to software.txt (local or remote)")
|
||||
uaBlocklistPath := flag.String("ua-blocklist", getEnv("UA_BLOCKLIST_PATH", "ua-blocklist.txt"), "Path to ua-blocklist.txt (optional)")
|
||||
port := flag.String("p", getEnv("PORT", "8080"), "Server port")
|
||||
isProd := flag.Bool("prod", os.Getenv("NODE_ENV") == "production", "Run in production mode")
|
||||
updateInterval := flag.Duration("u", 1*time.Hour, "Software update interval")
|
||||
flag.Parse()
|
||||
@@ -58,8 +58,9 @@ func main() {
|
||||
|
||||
r := chi.NewRouter()
|
||||
|
||||
allowedOrigins := strings.Split(getEnv("ALLOWED_ORIGINS", "*"), ",")
|
||||
r.Use(cors.Handler(cors.Options{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedOrigins: allowedOrigins,
|
||||
AllowedMethods: []string{"GET", "POST", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token", "X-Account-Number"},
|
||||
ExposedHeaders: []string{"Link"},
|
||||
@@ -172,3 +173,10 @@ func main() {
|
||||
}
|
||||
fmt.Println("Server exited properly")
|
||||
}
|
||||
|
||||
func getEnv(key, fallback string) string {
|
||||
if value, ok := os.LookupEnv(key); ok {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user