-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
59 lines (48 loc) · 1.13 KB
/
main.go
File metadata and controls
59 lines (48 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"os"
"server/internal/models"
"server/internal/routes"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main() {
// 加载环境变量
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file")
}
// 从环境变量中获取数据库连接信息
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"),
)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
// 自动迁移表结构
db.AutoMigrate(&models.User{}, &models.Article{})
// 初始化路由
router := gin.Default()
// 设置代理
// router.SetTrustedProxies([]string{"127.0.0.1"})
// 设置路由
routes.SetupRoutes(router, db)
port := os.Getenv("PORT")
if port == "" {
port = "7777"
}
// 启动服务
if err := router.Run(":" + port); err != nil {
panic("failed to start server")
} else {
fmt.Println("Server started on :" + port)
}
}