main.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package main
  2. import (
  3. "cicv-data-closedloop/amd64/score_server/handler"
  4. "cicv-data-closedloop/amd64/score_server/infra"
  5. "cicv-data-closedloop/common/config/c_db"
  6. "cicv-data-closedloop/common/config/c_log"
  7. "cicv-data-closedloop/common/gin/middleware"
  8. _ "embed"
  9. "github.com/gin-gonic/gin"
  10. _ "gopkg.in/yaml.v3"
  11. "os"
  12. )
  13. func init() {
  14. infra.InitApplication()
  15. c_log.InitLog(
  16. infra.ApplicationYaml.Log.Dir,
  17. infra.ApplicationYaml.Log.Prefix,
  18. )
  19. c_db.InitSqlxMysql(
  20. infra.ApplicationYaml.Mysql.Username,
  21. infra.ApplicationYaml.Mysql.Password,
  22. infra.ApplicationYaml.Mysql.Ip,
  23. infra.ApplicationYaml.Mysql.Port,
  24. infra.ApplicationYaml.Mysql.Dbname,
  25. infra.ApplicationYaml.Mysql.Charset,
  26. )
  27. c_db.InitSqlFilesMap(infra.ApplicationYaml.Mysql.SqlfileDir)
  28. }
  29. func main() {
  30. c_log.GlobalLogger.Info("配置文件为:", infra.ApplicationYaml)
  31. // 创建 gin 实例
  32. router := gin.Default()
  33. // 使用中间件
  34. router.Use(middleware.Cors()) // 解决cors
  35. router.Use(middleware.LogRequest()) // 请求日志打印
  36. router.Use(middleware.ValidateHeaders(infra.ApplicationYaml.Web.WhiteList, infra.ApplicationYaml.Web.Token)) // 全局请求头校验
  37. // 通过路由组设置全局前缀
  38. projectPrefix := router.Group(infra.ApplicationYaml.Web.RoutePrefix)
  39. {
  40. examPrefix := projectPrefix.Group("/exam")
  41. {
  42. examPrefix.POST("/tick", handler.Tick) // 考试开始
  43. go handler.ExamEndTicker() // 考试结束
  44. examPrefix.POST("/display", handler.Display) // 云控展示线上评分
  45. examPrefix.POST("/page", handler.Page) // 分页查询
  46. examPrefix.POST("/report", handler.Report) // pdf下载
  47. }
  48. }
  49. {
  50. monitorPrefix := router.Group("/web_server/monitor") // todo web_server会改成score_server,后续修改
  51. {
  52. monitorPrefix.POST("/insert", handler.SaveDeviceMonitor)
  53. }
  54. }
  55. // 端口
  56. err := router.Run(":" + infra.ApplicationYaml.Web.Port)
  57. if err != nil {
  58. c_log.GlobalLogger.Error("程序崩溃,监听端口 "+infra.ApplicationYaml.Web.Port+" 失败:", err)
  59. os.Exit(-1)
  60. }
  61. }