修改后端初始化

This commit is contained in:
2025-10-04 20:48:50 +08:00
parent 2dea178fde
commit 2afbafa192
13 changed files with 289 additions and 146 deletions

61
server/config/config.go Normal file
View 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
}

22
server/config/config.yaml Normal file
View File

@@ -0,0 +1,22 @@
server:
port: 8080
allowed_origins: ["http://localhost:5173"]
oss:
endpoint: "https://oss-cn-shenzhen.aliyuncs.com"
bucket_name: "hldr-data"
access_key_id: "LTAI5tMTSfbaaAtYBS2gFkf9"
access_key_secret: "dHfxH4XziDO0C8ppN94kbLUREAw1Dc"
object_prefix: "article/"
mysql:
host: "localhost"
port: 3306
username: "root"
password: "your_mysql_password"
database: "article_db"
charset: "utf8mb4"
upload:
allow_image_types: "image/jpeg,image/png,image/gif,image/webp"
max_file_size: 5242880 # 5MB (5 * 1024 * 1024)