tss-rocks/backend/internal/config/config.go
CDN 3e6181e578
All checks were successful
Build Backend / Build Docker Image (push) Successful in 5m3s
[feature/backend] overall enhancement of image uploading
2025-02-23 04:42:48 +08:00

103 lines
2.4 KiB
Go

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"`
Auth AuthConfig `yaml:"auth"`
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 AuthConfig struct {
Registration struct {
Enabled bool `yaml:"enabled"`
Message string `yaml:"message"`
} `yaml:"registration"`
}
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 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"`
}
type UploadConfig struct {
Limits struct {
Image struct {
MaxSize int `yaml:"max_size"`
AllowedTypes []string `yaml:"allowed_types"`
} `yaml:"image"`
Video struct {
MaxSize int `yaml:"max_size"`
AllowedTypes []string `yaml:"allowed_types"`
} `yaml:"video"`
Audio struct {
MaxSize int `yaml:"max_size"`
AllowedTypes []string `yaml:"allowed_types"`
} `yaml:"audio"`
Document struct {
MaxSize int `yaml:"max_size"`
AllowedTypes []string `yaml:"allowed_types"`
} `yaml:"document"`
} `yaml:"limits"`
}
// 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
}