115 lines
4.6 KiB
Go
115 lines
4.6 KiB
Go
package config
|
|
|
|
import (
|
|
"reflect"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// EnvVar represents an environment variable with its documentation
|
|
type EnvVar struct {
|
|
Name string
|
|
Description string
|
|
Default string
|
|
HasDefault bool
|
|
Required bool
|
|
}
|
|
|
|
// extractEnvVars parses a struct's field comments to extract environment variable documentation
|
|
func extractEnvVars(structType reflect.Type, fieldIndex int) *EnvVar {
|
|
field := structType.Field(fieldIndex)
|
|
tag := field.Tag.Get("comment")
|
|
if tag == "" {
|
|
// Try to get the comment from the struct field's tag or use reflection
|
|
// For now, we'll parse it manually from the comment string
|
|
return nil
|
|
}
|
|
|
|
comment := tag
|
|
if !strings.HasPrefix(comment, "ENV ") {
|
|
return nil
|
|
}
|
|
|
|
// Remove "ENV " prefix
|
|
comment = strings.TrimPrefix(comment, "ENV ")
|
|
|
|
// Extract name and description
|
|
parts := strings.SplitN(comment, ":", 2)
|
|
if len(parts) != 2 {
|
|
return nil
|
|
}
|
|
|
|
name := strings.TrimSpace(parts[0])
|
|
desc := strings.TrimSpace(parts[1])
|
|
|
|
// Check for default value in description
|
|
defaultRegex := regexp.MustCompile(`\(default:\s*([^)]+)\)`)
|
|
matches := defaultRegex.FindStringSubmatch(desc)
|
|
|
|
envVar := &EnvVar{
|
|
Name: name,
|
|
Description: desc,
|
|
}
|
|
|
|
if len(matches) > 1 {
|
|
envVar.Default = matches[1]
|
|
envVar.HasDefault = true
|
|
// Remove the default notation from description
|
|
envVar.Description = strings.TrimSpace(defaultRegex.ReplaceAllString(desc, ""))
|
|
}
|
|
|
|
return envVar
|
|
}
|
|
|
|
// GetAllEnvVars returns a list of all environment variables used in the config
|
|
func GetAllEnvVars() []EnvVar {
|
|
var envVars []EnvVar
|
|
|
|
// Manually define all env vars based on the config structs
|
|
// This is more reliable than reflection for extracting comments
|
|
|
|
// DBConfig
|
|
envVars = append(envVars, []EnvVar{
|
|
{Name: "DB_USER", Description: "Database user for authentication", HasDefault: false, Required: true},
|
|
{Name: "DB_PASSWORD", Description: "Database password for authentication", HasDefault: false, Required: true},
|
|
{Name: "DB_HOST", Description: "Database host address", HasDefault: false, Required: true},
|
|
{Name: "DB_PORT", Description: "Database port", Default: "5432", HasDefault: true, Required: false},
|
|
{Name: "DB_NAME", Description: "Database name to connect to", HasDefault: false, Required: true},
|
|
{Name: "DB_SSL", Description: "SSL mode for connection", Default: "disable", HasDefault: true, Required: false},
|
|
}...)
|
|
|
|
// HWSConfig
|
|
envVars = append(envVars, []EnvVar{
|
|
{Name: "HWS_HOST", Description: "Host to listen on", Default: "127.0.0.1", HasDefault: true, Required: false},
|
|
{Name: "HWS_PORT", Description: "Port to listen on", Default: "3000", HasDefault: true, Required: false},
|
|
{Name: "HWS_TRUSTED_HOST", Description: "Domain/Hostname to accept as trusted", Default: "same as Host", HasDefault: true, Required: false},
|
|
{Name: "HWS_SSL", Description: "Flag for SSL Mode", Default: "false", HasDefault: true, Required: false},
|
|
{Name: "HWS_GZIP", Description: "Flag for GZIP compression on requests", Default: "false", HasDefault: true, Required: false},
|
|
{Name: "HWS_READ_HEADER_TIMEOUT", Description: "Timeout for reading request headers in seconds", Default: "2", HasDefault: true, Required: false},
|
|
{Name: "HWS_WRITE_TIMEOUT", Description: "Timeout for writing requests in seconds", Default: "10", HasDefault: true, Required: false},
|
|
{Name: "HWS_IDLE_TIMEOUT", Description: "Timeout for idle connections in seconds", Default: "120", HasDefault: true, Required: false},
|
|
}...)
|
|
|
|
// HWSAUTHConfig
|
|
envVars = append(envVars, []EnvVar{
|
|
{Name: "HWSAUTH_SECRET_KEY", Description: "Secret key for signing tokens", HasDefault: false, Required: true},
|
|
{Name: "HWSAUTH_ACCESS_TOKEN_EXPIRY", Description: "Access token expiry in minutes", Default: "5", HasDefault: true, Required: false},
|
|
{Name: "HWSAUTH_REFRESH_TOKEN_EXPIRY", Description: "Refresh token expiry in minutes", Default: "1440", HasDefault: true, Required: false},
|
|
{Name: "HWSAUTH_TOKEN_FRESH_TIME", Description: "Time for tokens to stay fresh in minutes", Default: "5", HasDefault: true, Required: false},
|
|
}...)
|
|
|
|
// TMDBConfig
|
|
envVars = append(envVars, []EnvVar{
|
|
{Name: "TMDB_TOKEN", Description: "API token for TMDB", HasDefault: false, Required: true},
|
|
}...)
|
|
|
|
// HLOGConfig
|
|
envVars = append(envVars, []EnvVar{
|
|
{Name: "LOG_LEVEL", Description: "Log level for global logging", Default: "info", HasDefault: true, Required: false},
|
|
{Name: "LOG_OUTPUT", Description: "Output method for the logger (file, console, or both)", Default: "console", HasDefault: true, Required: false},
|
|
{Name: "LOG_DIR", Description: "Path to create log files", HasDefault: false, Required: false},
|
|
}...)
|
|
|
|
return envVars
|
|
}
|