mirror of
https://gitee.com/luojinyi/likeadmin_go.git
synced 2025-09-27 12:42:27 +08:00

修复代码生成工具在 mysql 8.0 下无法显示问题 参照likeadmin-php 将前端用户管理完善 编辑和余额调整 运行server/tool/gormgen.go 即可生成dao 和模型文件 生成配置在gormgen.go中
100 lines
2.7 KiB
Go
100 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
adminRouters "likeadmin/admin/routers"
|
|
admin "likeadmin/admin/service"
|
|
"likeadmin/config"
|
|
"likeadmin/core"
|
|
"likeadmin/core/response"
|
|
"likeadmin/dao"
|
|
frontendRouters "likeadmin/frontend/routers"
|
|
frontend "likeadmin/frontend/service"
|
|
genRouters "likeadmin/generator/routers"
|
|
gen "likeadmin/generator/service"
|
|
"likeadmin/middleware"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// initDI 初始化DI
|
|
func initDI() {
|
|
regFunctions := admin.InitFunctions
|
|
regFunctions = append(regFunctions, gen.InitFunctions...)
|
|
regFunctions = append(regFunctions, frontend.InitFunctions...)
|
|
regFunctions = append(regFunctions, core.GetDB)
|
|
regFunctions = append(regFunctions, func() *dao.Query {
|
|
dao.SetDefault(core.GetDB())
|
|
return dao.Q
|
|
})
|
|
for i := 0; i < len(regFunctions); i++ {
|
|
if err := core.ProvideForDI(regFunctions[i]); err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// initRouter 初始化router
|
|
func initRouter() *gin.Engine {
|
|
// 初始化gin
|
|
gin.SetMode(config.Config.GinMode)
|
|
router := gin.New()
|
|
// 设置静态路径
|
|
router.Static(config.Config.PublicPrefix, config.Config.UploadDirectory)
|
|
router.Static(config.Config.StaticPath, config.Config.StaticDirectory)
|
|
// 设置中间件
|
|
router.Use(gin.Logger(), middleware.Cors(), middleware.ErrorRecover())
|
|
// 演示模式
|
|
if config.Config.DisallowModify {
|
|
router.Use(middleware.ShowMode())
|
|
}
|
|
// 特殊异常处理
|
|
router.NoMethod(response.NoMethod)
|
|
router.NoRoute(response.NoRoute)
|
|
// 注册路由
|
|
group := router.Group("/api")
|
|
//core.RegisterGroup(group, routers.CommonGroup, middleware.TokenAuth())
|
|
//core.RegisterGroup(group, routers.MonitorGroup, middleware.TokenAuth())
|
|
//core.RegisterGroup(group, routers.SettingGroup, middleware.TokenAuth())
|
|
//core.RegisterGroup(group, routers.SystemGroup, middleware.TokenAuth())
|
|
|
|
routers := adminRouters.InitRouters[:]
|
|
routers = append(routers, genRouters.InitRouters...)
|
|
routers = append(routers, frontendRouters.InitRouters...)
|
|
for i := 0; i < len(routers); i++ {
|
|
core.RegisterGroup(group, routers[i])
|
|
}
|
|
return router
|
|
}
|
|
|
|
// initServer 初始化server
|
|
func initServer(router *gin.Engine) *http.Server {
|
|
return &http.Server{
|
|
Addr: ":" + strconv.Itoa(config.Config.ServerPort),
|
|
Handler: router,
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
MaxHeaderBytes: 1 << 20,
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
// 刷新日志缓冲
|
|
defer core.Logger.Sync()
|
|
// 程序结束前关闭数据库连接
|
|
if core.GetDB() != nil {
|
|
db, _ := core.GetDB().DB()
|
|
defer db.Close()
|
|
}
|
|
// 初始化DI
|
|
initDI()
|
|
// 初始化router
|
|
router := initRouter()
|
|
// 初始化server
|
|
s := initServer(router)
|
|
// 运行服务
|
|
log.Fatalln(s.ListenAndServe().Error())
|
|
}
|