[feature] migrate to monorepo
This commit is contained in:
commit
05ddc1f783
267 changed files with 75165 additions and 0 deletions
44
backend/pkg/config/config.go
Normal file
44
backend/pkg/config/config.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Database struct {
|
||||
Driver string `yaml:"driver"`
|
||||
DSN string `yaml:"dsn"`
|
||||
} `yaml:"database"`
|
||||
|
||||
Server struct {
|
||||
Port int `yaml:"port"`
|
||||
Host string `yaml:"host"`
|
||||
} `yaml:"server"`
|
||||
|
||||
JWT struct {
|
||||
Secret string `yaml:"secret"`
|
||||
Expiration string `yaml:"expiration"`
|
||||
} `yaml:"jwt"`
|
||||
|
||||
Logging struct {
|
||||
Level string `yaml:"level"`
|
||||
Format string `yaml:"format"`
|
||||
} `yaml:"logging"`
|
||||
}
|
||||
|
||||
// Load loads the configuration from the specified file
|
||||
func Load(path string) (*Config, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
77
backend/pkg/config/config_test.go
Normal file
77
backend/pkg/config/config_test.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
// Create a temporary test config file
|
||||
testConfig := `
|
||||
database:
|
||||
driver: postgres
|
||||
dsn: postgres://user:pass@localhost:5432/db
|
||||
server:
|
||||
port: 8080
|
||||
host: localhost
|
||||
jwt:
|
||||
secret: test-secret
|
||||
expiration: 24h
|
||||
logging:
|
||||
level: debug
|
||||
format: console
|
||||
`
|
||||
tmpDir := t.TempDir()
|
||||
configPath := filepath.Join(tmpDir, "config.yaml")
|
||||
if err := os.WriteFile(configPath, []byte(testConfig), 0644); err != nil {
|
||||
t.Fatalf("Failed to create test config file: %v", err)
|
||||
}
|
||||
|
||||
// Test successful config loading
|
||||
cfg, err := Load(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load config: %v", err)
|
||||
}
|
||||
|
||||
// Verify loaded values
|
||||
tests := []struct {
|
||||
name string
|
||||
got interface{}
|
||||
expected interface{}
|
||||
}{
|
||||
{"database.driver", cfg.Database.Driver, "postgres"},
|
||||
{"database.dsn", cfg.Database.DSN, "postgres://user:pass@localhost:5432/db"},
|
||||
{"server.port", cfg.Server.Port, 8080},
|
||||
{"server.host", cfg.Server.Host, "localhost"},
|
||||
{"jwt.secret", cfg.JWT.Secret, "test-secret"},
|
||||
{"jwt.expiration", cfg.JWT.Expiration, "24h"},
|
||||
{"logging.level", cfg.Logging.Level, "debug"},
|
||||
{"logging.format", cfg.Logging.Format, "console"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.got != tt.expected {
|
||||
t.Errorf("Config %s = %v, want %v", tt.name, tt.got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Test loading non-existent file
|
||||
_, err = Load("non-existent.yaml")
|
||||
if err == nil {
|
||||
t.Error("Expected error when loading non-existent file, got nil")
|
||||
}
|
||||
|
||||
// Test loading invalid YAML
|
||||
invalidPath := filepath.Join(tmpDir, "invalid.yaml")
|
||||
if err := os.WriteFile(invalidPath, []byte("invalid: yaml: content"), 0644); err != nil {
|
||||
t.Fatalf("Failed to create invalid config file: %v", err)
|
||||
}
|
||||
|
||||
_, err = Load(invalidPath)
|
||||
if err == nil {
|
||||
t.Error("Expected error when loading invalid YAML, got nil")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue