move configs to serve.

This commit is contained in:
Miroslav Šedivý 2022-01-16 20:35:57 +01:00
parent 66d22dede4
commit 0dd11fc1b7
4 changed files with 164 additions and 158 deletions

View File

@ -11,7 +11,7 @@ import (
) )
func main() { func main() {
fmt.Print(utils.Colorf(neko.Header, "server", neko.Service.Version)) fmt.Print(utils.Colorf(neko.Header, "server", neko.Version))
if err := cmd.Execute(); err != nil { if err := cmd.Execute(); err != nil {
log.Panic().Err(err).Msg("failed to execute command") log.Panic().Err(err).Msg("failed to execute command")
} }

View File

@ -17,7 +17,6 @@ import (
"demodesk/neko" "demodesk/neko"
"demodesk/neko/internal/config" "demodesk/neko/internal/config"
"demodesk/neko/modules"
) )
func Execute() error { func Execute() error {
@ -28,19 +27,11 @@ var root = &cobra.Command{
Use: "neko", Use: "neko",
Short: "neko streaming server", Short: "neko streaming server",
Long: `neko streaming server`, Long: `neko streaming server`,
Version: neko.Service.Version.String(), Version: neko.Version.String(),
} }
func init() { func init() {
configs := append([]config.Config{ rootConfig := config.Root{}
neko.Service.Configs.Root,
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() { cobra.OnInitialize(func() {
////// //////
@ -138,16 +129,12 @@ func init() {
} }
} }
for _, cfg := range configs { rootConfig.Set()
cfg.Set()
}
}) })
for _, cfg := range configs { if err := rootConfig.Init(root); err != nil {
if err := cfg.Init(root); err != nil { log.Panic().Err(err).Msg("unable to run root command")
log.Panic().Err(err).Msg("unable to initialize configuration")
}
} }
root.SetVersionTemplate(neko.Service.Version.Details()) root.SetVersionTemplate(neko.Version.Details())
} }

View File

@ -8,9 +8,9 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"demodesk/neko"
"demodesk/neko/internal/api" "demodesk/neko/internal/api"
"demodesk/neko/internal/capture" "demodesk/neko/internal/capture"
"demodesk/neko/internal/config"
"demodesk/neko/internal/desktop" "demodesk/neko/internal/desktop"
"demodesk/neko/internal/http" "demodesk/neko/internal/http"
"demodesk/neko/internal/member" "demodesk/neko/internal/member"
@ -21,139 +21,190 @@ import (
) )
func init() { func init() {
service := Serve{ service := serve{}
Version: neko.Service.Version,
Configs: neko.Service.Configs,
}
command := &cobra.Command{ command := &cobra.Command{
Use: "serve", Use: "serve",
Short: "serve neko streaming server", Short: "serve neko streaming server",
Long: `serve neko streaming server`, Long: `serve neko streaming server`,
Run: service.ServeCommand, Run: service.Command,
} }
cobra.OnInitialize(service.Preflight) cobra.OnInitialize(service.Preflight)
if err := service.Init(command); err != nil {
log.Panic().Err(err).Msg("unable to initialize configuration")
}
root.AddCommand(command) root.AddCommand(command)
} }
type Serve struct { type serve struct {
Version *neko.Version
Configs *neko.Configs
logger zerolog.Logger 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() { configs struct {
neko.logger = log.With().Str("service", "neko").Logger() Root config.Root
} Desktop config.Desktop
Capture config.Capture
func (neko *Serve) Start() { WebRTC config.WebRTC
neko.sessionManager = session.New( Member config.Member
neko.Configs.Session, Session config.Session
) Server config.Server
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( managers struct {
neko.Configs.Desktop, desktop *desktop.DesktopManagerCtx
) capture *capture.CaptureManagerCtx
neko.desktopManager.Start() webRTC *webrtc.WebRTCManagerCtx
member *member.MemberManagerCtx
session *session.SessionManagerCtx
webSocket *websocket.WebSocketManagerCtx
api *api.ApiManagerCtx
http *http.HttpManagerCtx
}
}
neko.captureManager = capture.New( func (c *serve) Init(cmd *cobra.Command) error {
neko.desktopManager, if err := c.configs.Desktop.Init(cmd); err != nil {
neko.Configs.Capture, return err
) }
neko.captureManager.Start() if err := c.configs.Capture.Init(cmd); err != nil {
return err
}
if err := c.configs.WebRTC.Init(cmd); err != nil {
return err
}
if err := c.configs.Member.Init(cmd); err != nil {
return err
}
if err := c.configs.Session.Init(cmd); err != nil {
return err
}
if err := c.configs.Server.Init(cmd); err != nil {
return err
}
neko.webRTCManager = webrtc.New( for _, cfg := range modules.Configs() {
neko.desktopManager, if err := cfg.Init(root); err != nil {
neko.captureManager, log.Panic().Err(err).Msg("unable to initialize configuration")
neko.Configs.WebRTC, }
) }
neko.webRTCManager.Start()
neko.webSocketManager = websocket.New( return nil
neko.sessionManager, }
neko.desktopManager,
neko.captureManager,
neko.webRTCManager,
)
neko.webSocketManager.Start()
neko.apiManager = api.New( func (c *serve) Preflight() {
neko.sessionManager, c.logger = log.With().Str("service", "neko").Logger()
neko.memberManager,
neko.desktopManager, c.configs.Desktop.Set()
neko.captureManager, c.configs.Capture.Set()
neko.Configs.Server, c.configs.WebRTC.Set()
c.configs.Member.Set()
c.configs.Session.Set()
c.configs.Server.Set()
for _, cfg := range modules.Configs() {
cfg.Set()
}
}
func (c *serve) Start() {
c.managers.session = session.New(
&c.configs.Session,
)
c.managers.member = member.New(
c.managers.session,
&c.configs.Member,
)
if err := c.managers.member.Connect(); err != nil {
c.logger.Panic().Err(err).Msg("unable to connect to member manager")
}
c.managers.desktop = desktop.New(
&c.configs.Desktop,
)
c.managers.desktop.Start()
c.managers.capture = capture.New(
c.managers.desktop,
&c.configs.Capture,
)
c.managers.capture.Start()
c.managers.webRTC = webrtc.New(
c.managers.desktop,
c.managers.capture,
&c.configs.WebRTC,
)
c.managers.webRTC.Start()
c.managers.webSocket = websocket.New(
c.managers.session,
c.managers.desktop,
c.managers.capture,
c.managers.webRTC,
)
c.managers.webSocket.Start()
c.managers.api = api.New(
c.managers.session,
c.managers.member,
c.managers.desktop,
c.managers.capture,
&c.configs.Server,
) )
modules.Start( modules.Start(
neko.sessionManager, c.managers.session,
neko.webSocketManager, c.managers.webSocket,
neko.apiManager, c.managers.api,
) )
neko.httpManager = http.New( c.managers.http = http.New(
neko.webSocketManager, c.managers.webSocket,
neko.apiManager, c.managers.api,
neko.Configs.Server, &c.configs.Server,
) )
neko.httpManager.Start() c.managers.http.Start()
} }
func (neko *Serve) Shutdown() { func (c *serve) Shutdown() {
var err error var err error
err = neko.memberManager.Disconnect() err = c.managers.member.Disconnect()
neko.logger.Err(err).Msg("member manager disconnect") c.logger.Err(err).Msg("member manager disconnect")
err = neko.desktopManager.Shutdown() err = c.managers.desktop.Shutdown()
neko.logger.Err(err).Msg("desktop manager shutdown") c.logger.Err(err).Msg("desktop manager shutdown")
err = neko.captureManager.Shutdown() err = c.managers.capture.Shutdown()
neko.logger.Err(err).Msg("capture manager shutdown") c.logger.Err(err).Msg("capture manager shutdown")
err = neko.webRTCManager.Shutdown() err = c.managers.webRTC.Shutdown()
neko.logger.Err(err).Msg("webrtc manager shutdown") c.logger.Err(err).Msg("webrtc manager shutdown")
err = neko.webSocketManager.Shutdown() err = c.managers.webSocket.Shutdown()
neko.logger.Err(err).Msg("websocket manager shutdown") c.logger.Err(err).Msg("websocket manager shutdown")
err = modules.Shutdown() err = modules.Shutdown()
neko.logger.Err(err).Msg("modules shutdown") c.logger.Err(err).Msg("modules shutdown")
err = neko.httpManager.Shutdown() err = c.managers.http.Shutdown()
neko.logger.Err(err).Msg("http manager shutdown") c.logger.Err(err).Msg("http manager shutdown")
} }
func (neko *Serve) ServeCommand(cmd *cobra.Command, args []string) { func (c *serve) Command(cmd *cobra.Command, args []string) {
neko.logger.Info().Msg("starting neko server") c.logger.Info().Msg("starting neko server")
neko.Start() c.Start()
neko.logger.Info().Msg("neko ready") c.logger.Info().Msg("neko ready")
quit := make(chan os.Signal, 1) quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt) signal.Notify(quit, os.Interrupt)
sig := <-quit sig := <-quit
neko.logger.Warn().Msgf("received %s, attempting graceful shutdown", sig) c.logger.Warn().Msgf("received %s, attempting graceful shutdown", sig)
neko.Shutdown() c.Shutdown()
neko.logger.Info().Msg("shutdown complete") c.logger.Info().Msg("shutdown complete")
} }

40
neko.go
View File

@ -3,8 +3,6 @@ package neko
import ( import (
"fmt" "fmt"
"runtime" "runtime"
"demodesk/neko/internal/config"
) )
const Header = `&34 const Header = `&34
@ -32,11 +30,7 @@ var (
patch = "dev" patch = "dev"
) )
var Service *Neko var Version = &version{
func init() {
Service = &Neko{
Version: &Version{
Major: major, Major: major,
Minor: minor, Minor: minor,
Patch: patch, Patch: patch,
@ -46,20 +40,9 @@ func init() {
GoVersion: runtime.Version(), GoVersion: runtime.Version(),
Compiler: runtime.Compiler, Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
},
Configs: &Configs{
Root: &config.Root{},
Desktop: &config.Desktop{},
Capture: &config.Capture{},
WebRTC: &config.WebRTC{},
Member: &config.Member{},
Session: &config.Session{},
Server: &config.Server{},
},
}
} }
type Version struct { type version struct {
Major string Major string
Minor string Minor string
Patch string Patch string
@ -71,11 +54,11 @@ type Version struct {
Platform string Platform string
} }
func (i *Version) String() string { func (i *version) String() string {
return fmt.Sprintf("%s.%s.%s %s", i.Major, i.Minor, i.Patch, i.GitCommit) return fmt.Sprintf("%s.%s.%s %s", i.Major, i.Minor, i.Patch, i.GitCommit)
} }
func (i *Version) Details() string { func (i *version) Details() string {
return fmt.Sprintf( return fmt.Sprintf(
"%s\n%s\n%s\n%s\n%s\n%s\n%s\n", "%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("Version %s.%s.%s", i.Major, i.Minor, i.Patch),
@ -87,18 +70,3 @@ func (i *Version) Details() string {
fmt.Sprintf("Platform %s", i.Platform), fmt.Sprintf("Platform %s", i.Platform),
) )
} }
type Configs struct {
Root *config.Root
Desktop *config.Desktop
Capture *config.Capture
WebRTC *config.WebRTC
Member *config.Member
Session *config.Session
Server *config.Server
}
type Neko struct {
Version *Version
Configs *Configs
}