change 增加配置文件
parent
33626eeda5
commit
6833477292
|
@ -1,3 +1,4 @@
|
||||||
.git
|
.git
|
||||||
.gitignore
|
.gitignore
|
||||||
README.md
|
README.md
|
||||||
|
config/config.yaml
|
|
@ -1 +1,2 @@
|
||||||
.idea
|
.idea
|
||||||
|
config/config.yaml
|
|
@ -13,14 +13,6 @@ RUN go mod download
|
||||||
# 复制源代码
|
# 复制源代码
|
||||||
COPY . .
|
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 .
|
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"
|
"fmt"
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"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 {
|
type Wlzb struct {
|
||||||
ID int `json:"id" gorm:"primaryKey"`
|
ID int `json:"id" gorm:"primaryKey"`
|
||||||
PostID string `json:"post_id" gorm:"primaryKey"`
|
PostID string `json:"post_id" gorm:"primaryKey"`
|
||||||
|
@ -23,26 +38,53 @@ func (Wlzb) TableName() string {
|
||||||
return "wlzb"
|
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() {
|
func main() {
|
||||||
|
// 读取配置文件
|
||||||
|
config, err := LoadConfig("config/config.yaml")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error loading config file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
// 自定义 CORS 中间件配置
|
// 自定义 CORS 中间件配置
|
||||||
config := cors.DefaultConfig()
|
CORSconfig := cors.DefaultConfig()
|
||||||
config.AllowOrigins = []string{fmt.Sprintf("%s", os.Getenv("WEB_URL"))} // 允许的前端源
|
if config.Debug {
|
||||||
config.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}
|
// 允许所有来源
|
||||||
config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"}
|
CORSconfig.AllowAllOrigins = true
|
||||||
config.AllowCredentials = 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",
|
// 构建DSN
|
||||||
os.Getenv("DB_USER"),
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||||
os.Getenv("DB_PASSWORD"),
|
config.Database.User,
|
||||||
os.Getenv("DB_HOST"),
|
config.Database.Password,
|
||||||
os.Getenv("DB_PORT"),
|
config.Database.Host,
|
||||||
os.Getenv("DB_NAME"))
|
config.Database.Port,
|
||||||
|
config.Database.DBName,
|
||||||
|
)
|
||||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("failed to connect database")
|
panic("failed to connect database")
|
||||||
|
|
Loading…
Reference in New Issue