-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (43 loc) · 1.27 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
package main
import (
"log"
"mdr/config"
"mdr/database"
"mdr/handlers"
"mdr/middleware"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func main() {
config.Init()
database.Init()
r := gin.Default()
// 添加 CORS 中间件
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:5173"}, // 允许前端开发服务器的域名
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"},
AllowCredentials: true, // 允许携带 cookie
}))
// 添加搜索路由
r.GET("/search/:query", handlers.HandleSearch)
api := r.Group("/api")
{
auth := api.Group("/auth")
{
auth.GET("/login", handlers.HandleCheckLogin)
auth.POST("/login", handlers.HandleLogin)
auth.POST("/register", handlers.HandleRegister)
auth.GET("/verify-email", handlers.HandleVerifyEmail)
auth.DELETE("/logout", middleware.AuthRequired(), handlers.HandleLogout)
}
user := api.Group("/user")
{
user.PATCH("/:id", middleware.AuthRequired(), handlers.UpdateUsername(database.DB))
}
api.POST("/feedback", middleware.AuthRequired(), handlers.HandleFeedback)
}
if err := r.Run(":8080"); err != nil {
log.Fatal("Failed to start server:", err)
}
}