2023-01-30 17:01:47 -05:00
//go:generate bash -c "mkdir -p codegen && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,server,spec -package codegen api/casaos/openapi.yaml > codegen/casaos_api.go"
2023-02-06 15:47:29 +08:00
//go:generate bash -c "mkdir -p codegen/message_bus && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,client -package message_bus https://raw.githubusercontent.com/IceWhaleTech/CasaOS-MessageBus/main/api/message_bus/openapi.yaml > codegen/message_bus/api.go"
2021-09-26 10:35:02 +08:00
package main
import (
2023-02-06 15:47:29 +08:00
"context"
2023-01-30 17:01:47 -05:00
_ "embed"
2021-09-26 10:35:02 +08:00
"flag"
"fmt"
2022-09-06 14:28:49 +08:00
"net"
2021-10-15 11:43:41 +08:00
"net/http"
2022-10-20 02:25:26 -04:00
"path/filepath"
2021-10-15 11:43:41 +08:00
"time"
2022-10-28 17:34:18 -04:00
"github.com/IceWhaleTech/CasaOS-Common/model"
2022-12-15 18:31:06 -05:00
"github.com/IceWhaleTech/CasaOS-Common/utils/constants"
2022-11-29 12:17:14 -05:00
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
2023-01-30 17:01:47 -05:00
util_http "github.com/IceWhaleTech/CasaOS-Common/utils/http"
2023-02-06 15:47:29 +08:00
"github.com/IceWhaleTech/CasaOS/common"
2021-10-29 18:37:27 +08:00
"github.com/IceWhaleTech/CasaOS/pkg/cache"
2021-09-27 14:17:36 +08:00
"github.com/IceWhaleTech/CasaOS/pkg/config"
"github.com/IceWhaleTech/CasaOS/pkg/sqlite"
2022-12-15 18:31:06 -05:00
"github.com/IceWhaleTech/CasaOS/pkg/utils/command"
2022-10-20 02:25:26 -04:00
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
2021-09-27 14:17:36 +08:00
"github.com/IceWhaleTech/CasaOS/route"
"github.com/IceWhaleTech/CasaOS/service"
2022-10-20 02:25:26 -04:00
"github.com/coreos/go-systemd/daemon"
"go.uber.org/zap"
2021-10-29 18:37:27 +08:00
2023-03-18 20:55:51 -04:00
"github.com/robfig/cron/v3"
2021-09-26 10:35:02 +08:00
"gorm.io/gorm"
)
2022-09-06 14:28:49 +08:00
const LOCALHOST = "127.0.0.1"
2021-09-26 10:35:02 +08:00
var sqliteDB * gorm . DB
2022-10-20 02:25:26 -04:00
var (
2022-12-29 18:24:36 -05:00
commit = "private build"
date = "private build"
2023-01-30 17:01:47 -05:00
//go:embed api/index.html
_docHTML string
//go:embed api/casaos/openapi.yaml
_docYAML string
2023-07-31 16:53:03 +08:00
//go:embed build/sysroot/etc/casaos/casaos.conf.sample
_confSample string
2022-10-20 02:25:26 -04:00
configFlag = flag . String ( "c" , "" , "config address" )
dbFlag = flag . String ( "db" , "" , "db path" )
versionFlag = flag . Bool ( "v" , false , "version" )
)
2021-10-22 16:49:09 +08:00
2021-09-26 10:35:02 +08:00
func init ( ) {
flag . Parse ( )
2022-09-06 14:28:49 +08:00
if * versionFlag {
2023-02-06 15:47:29 +08:00
fmt . Println ( "v" + common . VERSION )
2022-08-15 11:37:21 +08:00
return
}
2022-12-29 18:24:36 -05:00
println ( "git commit:" , commit )
println ( "build date:" , date )
2023-07-31 16:53:03 +08:00
config . InitSetup ( * configFlag , _confSample )
2022-08-15 11:37:21 +08:00
2022-11-29 12:17:14 -05:00
logger . LogInit ( config . AppInfo . LogPath , config . AppInfo . LogSaveName , config . AppInfo . LogFileExt )
2022-05-05 13:46:55 +08:00
if len ( * dbFlag ) == 0 {
2022-06-29 11:09:58 +08:00
* dbFlag = config . AppInfo . DBPath + "/db"
2022-04-06 12:10:51 +08:00
}
2022-08-15 11:37:21 +08:00
2022-05-05 13:46:55 +08:00
sqliteDB = sqlite . GetDb ( * dbFlag )
2022-10-20 02:25:26 -04:00
// gredis.GetRedisConn(config.RedisInfo),
2022-08-15 11:37:21 +08:00
2023-02-06 15:47:29 +08:00
service . MyService = service . NewService ( sqliteDB , config . CommonInfo . RuntimePath )
2022-08-15 11:37:21 +08:00
2021-10-29 18:37:27 +08:00
service . Cache = cache . Init ( )
2022-03-16 15:41:14 +08:00
2022-11-16 09:30:19 +00:00
service . GetCPUThermalZone ( )
2023-03-06 16:10:19 +08:00
2021-11-09 18:57:50 +08:00
route . InitFunction ( )
2023-06-02 18:51:25 +08:00
2023-05-30 18:57:15 +08:00
//service.MyService.System().GenreateSystemEntry()
2023-03-17 15:37:28 +08:00
///
2023-04-12 13:53:27 +08:00
//service.MountLists = make(map[string]*mountlib.MountPoint)
//configfile.Install()
2021-09-26 10:35:02 +08:00
}
2021-10-22 16:49:09 +08:00
// @title casaOS API
2021-09-26 10:35:02 +08:00
// @version 1.0.0
// @contact.name lauren.pan
// @contact.url https://www.zimaboard.com
// @contact.email lauren.pan@icewhale.org
2021-10-22 16:49:09 +08:00
// @description casaOS v1版本api
// @host 192.168.2.217:8089
2021-09-26 10:35:02 +08:00
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @BasePath /v1
func main ( ) {
2022-09-06 14:28:49 +08:00
if * versionFlag {
2022-06-29 11:09:58 +08:00
return
}
2023-01-30 17:01:47 -05:00
v1Router := route . InitV1Router ( )
2023-02-02 15:59:33 +00:00
2023-01-30 17:01:47 -05:00
v2Router := route . InitV2Router ( )
v2DocRouter := route . InitV2DocRouter ( _docHTML , _docYAML )
2023-08-23 08:29:41 +01:00
v3File := route . InitFile ( )
2023-01-30 17:01:47 -05:00
mux := & util_http . HandlerMultiplexer {
HandlerMap : map [ string ] http . Handler {
"v1" : v1Router ,
"v2" : v2Router ,
2023-08-23 08:29:41 +01:00
"v3" : v3File ,
2023-01-30 17:01:47 -05:00
"doc" : v2DocRouter ,
} ,
}
2023-03-18 20:55:51 -04:00
crontab := cron . New ( cron . WithSeconds ( ) )
if _ , err := crontab . AddFunc ( "@every 5s" , route . SendAllHardwareStatusBySocket ) ; err != nil {
logger . Error ( "add crontab error" , zap . Error ( err ) )
2022-05-05 13:46:55 +08:00
}
2022-02-17 18:43:25 +08:00
2023-03-18 20:55:51 -04:00
crontab . Start ( )
defer crontab . Stop ( )
2021-09-26 10:35:02 +08:00
2022-09-06 14:28:49 +08:00
listener , err := net . Listen ( "tcp" , net . JoinHostPort ( LOCALHOST , "0" ) )
if err != nil {
panic ( err )
}
2023-02-02 03:51:41 +00:00
routers := [ ] string {
2023-01-30 17:01:47 -05:00
"/v1/sys" ,
"/v1/port" ,
"/v1/file" ,
"/v1/folder" ,
"/v1/batch" ,
"/v1/image" ,
"/v1/samba" ,
"/v1/notify" ,
2023-03-17 15:37:28 +08:00
"/v1/driver" ,
"/v1/cloud" ,
"/v1/recover" ,
2023-02-16 16:55:51 +08:00
"/v1/other" ,
2023-05-22 16:44:39 +08:00
"/v1/zt" ,
2023-05-23 16:39:25 +08:00
"/v1/test" ,
2023-01-30 17:01:47 -05:00
route . V2APIPath ,
route . V2DocPath ,
2023-08-23 08:29:41 +01:00
route . V3FilePath ,
2023-01-30 17:01:47 -05:00
}
2023-02-02 03:51:41 +00:00
for _ , apiPath := range routers {
2022-10-28 17:34:18 -04:00
err = service . MyService . Gateway ( ) . CreateRoute ( & model . Route {
2023-01-30 17:01:47 -05:00
Path : apiPath ,
2022-09-06 14:28:49 +08:00
Target : "http://" + listener . Addr ( ) . String ( ) ,
} )
if err != nil {
fmt . Println ( "err" , err )
panic ( err )
}
}
2023-08-23 18:21:12 +08:00
2023-02-06 15:47:29 +08:00
// register at message bus
2023-02-17 18:40:19 +08:00
for i := 0 ; i < 10 ; i ++ {
2023-08-23 18:21:12 +08:00
response , err := service . MyService . MessageBus ( ) . RegisterEventTypesWithResponse ( context . Background ( ) , common . EventTypes )
2023-02-17 18:40:19 +08:00
if err != nil {
logger . Error ( "error when trying to register one or more event types - some event type will not be discoverable" , zap . Error ( err ) )
}
if response != nil && response . StatusCode ( ) != http . StatusOK {
logger . Error ( "error when trying to register one or more event types - some event type will not be discoverable" , zap . String ( "status" , response . Status ( ) ) , zap . String ( "body" , string ( response . Body ) ) )
}
if response . StatusCode ( ) == http . StatusOK {
break
}
time . Sleep ( time . Second )
2023-02-06 15:47:29 +08:00
}
2022-09-06 14:28:49 +08:00
go func ( ) {
time . Sleep ( time . Second * 2 )
2022-10-20 02:25:26 -04:00
// v0.3.6
2022-09-06 14:28:49 +08:00
if config . ServerInfo . HttpPort != "" {
2022-10-28 17:34:18 -04:00
changePort := model . ChangePortRequest { }
2022-09-06 14:28:49 +08: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 02:25:26 -04:00
urlFilePath := filepath . Join ( config . CommonInfo . RuntimePath , "casaos.url" )
2022-11-29 12:17:14 -05: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 02:25:26 -04:00
zap . Any ( "address" , listener . Addr ( ) . String ( ) ) ,
zap . Any ( "filepath" , urlFilePath ) ,
)
}
2022-12-15 18:31:06 -05:00
// run any script that needs to be executed
scriptDirectory := filepath . Join ( constants . DefaultConfigPath , "start.d" )
command . ExecuteScripts ( scriptDirectory )
2022-10-20 02:25:26 -04:00
if supported , err := daemon . SdNotify ( false , daemon . SdNotifyReady ) ; err != nil {
2022-11-29 12:17:14 -05:00
logger . Error ( "Failed to notify systemd that casaos main service is ready" , zap . Any ( "error" , err ) )
2022-10-20 02:25:26 -04:00
} else if supported {
2022-11-29 12:17:14 -05:00
logger . Info ( "Notified systemd that casaos main service is ready" )
2022-10-20 02:25:26 -04:00
} else {
2022-11-29 12:17:14 -05:00
logger . Info ( "This process is not running as a systemd service." )
2022-10-20 02:25:26 -04:00
}
2023-02-08 18:11:46 +08:00
// http.HandleFunc("/v1/file/test", func(w http.ResponseWriter, r *http.Request) {
// //http.ServeFile(w, r, r.URL.Path[1:])
// http.ServeFile(w, r, "/DATA/test.img")
// })
// go http.ListenAndServe(":8081", nil)
2022-10-20 02:25:26 -04:00
s := & http . Server {
2023-01-30 17:01:47 -05:00
Handler : mux ,
2022-10-20 02:25:26 -04:00
ReadHeaderTimeout : 5 * time . Second , // fix G112: Potential slowloris attack (see https://github.com/securego/gosec)
}
2022-11-29 12:17:14 -05:00
logger . Info ( "CasaOS main service is listening..." , zap . Any ( "address" , listener . Addr ( ) . String ( ) ) )
2023-03-18 20:55:51 -04:00
// defer service.MyService.Storage().UnmountAllStorage()
2022-10-20 02:25:26 -04: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 14:28:49 +08:00
if err != nil {
panic ( err )
}
2021-09-26 10:35:02 +08:00
}