CasaOS/main.go

149 lines
3.4 KiB
Go
Raw Normal View History

2021-09-26 02:35:02 +00:00
package main
import (
"flag"
"fmt"
"net"
"net/http"
"time"
"github.com/IceWhaleTech/CasaOS-Gateway/common"
"github.com/IceWhaleTech/CasaOS/model/notify"
"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"
"github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
2021-09-27 06:17:36 +00:00
"github.com/IceWhaleTech/CasaOS/route"
"github.com/IceWhaleTech/CasaOS/service"
"github.com/IceWhaleTech/CasaOS/types"
2021-09-26 02:35:02 +00:00
"github.com/robfig/cron"
"gorm.io/gorm"
)
const LOCALHOST = "127.0.0.1"
2021-09-26 02:35:02 +00:00
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 versionFlag = flag.Bool("v", false, "version")
2021-09-26 02:35:02 +00:00
func init() {
flag.Parse()
if *versionFlag {
fmt.Println("v" + types.CURRENTVERSION)
return
}
2021-09-26 02:35:02 +00:00
config.InitSetup(*configFlag)
config.UpdateSetup()
loger.LogInit()
2022-05-05 05:46:55 +00:00
if len(*dbFlag) == 0 {
*dbFlag = config.AppInfo.DBPath + "/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, config.CommonInfo.RuntimePath)
service.Cache = cache.Init()
2022-03-16 07:41:14 +00:00
service.GetToken()
2022-05-05 05:46:55 +00:00
service.NewVersionApp = make(map[string]string)
route.InitFunction()
2022-02-18 11:06:03 +00:00
✨ New Feature - [Apps] This is a feature that has been highly requested by the community. Import the original Docker application into CasaOS. Now it's easy to import with just a few clicks! - [Apps] App list supports a custom sorting function! You can arrange apps in different orders by dragging the icons. - [Apps] App custom installation supports Docker Compose configuration import in YAML format. - [Files] Added thumbnail preview function for image files. - [Connect] Multiple CasaConenct devices in the LAN will be transmitted through the LAN network. - [System] Added a switch for auto-mounting USB disk devices. 🎈 Enhancement - [System] Optimized the system update alert, you will see the new version update log from the next version. - [Apps] Added live preview for icons in custom installed apps. - [Apps] Optimized the input of WebUI. - [Files] Completely updated the image preview, now it supports switching all images in the same folder, as well as dragging, zooming, rotating and resetting. - [Widgets] Added color levels for CPU and RAM charts. - [Conenct] Optimized the display of the right-click menu of the Connect friends list. 🎈 Changed - [Files] Change the initial display directory to /DATA 🐞 Fixed - [System] Fixed an issue with Raspberry Pi devices failing to boot using USB disks. (Achieved by disabling USB disk auto-mount) - [Apps] Fixed the issue that some Docker CLI commands failed to import. - [Apps] Fixed the issue that the app is not easily recognized in /DATA/AppData directory and docker command line after installation, it will be shown as the app name. (Newly installed apps only) - [Apps] Fixed the issue that Pi-hole cannot be launched after installation in the app store. - [Apps] Fixed the issue that apps cannot be updated with WatchTower. - [Files] Fixed the issue that when there is an upload task, the task status is lost after closing Files.
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
}
// @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() {
service.NotifyMsg = make(chan notify.Message, 10)
if *versionFlag {
return
}
go route.SocketInit(service.NotifyMsg)
go route.MonitoryUSB()
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-04-06 04:10:51 +00:00
err := cron2.AddFunc("0/5 * * * * *", func() {
if service.ClientCount > 0 {
//route.SendNetINfoBySocket()
//route.SendCPUBySocket()
//route.SendMemBySocket()
// route.SendDiskBySocket()
//route.SendUSBBySocket()
route.SendAllHardwareStatusBySocket()
}
2022-05-05 05:46:55 +00:00
})
if err != nil {
fmt.Println(err)
}
2021-09-26 02:35:02 +00:00
cron2.Start()
2022-02-17 10:43:25 +00:00
defer cron2.Stop()
2021-09-26 02:35:02 +00:00
listener, err := net.Listen("tcp", net.JoinHostPort(LOCALHOST, "0"))
if err != nil {
panic(err)
}
routers := []string{"sys", "apps", "container", "app-categories", "port", "file", "folder", "batch", "image", "disks", "storage", "samba"}
for _, v := range routers {
err = service.MyService.Gateway().CreateRoute(&common.Route{
Path: "/v1/" + v,
Target: "http://" + listener.Addr().String(),
})
if err != nil {
fmt.Println("err", err)
panic(err)
}
}
go func() {
time.Sleep(time.Second * 2)
//v0.3.6
if config.ServerInfo.HttpPort != "" {
changePort := common.ChangePortRequest{}
changePort.Port = config.ServerInfo.HttpPort
err := service.MyService.Gateway().ChangePort(&changePort)
if err == nil {
config.Cfg.Section("server").Key("HttpPort").SetValue("")
config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
}
}
}()
// s := &http.Server{
// Addr: listener.Addr().String(), //fmt.Sprintf(":%v", config.ServerInfo.HttpPort),
// Handler: r,
// ReadTimeout: 60 * time.Second,
// WriteTimeout: 60 * time.Second,
// MaxHeaderBytes: 1 << 20,
// }
// s.ListenAndServe()
err = http.Serve(listener, r)
if err != nil {
panic(err)
}
2021-09-26 02:35:02 +00:00
}