178 lines
3.3 KiB
Go
178 lines
3.3 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func getEnv(key, defaultValue string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvAsInt(key string, defaultValue int) int {
|
|
valueStr := getEnv(key, "")
|
|
if value, err := strconv.Atoi(valueStr); err == nil {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvFromMap(config map[string]string, key, defaultValue string) string {
|
|
if value, exists := config[key]; exists {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvAsIntFromMap(config map[string]string, key string, defaultValue int) int {
|
|
if value, exists := config[key]; exists {
|
|
if intValue, err := strconv.Atoi(value); err == nil {
|
|
return intValue
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvAsBoolFromMap(config map[string]string, key string, defaultValue bool) bool {
|
|
if value, exists := config[key]; exists {
|
|
if boolValue, err := strconv.ParseBool(value); err == nil {
|
|
return boolValue
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvAsBool(key string, defaultValue bool) bool {
|
|
valueStr := getEnv(key, "")
|
|
if value, err := strconv.ParseBool(valueStr); err == nil {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
// Helper functions for getting default values based on database type
|
|
func getDefaultPort(dbType string) int {
|
|
switch dbType {
|
|
case "postgres":
|
|
return 5432
|
|
case "mysql":
|
|
return 3306
|
|
case "sqlserver":
|
|
return 1433
|
|
case "mongodb":
|
|
return 27017
|
|
case "sqlite":
|
|
return 0 // SQLite doesn't use port
|
|
default:
|
|
return 5432
|
|
}
|
|
}
|
|
|
|
func getDefaultSchema(dbType string) string {
|
|
switch dbType {
|
|
case "postgres":
|
|
return "public"
|
|
case "mysql":
|
|
return ""
|
|
case "sqlserver":
|
|
return "dbo"
|
|
case "mongodb":
|
|
return ""
|
|
case "sqlite":
|
|
return ""
|
|
default:
|
|
return "public"
|
|
}
|
|
}
|
|
|
|
func getDefaultSSLMode(dbType string) string {
|
|
switch dbType {
|
|
case "postgres":
|
|
return "disable"
|
|
case "mysql":
|
|
return "false"
|
|
case "sqlserver":
|
|
return "false"
|
|
case "mongodb":
|
|
return "false"
|
|
case "sqlite":
|
|
return ""
|
|
default:
|
|
return "disable"
|
|
}
|
|
}
|
|
|
|
func getDefaultMaxOpenConns(dbType string) int {
|
|
switch dbType {
|
|
case "postgres":
|
|
return 25
|
|
case "mysql":
|
|
return 25
|
|
case "sqlserver":
|
|
return 25
|
|
case "mongodb":
|
|
return 100
|
|
case "sqlite":
|
|
return 1 // SQLite only supports one writer at a time
|
|
default:
|
|
return 25
|
|
}
|
|
}
|
|
|
|
func getDefaultMaxIdleConns(dbType string) int {
|
|
switch dbType {
|
|
case "postgres":
|
|
return 25
|
|
case "mysql":
|
|
return 25
|
|
case "sqlserver":
|
|
return 25
|
|
case "mongodb":
|
|
return 10
|
|
case "sqlite":
|
|
return 1 // SQLite only supports one writer at a time
|
|
default:
|
|
return 25
|
|
}
|
|
}
|
|
|
|
func getDefaultConnMaxLifetime(dbType string) string {
|
|
switch dbType {
|
|
case "postgres":
|
|
return "5m"
|
|
case "mysql":
|
|
return "5m"
|
|
case "sqlserver":
|
|
return "5m"
|
|
case "mongodb":
|
|
return "30m"
|
|
case "sqlite":
|
|
return "5m"
|
|
default:
|
|
return "5m"
|
|
}
|
|
}
|
|
|
|
func parseDuration(durationStr string) time.Duration {
|
|
if duration, err := time.ParseDuration(durationStr); err == nil {
|
|
return duration
|
|
}
|
|
return 5 * time.Minute
|
|
}
|
|
|
|
func parseOrigins(originsStr string) []string {
|
|
if originsStr == "" {
|
|
return []string{"http://localhost:8080"} // Default untuk pengembangan
|
|
}
|
|
origins := strings.Split(originsStr, ",")
|
|
for i, origin := range origins {
|
|
origins[i] = strings.TrimSpace(origin)
|
|
}
|
|
return origins
|
|
}
|