修改后端初始化
This commit is contained in:
61
server/config/config.go
Normal file
61
server/config/config.go
Normal file
@@ -0,0 +1,61 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user