neko/neko.go

253 lines
5.8 KiB
Go
Raw Normal View History

package neko
import (
"fmt"
"os"
"os/signal"
"runtime"
2021-02-15 02:40:17 +13:00
"demodesk/neko/internal/api"
"demodesk/neko/internal/capture"
2020-11-02 04:09:48 +13:00
"demodesk/neko/internal/config"
"demodesk/neko/internal/desktop"
2021-02-15 02:40:17 +13:00
"demodesk/neko/internal/http"
2021-03-14 13:08:50 +13:00
"demodesk/neko/internal/member"
2020-11-02 04:09:48 +13:00
"demodesk/neko/internal/session"
2021-02-15 02:40:17 +13:00
"demodesk/neko/internal/webrtc"
2020-10-29 07:15:48 +13:00
"demodesk/neko/internal/websocket"
2020-12-13 06:37:19 +13:00
"demodesk/neko/modules"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
const Header = `&34
_ __ __
/ | / /__ / /______ \ /\
/ |/ / _ \/ //_/ __ \ ) ( ')
/ /| / __/ ,< / /_/ / ( / )
/_/ |_/\___/_/|_|\____/ \(__)|
&1&37 nurdism/m1k1o &33%s v%s&0
`
var (
//
buildDate = "dev"
//
gitCommit = "dev"
//
gitBranch = "dev"
// Major version when you make incompatible API changes,
major = "dev"
// Minor version when you add functionality in a backwards-compatible manner, and
minor = "dev"
// Patch version when you make backwards-compatible bug fixeneko.
patch = "dev"
)
var Service *Neko
func init() {
Service = &Neko{
Version: &Version{
Major: major,
Minor: minor,
Patch: patch,
GitCommit: gitCommit,
GitBranch: gitBranch,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
},
2020-11-01 04:40:33 +13:00
Configs: &Configs{
2021-02-15 02:40:17 +13:00
Root: &config.Root{},
Desktop: &config.Desktop{},
Capture: &config.Capture{},
WebRTC: &config.WebRTC{},
2021-03-14 13:08:50 +13:00
Member: &config.Member{},
2021-02-15 02:40:17 +13:00
Session: &config.Session{},
Server: &config.Server{},
2020-11-01 04:40:33 +13:00
},
}
}
type Version struct {
Major string
Minor string
Patch string
GitCommit string
GitBranch string
BuildDate string
GoVersion string
Compiler string
Platform string
}
func (i *Version) String() string {
return fmt.Sprintf("%s.%s.%s %s", i.Major, i.Minor, i.Patch, i.GitCommit)
}
func (i *Version) Details() string {
return fmt.Sprintf(
"%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
fmt.Sprintf("Version %s.%s.%s", i.Major, i.Minor, i.Patch),
fmt.Sprintf("GitCommit %s", i.GitCommit),
fmt.Sprintf("GitBranch %s", i.GitBranch),
fmt.Sprintf("BuildDate %s", i.BuildDate),
fmt.Sprintf("GoVersion %s", i.GoVersion),
fmt.Sprintf("Compiler %s", i.Compiler),
fmt.Sprintf("Platform %s", i.Platform),
)
}
2020-11-01 04:40:33 +13:00
type Configs struct {
2021-02-15 02:40:17 +13:00
Root *config.Root
Desktop *config.Desktop
Capture *config.Capture
WebRTC *config.WebRTC
2021-03-14 13:08:50 +13:00
Member *config.Member
2021-02-15 02:40:17 +13:00
Session *config.Session
Server *config.Server
2020-11-01 04:40:33 +13:00
}
type Neko struct {
2021-02-15 02:40:17 +13:00
Version *Version
Configs *Configs
logger zerolog.Logger
desktopManager *desktop.DesktopManagerCtx
captureManager *capture.CaptureManagerCtx
webRTCManager *webrtc.WebRTCManagerCtx
2021-03-15 04:58:18 +13:00
memberManager *member.MemberManagerCtx
2021-02-15 02:40:17 +13:00
sessionManager *session.SessionManagerCtx
webSocketManager *websocket.WebSocketManagerCtx
apiManager *api.ApiManagerCtx
httpManager *http.HttpManagerCtx
}
func (neko *Neko) Preflight() {
neko.logger = log.With().Str("service", "neko").Logger()
}
func (neko *Neko) Start() {
2021-03-15 02:44:32 +13:00
neko.sessionManager = session.New(
neko.Configs.Session,
)
2021-03-14 13:08:50 +13:00
neko.memberManager = member.New(
neko.Configs.Member,
)
2021-03-15 02:44:32 +13:00
2021-03-14 13:08:50 +13:00
if err := neko.memberManager.Connect(); err != nil {
neko.logger.Panic().Err(err).Msg("unable to connect to member manager")
}
2020-11-02 04:09:48 +13:00
neko.desktopManager = desktop.New(
2020-11-04 12:27:47 +13:00
neko.Configs.Desktop,
2020-11-01 04:40:33 +13:00
)
2020-11-02 04:09:48 +13:00
neko.desktopManager.Start()
2020-11-02 04:09:48 +13:00
neko.captureManager = capture.New(
neko.desktopManager,
neko.Configs.Capture,
2020-11-01 04:40:33 +13:00
)
2020-11-02 04:09:48 +13:00
neko.captureManager.Start()
2020-11-01 04:40:33 +13:00
neko.webRTCManager = webrtc.New(
2020-11-02 04:09:48 +13:00
neko.desktopManager,
neko.captureManager,
2020-11-01 04:40:33 +13:00
neko.Configs.WebRTC,
)
neko.webRTCManager.Start()
2020-11-02 04:09:48 +13:00
neko.webSocketManager = websocket.New(
2020-11-01 04:40:33 +13:00
neko.sessionManager,
2020-11-02 04:09:48 +13:00
neko.desktopManager,
neko.captureManager,
2020-11-01 04:40:33 +13:00
neko.webRTCManager,
)
2020-11-02 04:09:48 +13:00
neko.webSocketManager.Start()
2020-11-01 04:40:33 +13:00
2020-11-02 04:54:06 +13:00
neko.apiManager = api.New(
neko.sessionManager,
2021-03-15 02:44:32 +13:00
neko.memberManager,
2020-11-02 04:54:06 +13:00
neko.desktopManager,
neko.captureManager,
neko.Configs.Server,
)
2020-12-13 06:37:19 +13:00
modules.Start(
neko.sessionManager,
neko.webSocketManager,
neko.apiManager,
)
2020-11-14 05:34:53 +13:00
neko.httpManager = http.New(
2020-11-02 04:09:48 +13:00
neko.webSocketManager,
2020-11-02 04:54:06 +13:00
neko.apiManager,
2020-11-01 04:40:33 +13:00
neko.Configs.Server,
2020-10-31 06:16:21 +13:00
)
2020-11-14 05:34:53 +13:00
neko.httpManager.Start()
}
func (neko *Neko) Shutdown() {
2021-03-14 13:08:50 +13:00
if err := neko.memberManager.Disconnect(); err != nil {
neko.logger.Err(err).Msg("member manager disconnect with an error")
} else {
neko.logger.Debug().Msg("member manager disconnect")
}
2020-11-02 04:09:48 +13:00
if err := neko.desktopManager.Shutdown(); err != nil {
neko.logger.Err(err).Msg("desktop manager shutdown with an error")
} else {
neko.logger.Debug().Msg("desktop manager shutdown")
}
if err := neko.captureManager.Shutdown(); err != nil {
neko.logger.Err(err).Msg("capture manager shutdown with an error")
} else {
2020-11-02 04:09:48 +13:00
neko.logger.Debug().Msg("capture manager shutdown")
}
if err := neko.webRTCManager.Shutdown(); err != nil {
neko.logger.Err(err).Msg("webrtc manager shutdown with an error")
} else {
neko.logger.Debug().Msg("webrtc manager shutdown")
}
2020-11-02 04:09:48 +13:00
if err := neko.webSocketManager.Shutdown(); err != nil {
neko.logger.Err(err).Msg("websocket manager shutdown with an error")
} else {
2020-11-02 04:09:48 +13:00
neko.logger.Debug().Msg("websocket manager shutdown")
}
2020-12-13 06:37:19 +13:00
if err := modules.Shutdown(); err != nil {
neko.logger.Err(err).Msg("modules shutdown with an error")
} else {
neko.logger.Debug().Msg("modules shutdown")
}
2020-11-14 05:34:53 +13:00
if err := neko.httpManager.Shutdown(); err != nil {
neko.logger.Err(err).Msg("http manager shutdown with an error")
} else {
2020-11-14 05:34:53 +13:00
neko.logger.Debug().Msg("http manager shutdown")
}
}
func (neko *Neko) ServeCommand(cmd *cobra.Command, args []string) {
neko.logger.Info().Msg("starting neko server")
neko.Start()
neko.logger.Info().Msg("neko ready")
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
sig := <-quit
neko.logger.Warn().Msgf("received %s, attempting graceful shutdown: \n", sig)
neko.Shutdown()
neko.logger.Info().Msg("shutdown complete")
}