feat: 初始化项目路由
This commit is contained in:
1 parent
4cec3f131a
commit
92a9e20934
11 files changed
+91
-83
No files matched your search
@@ -0,0 +1,7 @@
|
||||
package api
|
||||
|
||||
type Api struct {
|
||||
}
|
||||
|
||||
// App 统一导出全局 API 实例
|
||||
var App = new(Api)
|
||||
@@ -0,0 +1 @@
|
||||
package conf
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package handler
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func RegisterRoutes(r *gin.Engine) {
|
||||
// @RouterInjectPoint
|
||||
}
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package router
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func AuthRouter(g *gin.RouterGroup) {
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user