-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
56 lines (45 loc) · 2.13 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
package main
import (
"time"
routes2 "todoBackend/app/api/todo/routes"
"todoBackend/app/api/user/routes"
"todoBackend/app/config" // 导入应用配置
"todoBackend/utils/db"
_ "todoBackend/docs" // 导入Swagger文档
"github.com/gin-contrib/cors" // 导入CORS中间件
"github.com/gin-gonic/gin" // 导入Gin框架
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
// @title SimpleTodo后端接口文档
// @version 1.0
// @description SimpleTodo后端接口文档
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8090
// @BasePath /api/v1
// @securityDefinitions.basic BasicAuth
// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api/
func main() {
db.CreateTable() // 创建数据库表
gin.SetMode(gin.ReleaseMode)
router := gin.Default() // 创建Gin框架实例
router.Use(cors.New(cors.Config{ // 使用CORS中间件
AllowAllOrigins: true, // 允许所有来源
AllowMethods: []string{"GET", "PUT", "POST", "DELETE", "PATCH", "OPTIONS"}, // 允许的HTTP方法
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization"}, // 允许的请求头
ExposeHeaders: []string{"Content-Length"}, // 公开的响应头
AllowCredentials: true, // 允许发送凭据
MaxAge: 12 * time.Hour, // 预检请求的有效期
}))
// 设置路由
routes.SetupUserRoutes(router) // 设置用户相关路由
routes2.SetupTodoRoutes(router) // 设置待办事项相关路由
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
router.Run(":" + config.Cfg.Server.AppPort) // 运行服务并指定端口
}