2021-09-26 02:35:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-10-15 03:43:41 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-06-08 10:19:45 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/model/notify"
|
2021-10-29 10:37:27 +00:00
|
|
|
"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"
|
2022-06-29 03:09:58 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/encryption"
|
2022-06-08 10:19:45 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
|
2022-06-29 03:09:58 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/random"
|
2021-09-27 06:17:36 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/route"
|
|
|
|
"github.com/IceWhaleTech/CasaOS/service"
|
2022-08-15 03:37:21 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/types"
|
2021-10-29 10:37:27 +00:00
|
|
|
|
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")
|
2022-06-29 03:09:58 +00:00
|
|
|
var resetUser = flag.Bool("ru", false, "reset user")
|
|
|
|
var user = flag.String("user", "", "user name")
|
2022-08-15 03:37:21 +00:00
|
|
|
var version = flag.Bool("v", false, "show version")
|
2021-10-22 08:49:09 +00:00
|
|
|
|
2021-09-26 02:35:02 +00:00
|
|
|
func init() {
|
|
|
|
flag.Parse()
|
2022-08-15 03:37:21 +00:00
|
|
|
if *version {
|
|
|
|
fmt.Println("v" + types.CURRENTVERSION)
|
|
|
|
return
|
|
|
|
}
|
2021-09-26 02:35:02 +00:00
|
|
|
config.InitSetup(*configFlag)
|
2021-10-15 03:43:41 +00:00
|
|
|
config.UpdateSetup()
|
2022-08-15 03:37:21 +00:00
|
|
|
|
2022-06-08 10:19:45 +00:00
|
|
|
loger.LogInit()
|
2022-05-05 05:46:55 +00:00
|
|
|
if len(*dbFlag) == 0 {
|
2022-06-29 03:09:58 +00:00
|
|
|
*dbFlag = config.AppInfo.DBPath + "/db"
|
2022-04-06 04:10:51 +00:00
|
|
|
}
|
2022-08-15 03:37:21 +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),
|
2022-08-15 03:37:21 +00:00
|
|
|
|
2022-06-08 10:19:45 +00:00
|
|
|
service.MyService = service.NewService(sqliteDB)
|
2022-08-15 03:37:21 +00:00
|
|
|
|
2021-10-29 10:37:27 +00:00
|
|
|
service.Cache = cache.Init()
|
2022-03-16 07:41:14 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
service.GetToken()
|
2022-08-15 03:37:21 +00:00
|
|
|
|
2022-05-05 05:46:55 +00:00
|
|
|
service.NewVersionApp = make(map[string]string)
|
2021-11-09 10:57:50 +00:00
|
|
|
route.InitFunction()
|
2022-02-18 11:06:03 +00:00
|
|
|
|
2022-05-13 10:12:26 +00:00
|
|
|
// go service.LoopFriend()
|
|
|
|
// go service.MyService.App().CheckNewImage()
|
2022-03-18 03:13:07 +00:00
|
|
|
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 08:49:09 +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
|
2021-10-22 08:49:09 +00:00
|
|
|
// @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() {
|
2022-06-08 10:19:45 +00:00
|
|
|
service.NotifyMsg = make(chan notify.Message, 10)
|
2022-08-15 03:37:21 +00:00
|
|
|
if *version {
|
|
|
|
return
|
|
|
|
}
|
2022-06-29 03:09:58 +00:00
|
|
|
if *resetUser {
|
|
|
|
if user == nil || len(*user) == 0 {
|
|
|
|
fmt.Println("user is empty")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userData := service.MyService.User().GetUserAllInfoByName(*user)
|
|
|
|
if userData.Id == 0 {
|
|
|
|
fmt.Println("user not exist")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
password := random.RandomString(6, false)
|
|
|
|
userData.Password = encryption.GetMD5ByStr(password)
|
|
|
|
service.MyService.User().UpdateUserPassword(userData)
|
|
|
|
fmt.Println("User reset successful")
|
2022-07-22 03:02:11 +00:00
|
|
|
fmt.Println("UserName:" + userData.Username)
|
2022-06-29 03:09:58 +00:00
|
|
|
fmt.Println("Password:" + password)
|
|
|
|
return
|
|
|
|
}
|
2022-06-08 10:19:45 +00:00
|
|
|
go route.SocketInit(service.NotifyMsg)
|
2022-08-15 03:37:21 +00:00
|
|
|
go route.MonitoryUSB()
|
2021-09-26 02:35:02 +00:00
|
|
|
//model.Setup()
|
|
|
|
//gredis.Setup()
|
2021-10-22 08:49:09 +00:00
|
|
|
r := route.InitRouter()
|
2021-09-29 11:49:34 +00:00
|
|
|
//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-04-06 04:10:51 +00:00
|
|
|
|
2022-07-22 03:02:11 +00:00
|
|
|
err := cron2.AddFunc("0/5 * * * * *", func() {
|
2022-06-08 10:19:45 +00:00
|
|
|
if service.ClientCount > 0 {
|
2022-06-29 03:09:58 +00:00
|
|
|
//route.SendNetINfoBySocket()
|
|
|
|
//route.SendCPUBySocket()
|
|
|
|
//route.SendMemBySocket()
|
2022-06-10 05:33:53 +00:00
|
|
|
// route.SendDiskBySocket()
|
2022-06-29 03:09:58 +00:00
|
|
|
//route.SendUSBBySocket()
|
2022-06-10 05:33:53 +00:00
|
|
|
route.SendAllHardwareStatusBySocket()
|
2022-06-08 10:19:45 +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()
|
|
|
|
|
2022-06-08 10:19:45 +00:00
|
|
|
// if err := r.Run(fmt.Sprintf(":%v", config.ServerInfo.HttpPort)); err != nil {
|
|
|
|
// fmt.Println("failed run app: ", err)
|
|
|
|
// }
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|