package main

import (
	"eta/eta_mini_crm_ht/controllers"
	_ "eta/eta_mini_crm_ht/routers"
	"eta/eta_mini_crm_ht/utils"
	"fmt"
	"github.com/beego/beego/v2/core/logs"
	"github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/server/web/context"
	"runtime"
	"time"
)

func main() {
	if web.BConfig.RunMode == "dev" {
		web.BConfig.WebConfig.DirectoryIndex = true
		web.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
	}

	// 启动定时任务
	web.BConfig.WebConfig.AutoRender = false
	web.ErrorController(&controllers.ErrorController{})
	// 内存调整
	web.BConfig.MaxMemory = 1024 * 1024 * 128

	web.BConfig.RecoverFunc = Recover
	web.Run()
}

func Recover(ctx *context.Context, conf *web.Config) {
	if err := recover(); err != nil {
		if err == web.ErrAbort {
			return
		}
		if !web.BConfig.RecoverPanic {
			panic(err)
		}
		stack := ""
		msg := fmt.Sprintf("The request url is  %v", ctx.Input.URL())
		stack += msg + "</br>"
		logs.Critical(msg)
		msg = fmt.Sprintf("The request data is %v", string(ctx.Input.RequestBody))
		stack += msg + "</br>"
		logs.Critical(msg)
		msg = fmt.Sprintf("Handler crashed with error %v", err)
		stack += msg + "</br>"
		logs.Critical(msg)
		for i := 1; ; i++ {
			_, file, line, ok := runtime.Caller(i)
			if !ok {
				break
			}
			logs.Critical(fmt.Sprintf("%s:%d", file, line))
			stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d</br>", file, line))
		}
		utils.ApiLog.Notice(utils.APPNAME + "崩了" + time.Now().Format("2006-01-02 15:04:05") + "<br/>" + stack)
	}
	return
}
