main.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package main
  2. import (
  3. "cicv-data-closedloop/amd64/web-server/handler"
  4. "cicv-data-closedloop/common/config/c_db"
  5. "cicv-data-closedloop/common/config/c_log"
  6. "cicv-data-closedloop/common/gin/middleware"
  7. _ "embed"
  8. "github.com/gin-gonic/gin"
  9. "gopkg.in/yaml.v3"
  10. "os"
  11. )
  12. type ApplicationYamlStruct struct {
  13. ApplicationName string `yaml:"application"`
  14. Web struct {
  15. Port string `yaml:"port"`
  16. RoutePrefix string `yaml:"route-prefix"`
  17. Token string `yaml:"token"`
  18. WhiteList []string `yaml:"white-list"`
  19. } `yaml:"web"`
  20. Log struct {
  21. Dir string `yaml:"dir"`
  22. Prefix string `yaml:"prefix"`
  23. } `yaml:"log"`
  24. Mysql struct {
  25. Ip string `yaml:"ip"`
  26. Port string `yaml:"port"`
  27. Username string `yaml:"username"`
  28. Password string `yaml:"password"`
  29. Dbname string `yaml:"dbname"`
  30. Charset string `yaml:"charset"`
  31. SqlfileDir string `yaml:"sqlfile-dir"`
  32. } `yaml:"mysql"`
  33. }
  34. var (
  35. //go:embed application.yaml
  36. applicationYamlBytes []byte
  37. ApplicationYaml ApplicationYamlStruct
  38. )
  39. func init() {
  40. // 解析YAML内容
  41. _ = yaml.Unmarshal(applicationYamlBytes, &ApplicationYaml)
  42. c_log.InitLog(
  43. ApplicationYaml.Log.Dir,
  44. ApplicationYaml.Log.Prefix,
  45. )
  46. c_db.InitSqlxMysql(
  47. ApplicationYaml.Mysql.Username,
  48. ApplicationYaml.Mysql.Password,
  49. ApplicationYaml.Mysql.Ip,
  50. ApplicationYaml.Mysql.Port,
  51. ApplicationYaml.Mysql.Dbname,
  52. ApplicationYaml.Mysql.Charset,
  53. )
  54. c_db.InitSqlFilesMap(ApplicationYaml.Mysql.SqlfileDir)
  55. }
  56. func main() {
  57. c_log.GlobalLogger.Info("配置文件为:", ApplicationYaml)
  58. // 创建 gin 实例
  59. router := gin.Default()
  60. // 使用中间件
  61. //router.Use(cors.Default())
  62. router.Use(middleware.Cors())
  63. router.Use(middleware.LogRequest()) // 请求日志打印
  64. router.Use(middleware.ValidateHeaders(ApplicationYaml.Web.WhiteList, ApplicationYaml.Web.Token)) // 全局请求头校验
  65. // 通过路由组设置全局前缀
  66. projectPrefix := router.Group(ApplicationYaml.Web.RoutePrefix)
  67. reportPrefix := projectPrefix.Group("/report")
  68. reportPrefix.POST("/page", handler.Page) // 分页查询
  69. //reportPrefix.POST("/pdf", handler.Hello) // pdf下载
  70. monitorPrefix := projectPrefix.Group("/monitor")
  71. monitorPrefix.POST("/insert", handler.SaveDeviceMonitor)
  72. // 端口
  73. err := router.Run(":" + ApplicationYaml.Web.Port)
  74. if err != nil {
  75. c_log.GlobalLogger.Error("程序崩溃,监听端口 "+ApplicationYaml.Web.Port+" 失败:", err)
  76. os.Exit(-1)
  77. }
  78. }