[feature] migrate to monorepo
Some checks failed
Build Backend / Build Docker Image (push) Successful in 3m33s
Test Backend / test (push) Failing after 31s

This commit is contained in:
CDN 2025-02-21 00:49:20 +08:00
commit 05ddc1f783
Signed by: CDN
GPG key ID: 0C656827F9F80080
267 changed files with 75165 additions and 0 deletions

View file

@ -0,0 +1,74 @@
package config
import (
"os"
"gopkg.in/yaml.v3"
"tss-rocks-be/internal/types"
)
type Config struct {
Database DatabaseConfig `yaml:"database"`
Server ServerConfig `yaml:"server"`
JWT JWTConfig `yaml:"jwt"`
Storage StorageConfig `yaml:"storage"`
Logging LoggingConfig `yaml:"logging"`
RateLimit types.RateLimitConfig `yaml:"rate_limit"`
AccessLog types.AccessLogConfig `yaml:"access_log"`
}
type DatabaseConfig struct {
Driver string `yaml:"driver"`
DSN string `yaml:"dsn"`
}
type ServerConfig struct {
Port int `yaml:"port"`
Host string `yaml:"host"`
}
type JWTConfig struct {
Secret string `yaml:"secret"`
Expiration string `yaml:"expiration"`
}
type LoggingConfig struct {
Level string `yaml:"level"`
Format string `yaml:"format"`
}
type StorageConfig struct {
Type string `yaml:"type"`
Local LocalStorage `yaml:"local"`
S3 S3Storage `yaml:"s3"`
Upload types.UploadConfig `yaml:"upload"`
}
type LocalStorage struct {
RootDir string `yaml:"root_dir"`
}
type S3Storage struct {
Region string `yaml:"region"`
Bucket string `yaml:"bucket"`
AccessKeyID string `yaml:"access_key_id"`
SecretAccessKey string `yaml:"secret_access_key"`
Endpoint string `yaml:"endpoint"`
CustomURL string `yaml:"custom_url"`
ProxyS3 bool `yaml:"proxy_s3"`
}
// Load loads configuration from a YAML 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
}

View file

@ -0,0 +1,85 @@
package config
import (
"os"
"path/filepath"
"testing"
)
func TestLoad(t *testing.T) {
// Create a temporary test config file
content := []byte(`
database:
driver: postgres
dsn: postgres://user:pass@localhost:5432/dbname
server:
port: 8080
host: localhost
jwt:
secret: test-secret
expiration: 24h
storage:
type: local
local:
root_dir: /tmp/storage
upload:
max_size: 10485760
logging:
level: info
format: json
`)
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "config.yaml")
if err := os.WriteFile(configPath, content, 0644); err != nil {
t.Fatalf("Failed to write test config: %v", err)
}
// Test loading config
cfg, err := Load(configPath)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
// Verify loaded values
tests := []struct {
name string
got interface{}
want interface{}
errorMsg string
}{
{"Database Driver", cfg.Database.Driver, "postgres", "incorrect database driver"},
{"Server Port", cfg.Server.Port, 8080, "incorrect server port"},
{"JWT Secret", cfg.JWT.Secret, "test-secret", "incorrect JWT secret"},
{"Storage Type", cfg.Storage.Type, "local", "incorrect storage type"},
{"Logging Level", cfg.Logging.Level, "info", "incorrect logging level"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.got != tt.want {
t.Errorf("%s = %v, want %v", tt.name, tt.got, tt.want)
}
})
}
}
func TestLoadError(t *testing.T) {
// Test loading non-existent file
_, err := Load("non-existent-file.yaml")
if err == nil {
t.Error("Load() error = nil, want error for non-existent file")
}
// Test loading invalid YAML
tmpDir := t.TempDir()
invalidPath := filepath.Join(tmpDir, "invalid.yaml")
if err := os.WriteFile(invalidPath, []byte("invalid: }{yaml"), 0644); err != nil {
t.Fatalf("Failed to write invalid config: %v", err)
}
_, err = Load(invalidPath)
if err == nil {
t.Error("Load() error = nil, want error for invalid YAML")
}
}