Files
web_ylsa/api_iris/main.go
2025-07-11 16:54:11 +08:00

120 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/accesslog"
"github.com/kataras/iris/v12/websocket"
"github.com/sirupsen/logrus"
"main/config"
"main/crontab"
"main/database"
"main/jwtSet"
"main/service"
"main/service/admin"
"main/service/ws"
"main/utils"
"os"
"path"
"time"
)
func main() {
logrus.SetFormatter(&nested.Formatter{HideKeys: true, TimestampFormat: time.RFC3339})
//配置文件初始化
err := config.InitConfig(utils.GetEnvDefault("version", "dev"))
if err != nil {
logrus.Errorln("配置文件初始化失败:", err)
return
}
logrus.Infoln("配置文件初始化完成")
//日志初始化
ac := makeAccessLog()
//应用初始化
app := iris.New()
//应用ac-log
app.UseRouter(ac.Handler)
//错误处理
app.OnAnyErrorCode(handler)
//数据库连接初始化
if !database.GetInstance().InitDataPool() {
return
}
logrus.Infoln("数据库连接初始化完成")
if e := database.UpdateDbStruct(); e != nil {
logrus.Errorln("数据库初始化失败:", e)
return
}
//初始化用户信息,系统配置信息,菜单列表,本地IP地址信息
utils.UpdateUserInfo()
utils.UpdateSysSettings()
utils.UpdateMenuList()
admin.UpdateLocalIpList()
logrus.Infoln("数据库结构初始化完成")
jwtSet.Init()
logrus.Infoln("jwt认证初始化完成")
crontab.InitAllCron()
id, err := crontab.MyCron.AddFunc("0 0 0 * * *", func() {
utils.UpdateUserInfo()
jwtSet.GetJwtKeys()
})
if err != nil {
logrus.Errorln("cron任务添加失败", err)
return
}
crontab.UpdateCronDb(int(id), "每天初始化jwt密钥", true)
if utils.GetEnvDefault("version", "dev") == "prd" {
//id, err = crontab.MyCron.AddFunc("0 10 * * * *", func() {
// crontab.UpdateSysInfo("")
//})
//crontab.UpdateCronDb(int(id), "每小时10分同步历史系统信息记录", true)
id, err = crontab.MyCron.AddFunc("0 30 0 * * *", func() {
crontab.CornSaveNginxLog()
})
crontab.UpdateCronDb(int(id), "每天00:30同步nginx日志", true)
}
logrus.Infoln("定时任务初始化完成")
//接口组
app.PartyFunc("/api", service.Apis)
app.PartyFunc("/admin", service.Admin)
app.PartyFunc("/test", service.Test)
logrus.Infoln("接口组配置完成")
//启动websocket
wss := ws.SetupWebsocket()
app.Get("/ws", websocket.Handler(wss))
logrus.Infoln("websocket配置完成")
//应用启动
logrus.Infoln("应用启动")
err = app.Run(iris.Addr(":8080"))
if err != nil {
return
}
}
// 生成acLog实例
func makeAccessLog() *accesslog.AccessLog {
cPath, _ := os.Getwd()
ac := accesslog.File(path.Join(cPath, config.Config.Logs.Log, fmt.Sprintf("access-%s.log", time.Now().Format("2006-01-02"))))
ac.AddOutput(os.Stdout)
ac.IP = true
ac.Delim = ' '
ac.ResponseBody = false
return ac
}
// 接口错误处理
func handler(ctx iris.Context) {
if ctx.GetErr() != nil {
err := ctx.JSON(utils.FormatRes(ctx.GetStatusCode(), ctx.GetErr().Error(), nil))
if err != nil {
return
}
} else {
err := ctx.JSON(utils.FormatRes(ctx.GetStatusCode(), "", nil))
if err != nil {
return
}
}
}