main.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package main
  2. /*
  3. 1. 算法清理可以用仿真云平台的调度系统
  4. */
  5. import (
  6. "cicv-data-closedloop/amd64/dispatch_server/package/handler"
  7. "cicv-data-closedloop/amd64/dispatch_server/package/infra"
  8. commonHandler "cicv-data-closedloop/common/gin/handler"
  9. "cicv-data-closedloop/common/util"
  10. _ "embed"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "gopkg.in/yaml.v3"
  14. "os"
  15. )
  16. type ApplicationYamlStruct struct {
  17. ApplicationName string `yaml:"application-name"`
  18. Web WebStruct `yaml:"web"`
  19. Log LogStruct `yaml:"log"`
  20. Redis RedisStruct `yaml:"redis"`
  21. Oss OssStruct `yaml:"oss"`
  22. GpuNodeList []GpuNode `yaml:"gpu_node_list"`
  23. }
  24. type WebStruct struct {
  25. Port string `yaml:"port"`
  26. RoutePrefix string `yaml:"route-prefix"`
  27. Token string `yaml:"token"`
  28. WhiteList []string `yaml:"white-list"`
  29. }
  30. type LogStruct struct {
  31. Dir string `yaml:"dir"`
  32. Prefix string `yaml:"prefix"`
  33. }
  34. type RedisStruct struct {
  35. Addr string `yaml:"addr"`
  36. Password string `yaml:"password"`
  37. Db int `yaml:"db"`
  38. }
  39. type OssStruct struct {
  40. IsUseCname bool `yaml:"is-use-cname"`
  41. Endpoint string `yaml:"endpoint"`
  42. AccessKeyId string `yaml:"access-key-id"`
  43. AccessKeySecret string `yaml:"access-key-secret"`
  44. BucketName string `yaml:"bucket-name"`
  45. }
  46. type GpuNode struct {
  47. Hostname string `yaml:"hostname"`
  48. Ip string `yaml:"ip"`
  49. Parallelism string `yaml:"parallelism"`
  50. }
  51. var (
  52. //go:embed application.yaml
  53. applicationYamlBytes []byte
  54. ApplicationYaml ApplicationYamlStruct
  55. )
  56. func init() {
  57. // 1 解析YAML内容
  58. _ = yaml.Unmarshal(applicationYamlBytes, &ApplicationYaml)
  59. fmt.Println("加载配置文件内容为:", ApplicationYaml)
  60. // 2 初始化 日志
  61. infra.InitLog(ApplicationYaml.Log.Dir, ApplicationYaml.Log.Prefix)
  62. // 3 初始化 阿里云oss 客户端
  63. infra.InitOss(
  64. ApplicationYaml.Oss.IsUseCname,
  65. ApplicationYaml.Oss.Endpoint,
  66. ApplicationYaml.Oss.AccessKeyId,
  67. ApplicationYaml.Oss.AccessKeySecret,
  68. ApplicationYaml.Oss.BucketName,
  69. )
  70. // 4 初始化 Redis 客户端
  71. infra.InitRedisClient(
  72. ApplicationYaml.Redis.Addr,
  73. ApplicationYaml.Redis.Password,
  74. ApplicationYaml.Redis.Db,
  75. )
  76. }
  77. func main() {
  78. router := gin.Default()
  79. router.Use(commonHandler.ValidateHeaders())
  80. api := router.Group(ApplicationYaml.Web.RoutePrefix)
  81. api.POST("/start-project", handler.StartProject)
  82. err := router.Run(":" + ApplicationYaml.Web.Port)
  83. if err != nil {
  84. infra.GlobalLogger.Error("程序崩溃,监听端口 " + util.ToString(ApplicationYaml.Web.Port) + " 失败。")
  85. os.Exit(-1)
  86. }
  87. }