Perbaikan template go

This commit is contained in:
2025-08-13 09:50:09 +07:00
parent f1efac98a0
commit 315a240b08
7 changed files with 862 additions and 113 deletions

View File

@@ -9,6 +9,7 @@ import (
type Config struct {
Server ServerConfig
Database DatabaseConfig
Keycloak KeycloakConfig
}
type ServerConfig struct {
@@ -25,6 +26,13 @@ type DatabaseConfig struct {
Schema string
}
type KeycloakConfig struct {
Issuer string
Audience string
JwksURL string
Enabled bool
}
func LoadConfig() *Config {
config := &Config{
Server: ServerConfig{
@@ -39,6 +47,12 @@ func LoadConfig() *Config {
Database: getEnv("BLUEPRINT_DB_DATABASE", "api_service"),
Schema: getEnv("BLUEPRINT_DB_SCHEMA", "public"),
},
Keycloak: KeycloakConfig{
Issuer: getEnv("KEYCLOAK_ISSUER", "https://keycloak.example.com/auth/realms/yourrealm"),
Audience: getEnv("KEYCLOAK_AUDIENCE", "your-client-id"),
JwksURL: getEnv("KEYCLOAK_JWKS_URL", "https://keycloak.example.com/auth/realms/yourrealm/protocol/openid-connect/certs"),
Enabled: getEnvAsBool("KEYCLOAK_ENABLED", true),
},
}
return config
@@ -59,6 +73,14 @@ func getEnvAsInt(key string, defaultValue int) int {
return defaultValue
}
func getEnvAsBool(key string, defaultValue bool) bool {
valueStr := getEnv(key, "")
if value, err := strconv.ParseBool(valueStr); err == nil {
return value
}
return defaultValue
}
func (c *Config) Validate() error {
if c.Database.Host == "" {
log.Fatal("Database host is required")
@@ -72,5 +94,19 @@ func (c *Config) Validate() error {
if c.Database.Database == "" {
log.Fatal("Database name is required")
}
// Validate Keycloak configuration if enabled
if c.Keycloak.Enabled {
if c.Keycloak.Issuer == "" {
log.Fatal("Keycloak issuer is required when Keycloak is enabled")
}
if c.Keycloak.Audience == "" {
log.Fatal("Keycloak audience is required when Keycloak is enabled")
}
if c.Keycloak.JwksURL == "" {
log.Fatal("Keycloak JWKS URL is required when Keycloak is enabled")
}
}
return nil
}