feat: 合并Gin_Vue
This commit is contained in:
119
src/app.go
Normal file
119
src/app.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package src
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ems.agt/src/framework/config"
|
||||
"ems.agt/src/framework/errorcatch"
|
||||
"ems.agt/src/framework/middleware"
|
||||
"ems.agt/src/framework/middleware/security"
|
||||
"ems.agt/src/modules/common"
|
||||
"ems.agt/src/modules/monitor"
|
||||
"ems.agt/src/modules/system"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 路由函数句柄,交给由 http.ListenAndServe(addr, router)
|
||||
func AppEngine() *gin.Engine {
|
||||
app := initAppEngine()
|
||||
|
||||
// 初始全局默认
|
||||
initDefeat(app)
|
||||
|
||||
// 初始模块路由
|
||||
initModulesRoute(app)
|
||||
|
||||
// 读取服务配置
|
||||
app.ForwardedByClientIP = config.Get("server.proxy").(bool)
|
||||
addr := fmt.Sprintf(":%d", config.Get("server.port").(int))
|
||||
|
||||
// 启动服务
|
||||
fmt.Printf("\nopen http://localhost%s \n\n", addr)
|
||||
return app
|
||||
}
|
||||
|
||||
// 运行服务程序 main.go
|
||||
//
|
||||
// func main() {
|
||||
// src.ConfigurationInit()
|
||||
// if err := src.RunServer(); err != nil {
|
||||
// src.ConfigurationClose()
|
||||
// }
|
||||
// }
|
||||
func RunServer() error {
|
||||
app := initAppEngine()
|
||||
|
||||
// 初始全局默认
|
||||
initDefeat(app)
|
||||
|
||||
// 初始模块路由
|
||||
initModulesRoute(app)
|
||||
|
||||
// 读取服务配置
|
||||
app.ForwardedByClientIP = config.Get("server.proxy").(bool)
|
||||
addr := fmt.Sprintf(":%d", config.Get("server.port").(int))
|
||||
|
||||
// 启动服务
|
||||
fmt.Printf("\nopen http://localhost%s \n\n", addr)
|
||||
return app.Run(addr)
|
||||
}
|
||||
|
||||
// 初始应用引擎
|
||||
func initAppEngine() *gin.Engine {
|
||||
var app *gin.Engine
|
||||
|
||||
// 禁止控制台日志输出的颜色
|
||||
gin.DisableConsoleColor()
|
||||
|
||||
// 根据运行环境注册引擎
|
||||
if config.Env() == "prod" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
app = gin.New()
|
||||
app.Use(gin.Recovery())
|
||||
} else {
|
||||
app = gin.Default()
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
// 初始全局默认
|
||||
func initDefeat(app *gin.Engine) {
|
||||
// 全局中间件
|
||||
app.Use(errorcatch.ErrorCatch(), middleware.Report(), middleware.Cors(), security.Security())
|
||||
|
||||
// 静态目录-静态资源
|
||||
if v := config.Get("staticFile.default"); v != nil {
|
||||
fsMap := v.(map[string]any)
|
||||
prefix, dir := fsMap["prefix"], fsMap["dir"]
|
||||
if prefix != nil && dir != nil {
|
||||
app.StaticFS(prefix.(string), gin.Dir(dir.(string), true))
|
||||
}
|
||||
}
|
||||
|
||||
// 静态目录-上传资源
|
||||
if v := config.Get("staticFile.upload"); v != nil {
|
||||
fsMap := v.(map[string]any)
|
||||
prefix, dir := fsMap["prefix"], fsMap["dir"]
|
||||
if prefix != nil && dir != nil {
|
||||
app.StaticFS(prefix.(string), gin.Dir(dir.(string), true))
|
||||
}
|
||||
}
|
||||
|
||||
// 路由未找到时
|
||||
app.NoRoute(func(c *gin.Context) {
|
||||
c.JSON(404, gin.H{
|
||||
"code": 404,
|
||||
"msg": fmt.Sprintf("%s Not Found", c.Request.RequestURI),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 初始模块路由
|
||||
func initModulesRoute(app *gin.Engine) {
|
||||
|
||||
common.Setup(app)
|
||||
monitor.Setup(app)
|
||||
system.Setup(app)
|
||||
}
|
||||
Reference in New Issue
Block a user