feat: 初始化项目路由

This commit is contained in:
unclesam committed 2026-07-07 21:44:41 +08:00
1 parent 4cec3f131a
commit 92a9e20934
11 files changed
+91 -83

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
package api
type Api struct {
}
// App 统一导出全局 API 实例
var App = new(Api)
+1
View File
@@ -0,0 +1 @@
package conf
+4 -2
View File
@@ -1,6 +1,8 @@
package conf
type ServerConfig struct {
Port int `mapstructure:"port" yaml:"port"`
Mode string `mapstructure:"mode" yaml:"mode"` // debug / release
Port int `mapstructure:"port" yaml:"port"`
Mode string `mapstructure:"mode" yaml:"mode"` // debug / release
AllowOrigins []string `mapstructure:"allow_origins" yaml:"allow_origins"`
Version string `mapstructure:"mode" yaml:"version"`
}
-7
View File
@@ -1,7 +0,0 @@
package handler
import "github.com/gin-gonic/gin"
func RegisterRoutes(r *gin.Engine) {
// @RouterInjectPoint
}
+20
View File
@@ -0,0 +1,20 @@
package middleware
import (
"EnGin/internal/global"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func CORS() gin.HandlerFunc {
return cors.New(cors.Config{
AllowOrigins: global.Config.Server.AllowOrigins,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "X-Source", "access_token", "refresh_token"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
})
}
+7
View File
@@ -0,0 +1,7 @@
package router
import "github.com/gin-gonic/gin"
func AuthRouter(g *gin.RouterGroup) {
}
+12
View File
@@ -0,0 +1,12 @@
package router
import (
"github.com/gin-gonic/gin"
)
// RegisterRoutes 💡 主路由入口,注册中间件并分发子路由
func RegisterRoutes(r *gin.Engine) {
// 分组并调用子路由
g := r.Group("api/v1")
AuthRouter(g)
}
+3 -2
View File
@@ -2,8 +2,8 @@ package server
import (
"EnGin/internal/global"
"EnGin/internal/handler"
"EnGin/internal/middleware"
"EnGin/internal/router"
"fmt"
"github.com/gin-gonic/gin"
@@ -14,11 +14,12 @@ func Run() {
gin.SetMode(global.Config.Server.Mode)
r := gin.New()
r.Use(gin.Recovery())
r.Use(middleware.CORS())
r.Use(middleware.TraceID())
r.Use(middleware.Logger())
// 注册路由
handler.RegisterRoutes(r)
router.RegisterRoutes(r)
port := global.Config.Server.Port
global.Log.Info(fmt.Sprintf("Server is running on port %d", port))
if err := r.Run(fmt.Sprintf(":%d", port)); err != nil {