70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package config
|
||
|
||
import (
|
||
"os"
|
||
|
||
"gopkg.in/yaml.v3"
|
||
)
|
||
|
||
// Config 应用配置结构体
|
||
type Config struct {
|
||
Server ServerConfig `yaml:"server"`
|
||
OSS OSSConfig `yaml:"oss"`
|
||
MySQL MySQLConfig `yaml:"mysql"`
|
||
Upload UploadConfig `yaml:"upload"`
|
||
FileUpload FileUploadConfig `yaml:"fileupload"`
|
||
}
|
||
|
||
// ServerConfig 服务器配置
|
||
type ServerConfig struct {
|
||
Port string `yaml:"port"`
|
||
AllowedOrigins []string `yaml:"allowed_origins"`
|
||
}
|
||
|
||
// OSSConfig 阿里云OSS配置
|
||
type OSSConfig struct {
|
||
Endpoint string `yaml:"endpoint"`
|
||
BucketName string `yaml:"bucket_name"`
|
||
AccessKeyID string `yaml:"access_key_id"`
|
||
AccessKeySecret string `yaml:"access_key_secret"`
|
||
ObjectPrefix string `yaml:"object_prefix"`
|
||
}
|
||
|
||
// MySQLConfig MySQL数据库配置
|
||
type MySQLConfig struct {
|
||
Host string `yaml:"host"`
|
||
Port int `yaml:"port"`
|
||
Username string `yaml:"username"`
|
||
Password string `yaml:"password"`
|
||
Database string `yaml:"database"`
|
||
Charset string `yaml:"charset"`
|
||
}
|
||
|
||
// UploadConfig 上传配置
|
||
type UploadConfig struct {
|
||
AllowImageTypes string `yaml:"allow_image_types"`
|
||
MaxFileSize int64 `yaml:"max_file_size"`
|
||
}
|
||
|
||
type FileUploadConfig struct {
|
||
MaxFileSize int64 `yaml:"max_file_size"` // 最大文件大小(字节)
|
||
AllowFileTypes string `yaml:"allow_file_types"` // 允许的文件类型,用逗号分隔
|
||
ChunkSize int64 `yaml:"chunk_size"` // 分片大小(5MB)
|
||
TempDir string `yaml:"temp_dir"` // 分片临时存储目录
|
||
}
|
||
|
||
// LoadConfig 从文件加载配置
|
||
func LoadConfig(filename string) (*Config, error) {
|
||
data, err := os.ReadFile(filename)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
var config Config
|
||
if err := yaml.Unmarshal(data, &config); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &config, nil
|
||
}
|