main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. } `yaml:"mysql"`
  32. }
  33. var (
  34. //go:embed application.yaml
  35. applicationYamlBytes []byte
  36. ApplicationYaml ApplicationYamlStruct
  37. )
  38. func init() {
  39. // 解析YAML内容
  40. _ = yaml.Unmarshal(applicationYamlBytes, &ApplicationYaml)
  41. c_log.InitLog(
  42. ApplicationYaml.Log.Dir,
  43. ApplicationYaml.Log.Prefix,
  44. )
  45. c_db.InitSqlxMysql(
  46. ApplicationYaml.Mysql.Ip,
  47. ApplicationYaml.Mysql.Port,
  48. ApplicationYaml.Mysql.Username,
  49. ApplicationYaml.Mysql.Password,
  50. ApplicationYaml.Mysql.Dbname,
  51. ApplicationYaml.Mysql.Charset,
  52. )
  53. }
  54. func main() {
  55. c_log.GlobalLogger.Info("配置文件为:", ApplicationYaml)
  56. // 创建 gin 实例
  57. router := gin.Default()
  58. // 使用中间件
  59. //router.Use(cors.Default())
  60. router.Use(middleware.Cors())
  61. router.Use(middleware.LogRequest()) // 请求日志打印
  62. router.Use(middleware.ValidateHeaders(ApplicationYaml.Web.WhiteList, ApplicationYaml.Web.Token)) // 全局请求头校验
  63. // 通过路由组设置全局前缀
  64. projectPrefix := router.Group(ApplicationYaml.Web.RoutePrefix)
  65. reportPrefix := projectPrefix.Group("/report")
  66. reportPrefix.POST("/page", handler.Page) // 分页查询
  67. //reportPrefix.POST("/pdf", handler.Hello) // pdf下载
  68. monitorPrefix := projectPrefix.Group("/monitor")
  69. monitorPrefix.POST("/save", handler.SaveMonitor)
  70. // 端口
  71. err := router.Run(":" + ApplicationYaml.Web.Port)
  72. if err != nil {
  73. c_log.GlobalLogger.Error("程序崩溃,监听端口 "+ApplicationYaml.Web.Port+" 失败:", err)
  74. os.Exit(-1)
  75. }
  76. }