From 6833477292b37200cc07f34610b7d48d3f394170 Mon Sep 17 00:00:00 2001 From: dugulp Date: Thu, 1 Aug 2024 02:08:35 +0800 Subject: [PATCH] =?UTF-8?q?change=20=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 1 + .gitignore | 1 + Dockerfile | 8 ------ config/config.txt | 12 +++++++++ main.go | 68 ++++++++++++++++++++++++++++++++++++++--------- 5 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 config/config.txt diff --git a/.dockerignore b/.dockerignore index f38de7b..7a63261 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,4 @@ .git .gitignore README.md +config/config.yaml \ No newline at end of file diff --git a/.gitignore b/.gitignore index 485dee6..992f0cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea +config/config.yaml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 7a7fe24..0f599e1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,14 +13,6 @@ RUN go mod download # 复制源代码 COPY . . -# mysql环境变量 -ENV DB_HOST=mysql-container -ENV DB_USER=root -ENV DB_PASSWORD=your_password -ENV DB_NAME=wlzb -ENV DB_PORT=3306 -ENV WEB_URL=http://127.0.0.1:5173 - # 构建应用 RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . diff --git a/config/config.txt b/config/config.txt new file mode 100644 index 0000000..2d1549e --- /dev/null +++ b/config/config.txt @@ -0,0 +1,12 @@ +# 将此txt文件复制并重命名为config.yaml +# debug模式 +debug: true +# 前端地址 +web: http://localhost:8080 +# 数据库配置 +database: + user: yourusername + password: yourpassword + host: 127.0.0.1 + port: 3306 + dbname: yourdatabase diff --git a/main.go b/main.go index 5003783..5dacf62 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,28 @@ import ( "fmt" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" + "gopkg.in/yaml.v3" "gorm.io/driver/mysql" "gorm.io/gorm" + "log" "os" "strconv" "time" ) +// Config 结构体用于读取YAML配置文件 +type Config struct { + Debug bool `yaml:"debug"` + Web string `yaml:"web"` + Database struct { + User string `yaml:"user"` + Password string `yaml:"password"` + Host string `yaml:"host"` + Port int `yaml:"port"` + DBName string `yaml:"dbname"` + } `yaml:"database"` +} + type Wlzb struct { ID int `json:"id" gorm:"primaryKey"` PostID string `json:"post_id" gorm:"primaryKey"` @@ -23,26 +38,53 @@ func (Wlzb) TableName() string { return "wlzb" } +// LoadConfig 读取YAML配置文件 +func LoadConfig(filePath string) (*Config, error) { + file, err := os.ReadFile(filePath) + if err != nil { + return nil, err + } + var config Config + err = yaml.Unmarshal(file, &config) + if err != nil { + return nil, err + } + return &config, nil +} + func main() { + // 读取配置文件 + config, err := LoadConfig("config/config.yaml") + if err != nil { + log.Fatalf("Error loading config file: %v", err) + } + r := gin.Default() // 自定义 CORS 中间件配置 - config := cors.DefaultConfig() - config.AllowOrigins = []string{fmt.Sprintf("%s", os.Getenv("WEB_URL"))} // 允许的前端源 - config.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"} - config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"} - config.AllowCredentials = true + CORSconfig := cors.DefaultConfig() + if config.Debug { + // 允许所有来源 + CORSconfig.AllowAllOrigins = true + } else { + CORSconfig.AllowOrigins = []string{fmt.Sprintf("%s", config.Web), + fmt.Sprintf("http://localhost:5197")} // 允许的前端源 + } + CORSconfig.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"} + CORSconfig.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"} + CORSconfig.AllowCredentials = true - r.Use(cors.New(config)) + r.Use(cors.New(CORSconfig)) // 数据库连接 - dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", - os.Getenv("DB_USER"), - os.Getenv("DB_PASSWORD"), - os.Getenv("DB_HOST"), - os.Getenv("DB_PORT"), - os.Getenv("DB_NAME")) - + // 构建DSN + dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", + config.Database.User, + config.Database.Password, + config.Database.Host, + config.Database.Port, + config.Database.DBName, + ) db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { panic("failed to connect database")