初始化项目文件
This commit is contained in:
81
api_file/main.go
Normal file
81
api_file/main.go
Normal file
@ -0,0 +1,81 @@
|
||||
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/sirupsen/logrus"
|
||||
"main/config"
|
||||
"main/database"
|
||||
"main/service"
|
||||
"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("数据库已连接")
|
||||
//初始化用户信息,系统配置信息,菜单列表
|
||||
utils.UpdateUserInfo()
|
||||
utils.UpdateSysSettings()
|
||||
logrus.Infoln("数据库结构初始化完成")
|
||||
//jwtSet.Init()
|
||||
//logrus.Infoln("jwt认证初始化完成")
|
||||
//接口组
|
||||
app.PartyFunc("/api", service.Apis)
|
||||
logrus.Infoln("接口组配置完成")
|
||||
//应用启动
|
||||
logrus.Infoln("应用启动")
|
||||
err = app.Run(iris.Addr(":8081"))
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user