- Introduced Dockerfile and .dockerignore for containerization of the backend service. - Added logging configuration with support for daily rolling logs and customizable log levels. - Enhanced the server to utilize structured logging with zap, including detailed request and error logging. - Updated configuration to include logging parameters and proxy settings. - Implemented tests for logging configuration and proxy settings.
238 lines
6.9 KiB
Go
238 lines
6.9 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoadConfigNacosDefaults(t *testing.T) {
|
|
withEnv(t, "ENABLE_NACOS_REGISTER", "")
|
|
withEnv(t, "NACOS_SERVER_ADDR", "")
|
|
withEnv(t, "NACOS_NAMESPACE", "")
|
|
withEnv(t, "NACOS_GROUP", "")
|
|
withEnv(t, "NACOS_SERVICE_NAME", "")
|
|
withEnv(t, "NACOS_CLUSTER_NAME", "")
|
|
withEnv(t, "NACOS_USERNAME", "")
|
|
withEnv(t, "NACOS_PASSWORD", "")
|
|
withEnv(t, "NACOS_IP", "")
|
|
withEnv(t, "NACOS_PORT", "")
|
|
withEnv(t, "PORT", "")
|
|
withEnv(t, "INSTANCE_URL", "")
|
|
|
|
cfg, err := loadConfig()
|
|
if err != nil {
|
|
t.Fatalf("loadConfig() error = %v", err)
|
|
}
|
|
|
|
if cfg.EnableNacosRegister {
|
|
t.Fatalf("expected EnableNacosRegister default false")
|
|
}
|
|
if cfg.NacosServerAddr != "10.0.0.10:8848" {
|
|
t.Fatalf("expected default NacosServerAddr 10.0.0.10:8848, got %q", cfg.NacosServerAddr)
|
|
}
|
|
if cfg.NacosGroup != "DEFAULT_GROUP" {
|
|
t.Fatalf("expected default NacosGroup DEFAULT_GROUP, got %q", cfg.NacosGroup)
|
|
}
|
|
if cfg.NacosServiceName != "lsp-gateway" {
|
|
t.Fatalf("expected default NacosServiceName lsp-gateway, got %q", cfg.NacosServiceName)
|
|
}
|
|
if cfg.NacosRegisterPort != 8080 {
|
|
t.Fatalf("expected default NacosRegisterPort 8080, got %d", cfg.NacosRegisterPort)
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigNacosFromEnv(t *testing.T) {
|
|
withEnv(t, "ENABLE_NACOS_REGISTER", "true")
|
|
withEnv(t, "NACOS_SERVER_ADDR", "10.0.0.10:8848")
|
|
withEnv(t, "NACOS_NAMESPACE", "prod-ns")
|
|
withEnv(t, "NACOS_GROUP", "editor")
|
|
withEnv(t, "NACOS_SERVICE_NAME", "editor-lsp")
|
|
withEnv(t, "NACOS_CLUSTER_NAME", "hz-a")
|
|
withEnv(t, "NACOS_USERNAME", "nacos")
|
|
withEnv(t, "NACOS_PASSWORD", "nacos")
|
|
withEnv(t, "NACOS_IP", "172.16.10.9")
|
|
withEnv(t, "NACOS_PORT", "19090")
|
|
withEnv(t, "PORT", "9999")
|
|
|
|
cfg, err := loadConfig()
|
|
if err != nil {
|
|
t.Fatalf("loadConfig() error = %v", err)
|
|
}
|
|
|
|
if !cfg.EnableNacosRegister {
|
|
t.Fatalf("expected EnableNacosRegister true")
|
|
}
|
|
if cfg.NacosServerAddr != "10.0.0.10:8848" {
|
|
t.Fatalf("unexpected Nacos server %s", cfg.NacosServerAddr)
|
|
}
|
|
if cfg.NacosNamespace != "prod-ns" {
|
|
t.Fatalf("unexpected NacosNamespace %q", cfg.NacosNamespace)
|
|
}
|
|
if cfg.NacosGroup != "editor" {
|
|
t.Fatalf("unexpected NacosGroup %q", cfg.NacosGroup)
|
|
}
|
|
if cfg.NacosServiceName != "editor-lsp" {
|
|
t.Fatalf("unexpected NacosServiceName %q", cfg.NacosServiceName)
|
|
}
|
|
if cfg.NacosClusterName != "hz-a" {
|
|
t.Fatalf("unexpected NacosClusterName %q", cfg.NacosClusterName)
|
|
}
|
|
if cfg.NacosUsername != "nacos" || cfg.NacosPassword != "nacos" {
|
|
t.Fatalf("unexpected nacos auth")
|
|
}
|
|
if cfg.NacosRegisterIP != "172.16.10.9" || cfg.NacosRegisterPort != 19090 {
|
|
t.Fatalf("unexpected register endpoint %s:%d", cfg.NacosRegisterIP, cfg.NacosRegisterPort)
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigLoggingDefaults(t *testing.T) {
|
|
withEnv(t, "APP_NAME", "")
|
|
withEnv(t, "APP_ENV", "")
|
|
withEnv(t, "LOG_PATH", "")
|
|
withEnv(t, "LOG_LEVEL", "")
|
|
withEnv(t, "LOG_MAX_SIZE_MB", "")
|
|
withEnv(t, "LOG_MAX_BACKUPS", "")
|
|
withEnv(t, "LOG_MAX_AGE_DAYS", "")
|
|
withEnv(t, "LOG_COMPRESS", "")
|
|
withEnv(t, "LOG_CONSOLE_ENABLED", "")
|
|
|
|
cfg, err := loadConfig()
|
|
if err != nil {
|
|
t.Fatalf("loadConfig() error = %v", err)
|
|
}
|
|
|
|
if cfg.AppName != "lsp-gateway" {
|
|
t.Fatalf("expected default AppName lsp-gateway, got %q", cfg.AppName)
|
|
}
|
|
if cfg.AppEnv != "dev" {
|
|
t.Fatalf("expected default AppEnv dev, got %q", cfg.AppEnv)
|
|
}
|
|
expectedPath := filepath.Join(".", "logs", "lsp-gateway")
|
|
if cfg.LogPath != expectedPath {
|
|
t.Fatalf("expected default LogPath %q, got %q", expectedPath, cfg.LogPath)
|
|
}
|
|
if cfg.LogLevel != "info" {
|
|
t.Fatalf("expected default LogLevel info, got %q", cfg.LogLevel)
|
|
}
|
|
if cfg.LogMaxSizeMB != 100 {
|
|
t.Fatalf("expected default LogMaxSizeMB 100, got %d", cfg.LogMaxSizeMB)
|
|
}
|
|
if cfg.LogMaxBackups != 31 {
|
|
t.Fatalf("expected default LogMaxBackups 31, got %d", cfg.LogMaxBackups)
|
|
}
|
|
if cfg.LogMaxAgeDays != 31 {
|
|
t.Fatalf("expected default LogMaxAgeDays 31, got %d", cfg.LogMaxAgeDays)
|
|
}
|
|
if !cfg.LogCompress {
|
|
t.Fatalf("expected default LogCompress true")
|
|
}
|
|
if !cfg.LogConsoleEnabled {
|
|
t.Fatalf("expected default LogConsoleEnabled true")
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigLoggingFromEnv(t *testing.T) {
|
|
withEnv(t, "APP_NAME", "editor-bff")
|
|
withEnv(t, "APP_ENV", "prod")
|
|
withEnv(t, "LOG_PATH", "/data/logs/editor-bff")
|
|
withEnv(t, "LOG_LEVEL", "warn")
|
|
withEnv(t, "LOG_MAX_SIZE_MB", "200")
|
|
withEnv(t, "LOG_MAX_BACKUPS", "50")
|
|
withEnv(t, "LOG_MAX_AGE_DAYS", "15")
|
|
withEnv(t, "LOG_COMPRESS", "false")
|
|
withEnv(t, "LOG_CONSOLE_ENABLED", "false")
|
|
|
|
cfg, err := loadConfig()
|
|
if err != nil {
|
|
t.Fatalf("loadConfig() error = %v", err)
|
|
}
|
|
|
|
if cfg.AppName != "editor-bff" {
|
|
t.Fatalf("unexpected AppName %q", cfg.AppName)
|
|
}
|
|
if cfg.AppEnv != "prod" {
|
|
t.Fatalf("unexpected AppEnv %q", cfg.AppEnv)
|
|
}
|
|
if cfg.LogPath != "/data/logs/editor-bff" {
|
|
t.Fatalf("unexpected LogPath %q", cfg.LogPath)
|
|
}
|
|
if cfg.LogLevel != "warn" {
|
|
t.Fatalf("unexpected LogLevel %q", cfg.LogLevel)
|
|
}
|
|
if cfg.LogMaxSizeMB != 200 {
|
|
t.Fatalf("unexpected LogMaxSizeMB %d", cfg.LogMaxSizeMB)
|
|
}
|
|
if cfg.LogMaxBackups != 50 {
|
|
t.Fatalf("unexpected LogMaxBackups %d", cfg.LogMaxBackups)
|
|
}
|
|
if cfg.LogMaxAgeDays != 15 {
|
|
t.Fatalf("unexpected LogMaxAgeDays %d", cfg.LogMaxAgeDays)
|
|
}
|
|
if cfg.LogCompress {
|
|
t.Fatalf("expected LogCompress false")
|
|
}
|
|
if cfg.LogConsoleEnabled {
|
|
t.Fatalf("expected LogConsoleEnabled false")
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigProxyDefaults(t *testing.T) {
|
|
withEnv(t, "TRUSTED_PROXIES", "")
|
|
withEnv(t, "REMOTE_IP_HEADERS", "")
|
|
withEnv(t, "FORWARDED_BY_CLIENT_IP", "")
|
|
|
|
cfg, err := loadConfig()
|
|
if err != nil {
|
|
t.Fatalf("loadConfig() error = %v", err)
|
|
}
|
|
|
|
if len(cfg.TrustedProxies) != 0 {
|
|
t.Fatalf("expected default TrustedProxies empty, got %v", cfg.TrustedProxies)
|
|
}
|
|
if len(cfg.RemoteIPHeaders) != 2 || cfg.RemoteIPHeaders[0] != "X-Forwarded-For" || cfg.RemoteIPHeaders[1] != "X-Real-IP" {
|
|
t.Fatalf("unexpected default RemoteIPHeaders %v", cfg.RemoteIPHeaders)
|
|
}
|
|
if !cfg.ForwardedByClientIP {
|
|
t.Fatalf("expected default ForwardedByClientIP true")
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigProxyFromEnv(t *testing.T) {
|
|
withEnv(t, "TRUSTED_PROXIES", "10.0.0.0/8,172.16.0.0/12")
|
|
withEnv(t, "REMOTE_IP_HEADERS", "X-Forwarded-For,X-Real-IP")
|
|
withEnv(t, "FORWARDED_BY_CLIENT_IP", "false")
|
|
|
|
cfg, err := loadConfig()
|
|
if err != nil {
|
|
t.Fatalf("loadConfig() error = %v", err)
|
|
}
|
|
|
|
if len(cfg.TrustedProxies) != 2 || cfg.TrustedProxies[0] != "10.0.0.0/8" || cfg.TrustedProxies[1] != "172.16.0.0/12" {
|
|
t.Fatalf("unexpected TrustedProxies %v", cfg.TrustedProxies)
|
|
}
|
|
if len(cfg.RemoteIPHeaders) != 2 || cfg.RemoteIPHeaders[0] != "X-Forwarded-For" || cfg.RemoteIPHeaders[1] != "X-Real-IP" {
|
|
t.Fatalf("unexpected RemoteIPHeaders %v", cfg.RemoteIPHeaders)
|
|
}
|
|
if cfg.ForwardedByClientIP {
|
|
t.Fatalf("expected ForwardedByClientIP false")
|
|
}
|
|
}
|
|
|
|
func withEnv(t *testing.T, key, value string) {
|
|
t.Helper()
|
|
old, existed := os.LookupEnv(key)
|
|
if value == "" {
|
|
_ = os.Unsetenv(key)
|
|
} else {
|
|
_ = os.Setenv(key, value)
|
|
}
|
|
t.Cleanup(func() {
|
|
if !existed {
|
|
_ = os.Unsetenv(key)
|
|
return
|
|
}
|
|
_ = os.Setenv(key, old)
|
|
})
|
|
}
|