change 增加配置文件
parent
33626eeda5
commit
6833477292
|
@ -1,3 +1,4 @@
|
|||
.git
|
||||
.gitignore
|
||||
README.md
|
||||
config/config.yaml
|
|
@ -1 +1,2 @@
|
|||
.idea
|
||||
config/config.yaml
|
|
@ -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 .
|
||||
|
||||
|
|
|
@ -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
|
68
main.go
68
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")
|
||||
|
|
Loading…
Reference in New Issue