-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (81 loc) · 2.06 KB
/
main.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"log"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"github.com/FlowRays/WhisperTrail/controller"
"github.com/FlowRays/WhisperTrail/model"
)
func main() {
err := readConfig()
if err != nil {
log.Fatalf("Failed to read config file: %v", err)
}
// 连接数据库
db, err := connectDB()
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
// 执行数据库迁移
err = db.AutoMigrate(&model.Landmark{}, &model.User{}, &model.Rate{})
if err != nil {
log.Fatal(err)
}
// 创建数据库实例
database := model.Database{
DB: db,
}
r := gin.Default()
registerAPI(r, &database)
r.Run(":8080")
}
func readConfig() error {
// 设置配置文件的名称和路径
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
// 读取配置文件
err := viper.ReadInConfig()
return err
}
func connectDB() (*gorm.DB, error) {
dbHost := viper.GetString("database.host")
dbPort := viper.GetString("database.port")
dbUsername := viper.GetString("database.username")
dbPassword := viper.GetString("database.password")
dbName := viper.GetString("database.dbname")
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", dbUsername, dbPassword, dbHost, dbPort, dbName)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
}
return db, nil
}
func registerAPI(r *gin.Engine, db *model.Database) {
r.POST("/api/register", func(c *gin.Context) {
controller.UserRegister(c, db)
})
r.POST("/api/login", func(c *gin.Context) {
controller.UserLogin(c, db)
})
auth := r.Group("/api")
auth.Use(controller.AuthMiddleware())
auth.POST("/upload", func(c *gin.Context) {
controller.CreateLandmark(c, db)
})
auth.GET("/get", func(c *gin.Context) {
controller.GetLandmark(c, db)
})
auth.GET("/image/:id", func(c *gin.Context) {
controller.GetImage(c, db)
})
r.POST("/api/rate", func(c *gin.Context) {
controller.CreateRate(c, db)
})
r.GET("/api/rate/:id", func(c *gin.Context) {
controller.GetRate(c, db)
})
}