2021-09-26 02:35:02 +00:00
package main
import (
"flag"
"fmt"
2022-09-06 06:28:49 +00:00
"net"
2021-10-15 03:43:41 +00:00
"net/http"
2022-10-20 06:25:26 +00:00
"path/filepath"
2021-10-15 03:43:41 +00:00
"time"
2022-10-28 21:34:18 +00:00
"github.com/IceWhaleTech/CasaOS-Common/model"
2022-12-15 23:31:06 +00:00
"github.com/IceWhaleTech/CasaOS-Common/utils/constants"
2022-11-29 17:17:14 +00:00
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
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-12-15 23:31:06 +00:00
"github.com/IceWhaleTech/CasaOS/pkg/utils/command"
2022-10-20 06:25:26 +00:00
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
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"
2022-10-20 06:25:26 +00:00
"github.com/coreos/go-systemd/daemon"
"go.uber.org/zap"
2021-10-29 10:37:27 +00:00
2021-09-26 02:35:02 +00:00
"github.com/robfig/cron"
"gorm.io/gorm"
)
2022-09-06 06:28:49 +00:00
const LOCALHOST = "127.0.0.1"
2021-09-26 02:35:02 +00:00
var sqliteDB * gorm . DB
2022-10-20 06:25:26 +00:00
var (
configFlag = flag . String ( "c" , "" , "config address" )
dbFlag = flag . String ( "db" , "" , "db path" )
versionFlag = flag . Bool ( "v" , false , "version" )
)
2021-10-22 08:49:09 +00:00
2021-09-26 02:35:02 +00:00
func init ( ) {
flag . Parse ( )
2022-09-06 06:28:49 +00:00
if * versionFlag {
2022-08-15 03:37:21 +00:00
fmt . Println ( "v" + types . CURRENTVERSION )
return
}
2021-09-26 02:35:02 +00:00
config . InitSetup ( * configFlag )
2022-08-15 03:37:21 +00:00
2022-11-29 17:17:14 +00:00
logger . LogInit ( config . AppInfo . LogPath , config . AppInfo . LogSaveName , config . AppInfo . LogFileExt )
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 )
2022-10-20 06:25:26 +00:00
// gredis.GetRedisConn(config.RedisInfo),
2022-08-15 03:37:21 +00:00
2022-09-06 06:28:49 +00:00
service . MyService = service . NewService ( sqliteDB , config . CommonInfo . RuntimePath )
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-11-16 09:30:19 +00:00
service . GetCPUThermalZone ( )
2022-08-15 03:37:21 +00:00
2021-11-09 10:57:50 +00:00
route . InitFunction ( )
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-09-06 06:28:49 +00:00
if * versionFlag {
2022-06-29 03:09:58 +00:00
return
}
2022-06-08 10:19:45 +00:00
go route . SocketInit ( service . NotifyMsg )
2022-10-20 06:25:26 +00:00
// model.Setup()
// gredis.Setup()
2022-09-06 06:28:49 +00:00
2021-10-22 08:49:09 +00:00
r := route . InitRouter ( )
2022-10-20 06:25:26 +00:00
// service.SyncTask(sqliteDB)
2022-04-06 04:10:51 +00:00
cron2 := cron . New ( )
2022-10-20 06:25:26 +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-10-20 06:25:26 +00:00
// route.SendNetINfoBySocket()
// route.SendCPUBySocket()
// route.SendMemBySocket()
2022-06-10 05:33:53 +00:00
// route.SendDiskBySocket()
2022-10-20 06:25:26 +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 ( )
2022-02-17 10:43:25 +00:00
2022-09-06 06:28:49 +00:00
defer cron2 . Stop ( )
2021-09-26 02:35:02 +00:00
2022-09-06 06:28:49 +00:00
listener , err := net . Listen ( "tcp" , net . JoinHostPort ( LOCALHOST , "0" ) )
if err != nil {
panic ( err )
}
2022-11-29 17:17:14 +00:00
routers := [ ] string { "sys" , "port" , "file" , "folder" , "batch" , "image" , "samba" , "notify" }
2022-09-06 06:28:49 +00:00
for _ , v := range routers {
2022-10-28 21:34:18 +00:00
err = service . MyService . Gateway ( ) . CreateRoute ( & model . Route {
2022-09-06 06:28:49 +00:00
Path : "/v1/" + v ,
Target : "http://" + listener . Addr ( ) . String ( ) ,
} )
if err != nil {
fmt . Println ( "err" , err )
panic ( err )
}
}
go func ( ) {
time . Sleep ( time . Second * 2 )
2022-10-20 06:25:26 +00:00
// v0.3.6
2022-09-06 06:28:49 +00:00
if config . ServerInfo . HttpPort != "" {
2022-10-28 21:34:18 +00:00
changePort := model . ChangePortRequest { }
2022-09-06 06:28:49 +00:00
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 )
}
}
} ( )
2022-10-20 06:25:26 +00:00
urlFilePath := filepath . Join ( config . CommonInfo . RuntimePath , "casaos.url" )
2022-11-29 17:17:14 +00:00
if err := file . CreateFileAndWriteContent ( urlFilePath , "http://" + listener . Addr ( ) . String ( ) ) ; err != nil {
logger . Error ( "error when creating address file" , zap . Error ( err ) ,
2022-10-20 06:25:26 +00:00
zap . Any ( "address" , listener . Addr ( ) . String ( ) ) ,
zap . Any ( "filepath" , urlFilePath ) ,
)
}
2022-12-15 23:31:06 +00:00
// run any script that needs to be executed
scriptDirectory := filepath . Join ( constants . DefaultConfigPath , "start.d" )
command . ExecuteScripts ( scriptDirectory )
2022-10-20 06:25:26 +00:00
if supported , err := daemon . SdNotify ( false , daemon . SdNotifyReady ) ; err != nil {
2022-11-29 17:17:14 +00:00
logger . Error ( "Failed to notify systemd that casaos main service is ready" , zap . Any ( "error" , err ) )
2022-10-20 06:25:26 +00:00
} else if supported {
2022-11-29 17:17:14 +00:00
logger . Info ( "Notified systemd that casaos main service is ready" )
2022-10-20 06:25:26 +00:00
} else {
2022-11-29 17:17:14 +00:00
logger . Info ( "This process is not running as a systemd service." )
2022-10-20 06:25:26 +00:00
}
s := & http . Server {
Handler : r ,
ReadHeaderTimeout : 5 * time . Second , // fix G112: Potential slowloris attack (see https://github.com/securego/gosec)
}
2022-11-29 17:17:14 +00:00
logger . Info ( "CasaOS main service is listening..." , zap . Any ( "address" , listener . Addr ( ) . String ( ) ) )
2022-10-20 06:25:26 +00:00
err = s . Serve ( listener ) // not using http.serve() to fix G114: Use of net/http serve function that has no support for setting timeouts (see https://github.com/securego/gosec)
2022-09-06 06:28:49 +00:00
if err != nil {
panic ( err )
}
2021-09-26 02:35:02 +00:00
}