CasaOS/main.go

118 lines
2.8 KiB
Go
Raw Normal View History

2021-09-26 02:35:02 +00:00
package main
import (
"flag"
"fmt"
"net/http"
"time"
"github.com/IceWhaleTech/CasaOS/pkg/cache"
2021-09-27 06:17:36 +00:00
"github.com/IceWhaleTech/CasaOS/pkg/config"
"github.com/IceWhaleTech/CasaOS/pkg/sqlite"
loger2 "github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
"github.com/IceWhaleTech/CasaOS/route"
"github.com/IceWhaleTech/CasaOS/service"
2022-05-05 05:46:55 +00:00
model2 "github.com/IceWhaleTech/CasaOS/service/model"
"github.com/IceWhaleTech/CasaOS/types"
2021-09-26 02:35:02 +00:00
"github.com/robfig/cron"
"gorm.io/gorm"
)
var sqliteDB *gorm.DB
var configFlag = flag.String("c", "", "config address")
2022-05-05 05:46:55 +00:00
var dbFlag = flag.String("db", "", "db path")
var showUserInfo = flag.Bool("show-user-info", false, "show user info")
2021-09-26 02:35:02 +00:00
func init() {
flag.Parse()
config.InitSetup(*configFlag)
config.UpdateSetup()
2021-09-26 02:35:02 +00:00
loger2.LogSetup()
2022-05-05 05:46:55 +00:00
if len(*dbFlag) == 0 {
*dbFlag = config.AppInfo.ProjectPath + "/db"
2022-04-06 04:10:51 +00:00
}
2022-05-05 05:46:55 +00:00
sqliteDB = sqlite.GetDb(*dbFlag)
2021-09-26 02:35:02 +00:00
//gredis.GetRedisConn(config.RedisInfo),
service.MyService = service.NewService(sqliteDB, loger2.NewOLoger())
service.Cache = cache.Init()
2022-03-16 07:41:14 +00:00
go service.UDPService()
2022-04-06 04:10:51 +00:00
fmt.Println("token", service.GetToken())
2022-03-16 07:41:14 +00:00
service.UDPAddressMap = make(map[string]string)
//go service.SocketConnect()
2022-04-06 04:10:51 +00:00
service.CancelList = make(map[string]string)
2022-05-05 05:46:55 +00:00
service.InternalInspection = make(map[string][]string)
service.NewVersionApp = make(map[string]string)
route.InitFunction()
2022-02-18 11:06:03 +00:00
2022-03-18 03:13:07 +00:00
go service.SendIPToServer()
go service.LoopFriend()
2022-05-05 05:46:55 +00:00
go service.MyService.App().CheckNewImage()
2022-03-18 03:13:07 +00:00
2021-09-26 02:35:02 +00:00
}
// @title casaOS API
2021-09-26 02:35:02 +00:00
// @version 1.0.0
// @contact.name lauren.pan
// @contact.url https://www.zimaboard.com
// @contact.email lauren.pan@icewhale.org
// @description casaOS v1版本api
// @host 192.168.2.217:8089
2021-09-26 02:35:02 +00:00
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @BasePath /v1
func main() {
if *showUserInfo {
fmt.Println("CasaOS User Info")
fmt.Println("UserName:" + config.UserInfo.UserName)
fmt.Println("Password:" + config.UserInfo.PWD)
return
}
2021-09-26 02:35:02 +00:00
//model.Setup()
//gredis.Setup()
r := route.InitRouter()
//service.SyncTask(sqliteDB)
2022-04-06 04:10:51 +00:00
cron2 := cron.New()
2022-02-17 10:43:25 +00:00
//every day execution
2022-03-18 03:13:07 +00:00
err := cron2.AddFunc("0 0/5 * * * *", func() {
2022-02-17 10:43:25 +00:00
//service.PushIpInfo(*&config.ServerInfo.Token)
2021-09-26 02:35:02 +00:00
//service.UpdataDDNSList(mysqldb)
//service.SyncTask(sqliteDB)
2022-04-06 04:10:51 +00:00
2022-03-18 03:13:07 +00:00
service.SendIPToServer()
2022-04-06 04:10:51 +00:00
2022-03-18 03:13:07 +00:00
service.LoopFriend()
2022-05-05 05:46:55 +00:00
service.MyService.App().CheckNewImage()
2021-09-26 02:35:02 +00:00
})
if err != nil {
fmt.Println(err)
}
2022-05-05 05:46:55 +00:00
err = cron2.AddFunc("0/1 * * * * *", func() {
notify := model2.AppNotify{}
notify.CustomId = ""
notify.Type = types.NOTIFY_TYPE_HEALTH_CHECK
2022-04-06 04:10:51 +00:00
2022-05-05 05:46:55 +00:00
go service.MyService.Notify().SendText(notify)
2021-09-26 02:35:02 +00:00
2022-05-05 05:46:55 +00:00
})
if err != nil {
fmt.Println(err)
}
2021-09-26 02:35:02 +00:00
cron2.Start()
defer cron2.Stop()
s := &http.Server{
Addr: fmt.Sprintf(":%v", config.ServerInfo.HttpPort),
Handler: r,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
MaxHeaderBytes: 1 << 20,
}
2022-02-17 10:43:25 +00:00
2021-09-26 02:35:02 +00:00
s.ListenAndServe()
}