package main import ( _ "Cold_Api/Nats" "Cold_Api/conf" "Cold_Api/controllers" _ "Cold_Api/initialize" "Cold_Api/models/Device" "Cold_Api/models/Product" "Cold_Api/models/Warning" "Cold_Api/routers" _ "Cold_Api/routers" "fmt" "github.com/astaxie/beego/logs" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" beego "github.com/beego/beego/v2/server/web" "github.com/beego/beego/v2/server/web/context" "github.com/beego/beego/v2/server/web/filter/cors" _ "github.com/go-sql-driver/mysql" "runtime" "strconv" ) func init() { //lib.WritePid() fmt.Println(runtime.GOOS) orm.RegisterDriver("mysql", orm.DRMySQL) //orm.RegisterDataBase("default", "mysql", "zdxq:7e5853d9178edfcc@tcp(47.108.133.234:3306)/zdxq?charset=utf8",100,200) orm.RegisterDataBase("default", "mysql", conf.MysqlServer_Username+":"+conf.MysqlServer_Password+"@tcp("+conf.MysqlServer_UrlPort+")/"+conf.MysqlServer_Database+"?charset=utf8mb4&loc=Local&parseTime=True", conf.MysqlServer_MaxIdleConnections, conf.MysqlServer_MaxOpenConnections) orm.RunSyncdb("default", false, false) // 创建数据库 println(conf.MysqlServer_Username + ":" + conf.MysqlServer_Password + "@tcp(" + conf.MysqlServer_UrlPort + ")/" + conf.MysqlServer_Database + "?charset=utf8mb4&loc=Local&parseTime=True") orm2.Debug = conf.MysqlServer_Debug // sql文件输出到日志 if orm2.Debug == true { sqllog := logs.NewLogger() //sqllog.SetLogger(logs.AdapterFile, `{"filename":"logs/sql/sql.log","level":7,"maxlines":10000,"maxsize":1024000,"daily":true,"maxdays":10,"color":true}`) sqllog.SetLogger(logs.AdapterFile, `{"filename":"logs/sql/sql.log"}`) if conf.RunMode == "dev" { sqllog.SetLogger(logs.AdapterConsole) } orm2.DebugLog = orm2.NewLog(sqllog) } } func main() { HTTPPort_int, _ := strconv.Atoi(conf.HTTPPort) beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{ // 允许访问所有源 AllowAllOrigins: true, // 可选参数"GET", "POST", "PUT", "DELETE", "OPTIONS" (*为所有) AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, // 指的是允许的Header的种类 AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"}, // 公开的HTTP标头列表 ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Access-Control-Allow-Headers", "Content-Type"}, // 如果设置,则允许共享身份验证凭据,例如cookie AllowCredentials: true, })) //过滤器,拦截所有请求 beego.InsertFilter("/*", beego.BeforeRouter, routers.RBACFilter) beego.BConfig.AppName = conf.AppName // 项目名 beego.BConfig.ServerName = conf.AppName + conf.HTTPPort //server 名称 beego.BConfig.RunMode = conf.RunMode // 应用的运行模式 beego.BConfig.Listen.HTTPPort = HTTPPort_int //监听端口 本地:8518 线上:8528 beego.BConfig.WebConfig.AutoRender = false beego.BConfig.RecoverPanic = true beego.BConfig.RecoverFunc = RecoverPanic go controllers.DeviceRealTime() // 实时 数据 //go controllers.Update_DeviceSensor_Parameter() // 更新传感器表-传感器参数t_sp go Warning.Read_WarningType_All_Maps() // 初始化报警类型 go Product.Read_ProductType_All_Map() // 初始化产品类型 go Device.Read_DeviceSensorTypeAll_Map() // 初始化传感器类型 if conf.MqttServer_id == "mqttjxit" { go controllers.Cron_WarningRate() // 只在一台机启动定时任务 } fmt.Println("======= beego.Run ======") beego.Run() } func RecoverPanic(ctx *context.Context, config *beego.Config) { if err := recover(); err != nil { ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", ctx.Request.Header.Get("Origin")) 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 = append(stack, fmt.Sprintln(fmt.Sprintf("%s:%d", file, line))) } //显示错误 data := map[string]interface{}{ "AppError": fmt.Sprintf("%v", err), "Code": 500, "Msg": "请稍后重试!", } if conf.RunMode == "dev" { data["RequestMethod"] = ctx.Input.Method() data["RequestURL"] = ctx.Input.URI() data["RemoteAddr"] = ctx.Input.IP() data["Stack"] = stack data["GoVersion"] = runtime.Version() } _ = ctx.Output.JSON(data, true, true) //if ctx.Output.Status != 0 { // ctx.ResponseWriter.WriteHeader(ctx.Output.Status) //} else { // ctx.ResponseWriter.WriteHeader(500) //} } }