62 lines
1.4 KiB
Go
62 lines
1.4 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"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// 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"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// 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
|
||
|
|
}
|