main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import (
  3. "encoding/base64"
  4. "eps/models/etc"
  5. "eps/models/statusMgr"
  6. "eps/models/userMgr"
  7. _ "eps/routers"
  8. "eps/tcp/bds"
  9. "eps/tcp/robot"
  10. "eps/tcp/zz"
  11. "github.com/astaxie/beego"
  12. "github.com/astaxie/beego/context"
  13. "github.com/astaxie/beego/orm"
  14. _ "github.com/mattn/go-sqlite3"
  15. "strings"
  16. "wb/cc"
  17. "wb/cfg"
  18. "wb/ctrl"
  19. "wb/ii"
  20. "wb/lg"
  21. "wb/modbus"
  22. "wb/usr"
  23. )
  24. func initLog() {
  25. dataPath := cfg.WbConfig.DataPath
  26. if beego.BConfig.RunMode == "prod" {
  27. lg.InitLog(lg.LevelInfo, dataPath+"/log")
  28. } else {
  29. lg.InitLog(lg.LevelDebug, dataPath+"/log")
  30. }
  31. }
  32. func initDb() {
  33. orm.RegisterDriver("sqlite", orm.DRSqlite)
  34. dbPath := cfg.WbConfig.DataPath + "/db/main.db"
  35. beego.Info("initDb file:", dbPath)
  36. orm.RegisterDataBase("default", "sqlite3", dbPath)
  37. postitionPath := cfg.WbConfig.DataPath + "/db/position.db"
  38. beego.Info("initDb file:", postitionPath)
  39. orm.RegisterDataBase(etc.DbNamePosition, "sqlite3", postitionPath)
  40. historyPath := cfg.WbConfig.DataPath + "/db/gsstatus.db"
  41. beego.Info("initDb file:", historyPath)
  42. orm.RegisterDataBase(etc.DbNameGsStatus, "sqlite3", historyPath)
  43. historyPath = cfg.WbConfig.DataPath + "/db/wpstatus.db"
  44. beego.Info("initDb file:", historyPath)
  45. orm.RegisterDataBase(etc.DbNameWpStatus, "sqlite3", historyPath)
  46. historyPath = cfg.WbConfig.DataPath + "/db/epsstatus.db"
  47. beego.Info("initDb file:", historyPath)
  48. orm.RegisterDataBase(etc.DbNameEpsStatus, "sqlite3", historyPath)
  49. orm.Debug = true
  50. }
  51. var needLogin bool
  52. var FilterUser = func(ctx *context.Context) {
  53. if v, _ := beego.AppConfig.Bool("demoEnable"); v && ctx.Input.URL() == "/demo" {
  54. return
  55. }
  56. if v, _ := beego.AppConfig.Bool("publicEnable"); v && ctx.Input.URL() == "/public" {
  57. return
  58. }
  59. switch ctx.Input.URL() {
  60. case "/login", "/logout", "/ViewStatus":
  61. return
  62. }
  63. _, ok := ctx.Input.Session(cc.SessionUser).(usr.Usr)
  64. if !ok && ctx.Request.RequestURI != "/login" {
  65. if needLogin == false {
  66. u := userMgr.User{}
  67. u.Role = "role_sysuser"
  68. ctx.Input.CruSession.Set(cc.SessionUser, u)
  69. return
  70. }
  71. lg.Debug("FilterUser need login: ", ctx.Input.URL(), ctx.Input.URI())
  72. if strings.EqualFold("POST", ctx.Request.Method) {
  73. ctx.WriteString("need_login")
  74. return
  75. }
  76. redirect := ctx.Input.URI()
  77. redirectB64 := base64.URLEncoding.EncodeToString([]byte(redirect))
  78. ctx.Redirect(302, "/login?redirect="+redirectB64)
  79. }
  80. }
  81. func main() {
  82. cfg.InitConfig()
  83. iot.InitThings("conf/iot/things.xml")
  84. initLog()
  85. initDb()
  86. ii.LoadItemInfo("conf/item/fields")
  87. modbus.LoadModelInfo("conf/mdbs")
  88. zz.LoadModelInfo("conf/zz/zz.xml")
  89. needLogin = beego.AppConfig.DefaultBool("needLogin", true)
  90. lg.Info("Init need login:", needLogin)
  91. beego.InsertFilter("/*", beego.BeforeRouter, FilterUser)
  92. ctrl.LoadSvcConfig("conf/svc")
  93. statusMgr.InitStatusMgr()
  94. //go mdbs.ServerRun()
  95. go bds.ServerRun()
  96. go zz.ServerRun()
  97. go robot.ServerRun()
  98. //for i := int16(1);i<99;i++{
  99. // time.Sleep(2 * time.Second)
  100. // print(i)
  101. // go client.BdsClient(i)
  102. //}
  103. beego.Run()
  104. }