main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "path/filepath"
  8. "time"
  9. "github.com/IceWhaleTech/CasaOS-Gateway/common"
  10. "github.com/IceWhaleTech/CasaOS/model/notify"
  11. "github.com/IceWhaleTech/CasaOS/pkg/cache"
  12. "github.com/IceWhaleTech/CasaOS/pkg/config"
  13. "github.com/IceWhaleTech/CasaOS/pkg/sqlite"
  14. "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
  15. "github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
  16. "github.com/IceWhaleTech/CasaOS/route"
  17. "github.com/IceWhaleTech/CasaOS/service"
  18. "github.com/IceWhaleTech/CasaOS/types"
  19. "go.uber.org/zap"
  20. "github.com/robfig/cron"
  21. "gorm.io/gorm"
  22. )
  23. const LOCALHOST = "127.0.0.1"
  24. var sqliteDB *gorm.DB
  25. var (
  26. configFlag = flag.String("c", "", "config address")
  27. dbFlag = flag.String("db", "", "db path")
  28. versionFlag = flag.Bool("v", false, "version")
  29. )
  30. func init() {
  31. flag.Parse()
  32. if *versionFlag {
  33. fmt.Println("v" + types.CURRENTVERSION)
  34. return
  35. }
  36. config.InitSetup(*configFlag)
  37. loger.LogInit()
  38. if len(*dbFlag) == 0 {
  39. *dbFlag = config.AppInfo.DBPath + "/db"
  40. }
  41. sqliteDB = sqlite.GetDb(*dbFlag)
  42. // gredis.GetRedisConn(config.RedisInfo),
  43. service.MyService = service.NewService(sqliteDB, config.CommonInfo.RuntimePath)
  44. service.Cache = cache.Init()
  45. service.GetToken()
  46. service.NewVersionApp = make(map[string]string)
  47. route.InitFunction()
  48. // go service.LoopFriend()
  49. // go service.MyService.App().CheckNewImage()
  50. }
  51. // @title casaOS API
  52. // @version 1.0.0
  53. // @contact.name lauren.pan
  54. // @contact.url https://www.zimaboard.com
  55. // @contact.email lauren.pan@icewhale.org
  56. // @description casaOS v1版本api
  57. // @host 192.168.2.217:8089
  58. // @securityDefinitions.apikey ApiKeyAuth
  59. // @in header
  60. // @name Authorization
  61. // @BasePath /v1
  62. func main() {
  63. service.NotifyMsg = make(chan notify.Message, 10)
  64. if *versionFlag {
  65. return
  66. }
  67. go route.SocketInit(service.NotifyMsg)
  68. // model.Setup()
  69. // gredis.Setup()
  70. r := route.InitRouter()
  71. // service.SyncTask(sqliteDB)
  72. cron2 := cron.New()
  73. // every day execution
  74. err := cron2.AddFunc("0/5 * * * * *", func() {
  75. if service.ClientCount > 0 {
  76. // route.SendNetINfoBySocket()
  77. // route.SendCPUBySocket()
  78. // route.SendMemBySocket()
  79. // route.SendDiskBySocket()
  80. // route.SendUSBBySocket()
  81. route.SendAllHardwareStatusBySocket()
  82. }
  83. })
  84. if err != nil {
  85. fmt.Println(err)
  86. }
  87. cron2.Start()
  88. defer cron2.Stop()
  89. listener, err := net.Listen("tcp", net.JoinHostPort(LOCALHOST, "0"))
  90. if err != nil {
  91. panic(err)
  92. }
  93. routers := []string{"sys", "apps", "container", "app-categories", "port", "file", "folder", "batch", "image", "samba", "notify"}
  94. for _, v := range routers {
  95. err = service.MyService.Gateway().CreateRoute(&common.Route{
  96. Path: "/v1/" + v,
  97. Target: "http://" + listener.Addr().String(),
  98. })
  99. if err != nil {
  100. fmt.Println("err", err)
  101. panic(err)
  102. }
  103. }
  104. go func() {
  105. time.Sleep(time.Second * 2)
  106. // v0.3.6
  107. if config.ServerInfo.HttpPort != "" {
  108. changePort := common.ChangePortRequest{}
  109. changePort.Port = config.ServerInfo.HttpPort
  110. err := service.MyService.Gateway().ChangePort(&changePort)
  111. if err == nil {
  112. config.Cfg.Section("server").Key("HttpPort").SetValue("")
  113. config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  114. }
  115. }
  116. }()
  117. // s := &http.Server{
  118. // Addr: listener.Addr().String(), //fmt.Sprintf(":%v", config.ServerInfo.HttpPort),
  119. // Handler: r,
  120. // ReadTimeout: 60 * time.Second,
  121. // WriteTimeout: 60 * time.Second,
  122. // MaxHeaderBytes: 1 << 20,
  123. // }
  124. // s.ListenAndServe()
  125. urlFilePath := filepath.Join(config.CommonInfo.RuntimePath, "casaos.url")
  126. err = file.CreateFileAndWriteContent(urlFilePath, "http://"+listener.Addr().String())
  127. if err != nil {
  128. loger.Error("Management service is listening...",
  129. zap.Any("address", listener.Addr().String()),
  130. zap.Any("filepath", urlFilePath),
  131. )
  132. }
  133. err = http.Serve(listener, r)
  134. if err != nil {
  135. panic(err)
  136. }
  137. }