move logic to serve.

This commit is contained in:
Miroslav Šedivý 2022-01-14 00:21:56 +01:00
parent 552c4e37bc
commit 62353c776d
2 changed files with 184 additions and 149 deletions

View File

@ -1,43 +1,166 @@
package cmd
import (
"os"
"os/signal"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"demodesk/neko"
"demodesk/neko/internal/config"
"demodesk/neko/internal/api"
"demodesk/neko/internal/capture"
"demodesk/neko/internal/desktop"
"demodesk/neko/internal/http"
"demodesk/neko/internal/member"
"demodesk/neko/internal/session"
"demodesk/neko/internal/webrtc"
"demodesk/neko/internal/websocket"
"demodesk/neko/modules"
)
func init() {
service := Serve{
Version: neko.Service.Version,
Configs: neko.Service.Configs,
}
command := &cobra.Command{
Use: "serve",
Short: "serve neko streaming server",
Long: `serve neko streaming server`,
Run: neko.Service.ServeCommand,
Run: service.ServeCommand,
}
configs := append([]config.Config{
neko.Service.Configs.Desktop,
neko.Service.Configs.Capture,
neko.Service.Configs.WebRTC,
neko.Service.Configs.Member,
neko.Service.Configs.Session,
neko.Service.Configs.Server,
}, modules.Configs()...)
cobra.OnInitialize(func() {
for _, cfg := range configs {
cfg.Set()
}
neko.Service.Preflight()
service.Configs.Set()
service.Preflight()
})
for _, cfg := range configs {
if err := cfg.Init(command); err != nil {
if err := service.Configs.Init(command); err != nil {
log.Panic().Err(err).Msg("unable to run serve command")
}
}
root.AddCommand(command)
}
type Serve struct {
Version *neko.Version
Configs *neko.Configs
logger zerolog.Logger
desktopManager *desktop.DesktopManagerCtx
captureManager *capture.CaptureManagerCtx
webRTCManager *webrtc.WebRTCManagerCtx
memberManager *member.MemberManagerCtx
sessionManager *session.SessionManagerCtx
webSocketManager *websocket.WebSocketManagerCtx
apiManager *api.ApiManagerCtx
httpManager *http.HttpManagerCtx
}
func (neko *Serve) Preflight() {
neko.logger = log.With().Str("service", "neko").Logger()
}
func (neko *Serve) Start() {
neko.sessionManager = session.New(
neko.Configs.Session,
)
neko.memberManager = member.New(
neko.sessionManager,
neko.Configs.Member,
)
if err := neko.memberManager.Connect(); err != nil {
neko.logger.Panic().Err(err).Msg("unable to connect to member manager")
}
neko.desktopManager = desktop.New(
neko.Configs.Desktop,
)
neko.desktopManager.Start()
neko.captureManager = capture.New(
neko.desktopManager,
neko.Configs.Capture,
)
neko.captureManager.Start()
neko.webRTCManager = webrtc.New(
neko.desktopManager,
neko.captureManager,
neko.Configs.WebRTC,
)
neko.webRTCManager.Start()
neko.webSocketManager = websocket.New(
neko.sessionManager,
neko.desktopManager,
neko.captureManager,
neko.webRTCManager,
)
neko.webSocketManager.Start()
neko.apiManager = api.New(
neko.sessionManager,
neko.memberManager,
neko.desktopManager,
neko.captureManager,
neko.Configs.Server,
)
modules.Start(
neko.sessionManager,
neko.webSocketManager,
neko.apiManager,
)
neko.httpManager = http.New(
neko.webSocketManager,
neko.apiManager,
neko.Configs.Server,
)
neko.httpManager.Start()
}
func (neko *Serve) Shutdown() {
var err error
err = neko.memberManager.Disconnect()
neko.logger.Err(err).Msg("member manager disconnect")
err = neko.desktopManager.Shutdown()
neko.logger.Err(err).Msg("desktop manager shutdown")
err = neko.captureManager.Shutdown()
neko.logger.Err(err).Msg("capture manager shutdown")
err = neko.webRTCManager.Shutdown()
neko.logger.Err(err).Msg("webrtc manager shutdown")
err = neko.webSocketManager.Shutdown()
neko.logger.Err(err).Msg("websocket manager shutdown")
err = modules.Shutdown()
neko.logger.Err(err).Msg("modules shutdown")
err = neko.httpManager.Shutdown()
neko.logger.Err(err).Msg("http manager shutdown")
}
func (neko *Serve) 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", sig)
neko.Shutdown()
neko.logger.Info().Msg("shutdown complete")
}

172
neko.go
View File

@ -2,24 +2,12 @@ package neko
import (
"fmt"
"os"
"os/signal"
"runtime"
"demodesk/neko/internal/api"
"demodesk/neko/internal/capture"
"demodesk/neko/internal/config"
"demodesk/neko/internal/desktop"
"demodesk/neko/internal/http"
"demodesk/neko/internal/member"
"demodesk/neko/internal/session"
"demodesk/neko/internal/webrtc"
"demodesk/neko/internal/websocket"
"demodesk/neko/modules"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"demodesk/neko/internal/config"
"demodesk/neko/modules"
)
const Header = `&34
@ -113,122 +101,46 @@ type Configs struct {
Server *config.Server
}
func (i *Configs) Set() {
i.Root.Set()
i.Desktop.Set()
i.Capture.Set()
i.WebRTC.Set()
i.Member.Set()
i.Session.Set()
i.Server.Set()
modules.SetConfigs()
}
func (i *Configs) Init(cmd *cobra.Command) error {
if err := i.Root.Init(cmd); err != nil {
return err
}
if err := i.Desktop.Init(cmd); err != nil {
return err
}
if err := i.Capture.Init(cmd); err != nil {
return err
}
if err := i.WebRTC.Init(cmd); err != nil {
return err
}
if err := i.Member.Init(cmd); err != nil {
return err
}
if err := i.Session.Init(cmd); err != nil {
return err
}
if err := i.Server.Init(cmd); err != nil {
return err
}
if err := modules.InitConfigs(cmd); err != nil {
return err
}
return nil
}
type Neko struct {
Version *Version
Configs *Configs
logger zerolog.Logger
desktopManager *desktop.DesktopManagerCtx
captureManager *capture.CaptureManagerCtx
webRTCManager *webrtc.WebRTCManagerCtx
memberManager *member.MemberManagerCtx
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() {
neko.sessionManager = session.New(
neko.Configs.Session,
)
neko.memberManager = member.New(
neko.sessionManager,
neko.Configs.Member,
)
if err := neko.memberManager.Connect(); err != nil {
neko.logger.Panic().Err(err).Msg("unable to connect to member manager")
}
neko.desktopManager = desktop.New(
neko.Configs.Desktop,
)
neko.desktopManager.Start()
neko.captureManager = capture.New(
neko.desktopManager,
neko.Configs.Capture,
)
neko.captureManager.Start()
neko.webRTCManager = webrtc.New(
neko.desktopManager,
neko.captureManager,
neko.Configs.WebRTC,
)
neko.webRTCManager.Start()
neko.webSocketManager = websocket.New(
neko.sessionManager,
neko.desktopManager,
neko.captureManager,
neko.webRTCManager,
)
neko.webSocketManager.Start()
neko.apiManager = api.New(
neko.sessionManager,
neko.memberManager,
neko.desktopManager,
neko.captureManager,
neko.Configs.Server,
)
modules.Start(
neko.sessionManager,
neko.webSocketManager,
neko.apiManager,
)
neko.httpManager = http.New(
neko.webSocketManager,
neko.apiManager,
neko.Configs.Server,
)
neko.httpManager.Start()
}
func (neko *Neko) Shutdown() {
var err error
err = neko.memberManager.Disconnect()
neko.logger.Err(err).Msg("member manager disconnect")
err = neko.desktopManager.Shutdown()
neko.logger.Err(err).Msg("desktop manager shutdown")
err = neko.captureManager.Shutdown()
neko.logger.Err(err).Msg("capture manager shutdown")
err = neko.webRTCManager.Shutdown()
neko.logger.Err(err).Msg("webrtc manager shutdown")
err = neko.webSocketManager.Shutdown()
neko.logger.Err(err).Msg("websocket manager shutdown")
err = modules.Shutdown()
neko.logger.Err(err).Msg("modules shutdown")
err = neko.httpManager.Shutdown()
neko.logger.Err(err).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", sig)
neko.Shutdown()
neko.logger.Info().Msg("shutdown complete")
}