1 Star 0 Fork 22

arphone2020/go-iot

forked from 有理想的鸭子/go-iot 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 2.34 KB
一键复制 编辑 原始数据 按行查看 历史
有理想的鸭子 提交于 2023-02-12 12:16 . rename package
package main
import (
"fmt"
_ "go-iot/pkg/api"
"go-iot/pkg/core"
"go-iot/pkg/core/es"
"go-iot/pkg/core/redis"
"go-iot/pkg/models"
_ "go-iot/pkg/network/clients/registry"
_ "go-iot/pkg/network/servers/registry"
_ "go-iot/pkg/notify/registry"
"net/http"
"runtime"
"strings"
"github.com/beego/beego/v2/core/config"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/server/web/context"
)
func main() {
configLog()
setDefaultConfig()
getConfigString("db.url", func(s string) {
models.DefaultDbConfig.Url = s
})
models.InitDb()
web.BConfig.RecoverFunc = defaultRecoverPanic
web.ErrorHandler("404", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(404)
})
web.Run()
}
func defaultRecoverPanic(ctx *context.Context, cfg *web.Config) {
if err := recover(); err != nil {
if err == web.ErrAbort {
return
}
logs.Error("the request url is ", ctx.Input.URL())
var stack string
for i := 1; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
logs.Error(fmt.Sprintf("%s:%d", file, line))
stack = stack + fmt.Sprintf("%s:%d\n", file, line)
}
if ctx.Output.Status == 0 {
ctx.Output.Status = 500
}
ctx.Output.JSON(models.JsonRespError(fmt.Errorf("%v", err)), false, false)
}
}
func setDefaultConfig() {
{
getConfigString("device.manager.id", func(s string) {
core.DefaultManagerId = s
})
logs.Info("default device manager: ", core.DefaultManagerId)
}
es.Config(getConfigString)
redis.Config(getConfigString)
}
func configLog() {
logs.Async()
var logLevel string
getConfigString("logs.level", func(s string) {
logLevel = s
})
level := 7
switch logLevel {
case "info":
level = logs.LevelInfo
case "warn":
level = logs.LevelWarn
case "error":
level = logs.LevelError
}
var filename string = "go-iot.log"
getConfigString("logs.filename", func(s string) {
filename = "go-iot.log"
})
logs.GetBeeLogger().SetLevel(level)
err := logs.SetLogger(logs.AdapterFile, fmt.Sprintf(`{"filename":"%s","level":%d,"maxlines":0,
"maxsize":0,"daily":true,"maxdays":10,"color":false}`, filename, level))
if err != nil {
panic(err)
}
}
func getConfigString(key string, callback func(string)) {
data, err := config.String(key)
if err != nil {
panic(err)
}
val := strings.TrimSpace(data)
if len(val) > 0 {
callback(val)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/arphone2020/go-iot.git
[email protected]:arphone2020/go-iot.git
arphone2020
go-iot
go-iot
master

搜索帮助