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 configs struct {
webRTCManager *webrtc.WebRTCManagerCtx Root config.Root
memberManager *member.MemberManagerCtx Desktop config.Desktop
sessionManager *session.SessionManagerCtx Capture config.Capture
webSocketManager *websocket.WebSocketManagerCtx WebRTC config.WebRTC
apiManager *api.ApiManagerCtx Member config.Member
httpManager *http.HttpManagerCtx Session config.Session
Server config.Server
} }
func (neko *Serve) Preflight() { managers struct {
neko.logger = log.With().Str("service", "neko").Logger() desktop *desktop.DesktopManagerCtx
capture *capture.CaptureManagerCtx
webRTC *webrtc.WebRTCManagerCtx
member *member.MemberManagerCtx
session *session.SessionManagerCtx
webSocket *websocket.WebSocketManagerCtx
api *api.ApiManagerCtx
http *http.HttpManagerCtx
}
} }
func (neko *Serve) Start() { func (c *serve) Init(cmd *cobra.Command) error {
neko.sessionManager = session.New( if err := c.configs.Desktop.Init(cmd); err != nil {
neko.Configs.Session, return err
) }
if err := c.configs.Capture.Init(cmd); err != nil {
neko.memberManager = member.New( return err
neko.sessionManager, }
neko.Configs.Member, if err := c.configs.WebRTC.Init(cmd); err != nil {
) return err
}
if err := neko.memberManager.Connect(); err != nil { if err := c.configs.Member.Init(cmd); err != nil {
neko.logger.Panic().Err(err).Msg("unable to connect to member manager") 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.desktopManager = desktop.New( for _, cfg := range modules.Configs() {
neko.Configs.Desktop, if err := cfg.Init(root); err != nil {
) log.Panic().Err(err).Msg("unable to initialize configuration")
neko.desktopManager.Start() }
}
neko.captureManager = capture.New( return nil
neko.desktopManager, }
neko.Configs.Capture,
)
neko.captureManager.Start()
neko.webRTCManager = webrtc.New( func (c *serve) Preflight() {
neko.desktopManager, c.logger = log.With().Str("service", "neko").Logger()
neko.captureManager,
neko.Configs.WebRTC,
)
neko.webRTCManager.Start()
neko.webSocketManager = websocket.New( c.configs.Desktop.Set()
neko.sessionManager, c.configs.Capture.Set()
neko.desktopManager, c.configs.WebRTC.Set()
neko.captureManager, c.configs.Member.Set()
neko.webRTCManager, c.configs.Session.Set()
) c.configs.Server.Set()
neko.webSocketManager.Start()
neko.apiManager = api.New( for _, cfg := range modules.Configs() {
neko.sessionManager, cfg.Set()
neko.memberManager, }
neko.desktopManager, }
neko.captureManager,
neko.Configs.Server, 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
}