2020-10-23 03:54:50 +13:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-01-14 12:21:56 +13:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
2020-10-23 03:54:50 +13:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2022-07-14 10:58:22 +12:00
|
|
|
"github.com/demodesk/neko/internal/api"
|
|
|
|
"github.com/demodesk/neko/internal/capture"
|
|
|
|
"github.com/demodesk/neko/internal/config"
|
|
|
|
"github.com/demodesk/neko/internal/desktop"
|
|
|
|
"github.com/demodesk/neko/internal/http"
|
|
|
|
"github.com/demodesk/neko/internal/member"
|
|
|
|
"github.com/demodesk/neko/internal/plugins"
|
|
|
|
"github.com/demodesk/neko/internal/session"
|
|
|
|
"github.com/demodesk/neko/internal/webrtc"
|
|
|
|
"github.com/demodesk/neko/internal/websocket"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-01-17 08:35:57 +13:00
|
|
|
service := serve{}
|
2022-01-14 12:21:56 +13:00
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
command := &cobra.Command{
|
2023-03-31 23:02:33 +13:00
|
|
|
Use: "serve",
|
|
|
|
Short: "serve neko streaming server",
|
|
|
|
Long: `serve neko streaming server`,
|
|
|
|
PreRun: service.PreRun,
|
|
|
|
Run: service.Run,
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
if err := service.Init(command); err != nil {
|
|
|
|
log.Panic().Err(err).Msg("unable to initialize configuration")
|
|
|
|
}
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
root.AddCommand(command)
|
|
|
|
}
|
2022-01-14 12:21:56 +13:00
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
type serve struct {
|
|
|
|
logger zerolog.Logger
|
|
|
|
|
|
|
|
configs struct {
|
|
|
|
Desktop config.Desktop
|
|
|
|
Capture config.Capture
|
|
|
|
WebRTC config.WebRTC
|
|
|
|
Member config.Member
|
|
|
|
Session config.Session
|
2022-04-16 07:28:00 +12:00
|
|
|
Plugins config.Plugins
|
2022-01-17 08:35:57 +13:00
|
|
|
Server config.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
managers struct {
|
|
|
|
desktop *desktop.DesktopManagerCtx
|
|
|
|
capture *capture.CaptureManagerCtx
|
|
|
|
webRTC *webrtc.WebRTCManagerCtx
|
|
|
|
member *member.MemberManagerCtx
|
|
|
|
session *session.SessionManagerCtx
|
|
|
|
webSocket *websocket.WebSocketManagerCtx
|
2022-04-19 22:14:59 +12:00
|
|
|
plugins *plugins.ManagerCtx
|
2022-01-17 08:35:57 +13:00
|
|
|
api *api.ApiManagerCtx
|
|
|
|
http *http.HttpManagerCtx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serve) Init(cmd *cobra.Command) error {
|
|
|
|
if err := c.configs.Desktop.Init(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2022-04-16 07:28:00 +12:00
|
|
|
if err := c.configs.Plugins.Init(cmd); err != nil {
|
2022-01-17 08:35:57 +13:00
|
|
|
return err
|
|
|
|
}
|
2022-04-16 07:28:00 +12:00
|
|
|
if err := c.configs.Server.Init(cmd); err != nil {
|
|
|
|
return err
|
2022-01-17 08:35:57 +13:00
|
|
|
}
|
|
|
|
|
2024-07-19 07:48:09 +12:00
|
|
|
// V2 configuration
|
|
|
|
|
|
|
|
if err := c.configs.Desktop.InitV2(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.configs.Capture.InitV2(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.configs.WebRTC.InitV2(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.configs.Member.InitV2(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.configs.Session.InitV2(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.configs.Server.InitV2(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
return nil
|
2022-01-14 12:21:56 +13:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:02:33 +13:00
|
|
|
func (c *serve) PreRun(cmd *cobra.Command, args []string) {
|
2022-01-17 08:35:57 +13:00
|
|
|
c.logger = log.With().Str("service", "neko").Logger()
|
|
|
|
|
|
|
|
c.configs.Desktop.Set()
|
|
|
|
c.configs.Capture.Set()
|
|
|
|
c.configs.WebRTC.Set()
|
|
|
|
c.configs.Member.Set()
|
|
|
|
c.configs.Session.Set()
|
2022-04-16 07:28:00 +12:00
|
|
|
c.configs.Plugins.Set()
|
2022-01-17 08:35:57 +13:00
|
|
|
c.configs.Server.Set()
|
2024-07-19 07:48:09 +12:00
|
|
|
|
|
|
|
c.configs.Desktop.SetV2()
|
|
|
|
c.configs.Capture.SetV2()
|
|
|
|
c.configs.WebRTC.SetV2()
|
|
|
|
c.configs.Member.SetV2()
|
|
|
|
c.configs.Session.SetV2()
|
|
|
|
c.configs.Server.SetV2()
|
2022-01-14 12:21:56 +13:00
|
|
|
}
|
|
|
|
|
2022-04-16 07:28:00 +12:00
|
|
|
func (c *serve) Start(cmd *cobra.Command) {
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.session = session.New(
|
|
|
|
&c.configs.Session,
|
2022-01-14 12:21:56 +13:00
|
|
|
)
|
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.member = member.New(
|
|
|
|
c.managers.session,
|
|
|
|
&c.configs.Member,
|
2022-01-14 12:21:56 +13:00
|
|
|
)
|
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
if err := c.managers.member.Connect(); err != nil {
|
|
|
|
c.logger.Panic().Err(err).Msg("unable to connect to member manager")
|
2022-01-14 12:21:56 +13:00
|
|
|
}
|
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.desktop = desktop.New(
|
|
|
|
&c.configs.Desktop,
|
2022-01-14 12:21:56 +13:00
|
|
|
)
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.desktop.Start()
|
2022-01-14 12:21:56 +13:00
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.capture = capture.New(
|
|
|
|
c.managers.desktop,
|
|
|
|
&c.configs.Capture,
|
2022-01-14 12:21:56 +13:00
|
|
|
)
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.capture.Start()
|
2022-01-14 12:21:56 +13:00
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.webRTC = webrtc.New(
|
|
|
|
c.managers.desktop,
|
|
|
|
c.managers.capture,
|
|
|
|
&c.configs.WebRTC,
|
2022-01-14 12:21:56 +13:00
|
|
|
)
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.webRTC.Start()
|
2022-01-14 12:21:56 +13:00
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.webSocket = websocket.New(
|
|
|
|
c.managers.session,
|
|
|
|
c.managers.desktop,
|
|
|
|
c.managers.capture,
|
|
|
|
c.managers.webRTC,
|
2022-01-14 12:21:56 +13:00
|
|
|
)
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.webSocket.Start()
|
|
|
|
|
|
|
|
c.managers.api = api.New(
|
|
|
|
c.managers.session,
|
|
|
|
c.managers.member,
|
|
|
|
c.managers.desktop,
|
|
|
|
c.managers.capture,
|
2022-01-14 12:21:56 +13:00
|
|
|
)
|
|
|
|
|
2022-04-16 07:28:00 +12:00
|
|
|
c.managers.plugins = plugins.New(
|
|
|
|
&c.configs.Plugins,
|
|
|
|
)
|
|
|
|
|
|
|
|
// init and set configuration now
|
|
|
|
// this means it won't be in --help
|
|
|
|
c.managers.plugins.InitConfigs(cmd)
|
|
|
|
c.managers.plugins.SetConfigs()
|
|
|
|
|
|
|
|
c.managers.plugins.Start(
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.session,
|
|
|
|
c.managers.webSocket,
|
|
|
|
c.managers.api,
|
2022-01-14 12:21:56 +13:00
|
|
|
)
|
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.http = http.New(
|
|
|
|
c.managers.webSocket,
|
|
|
|
c.managers.api,
|
|
|
|
&c.configs.Server,
|
2022-01-14 12:21:56 +13:00
|
|
|
)
|
2022-01-17 08:35:57 +13:00
|
|
|
c.managers.http.Start()
|
2022-01-14 12:21:56 +13:00
|
|
|
}
|
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
func (c *serve) Shutdown() {
|
2022-01-14 12:21:56 +13:00
|
|
|
var err error
|
|
|
|
|
2022-07-28 22:19:58 +12:00
|
|
|
err = c.managers.http.Shutdown()
|
|
|
|
c.logger.Err(err).Msg("http manager shutdown")
|
2022-01-14 12:21:56 +13:00
|
|
|
|
2022-07-28 22:19:58 +12:00
|
|
|
err = c.managers.plugins.Shutdown()
|
|
|
|
c.logger.Err(err).Msg("plugins manager shutdown")
|
2022-01-14 12:21:56 +13:00
|
|
|
|
2022-07-28 22:19:58 +12:00
|
|
|
err = c.managers.webSocket.Shutdown()
|
|
|
|
c.logger.Err(err).Msg("websocket manager shutdown")
|
2022-01-14 12:21:56 +13:00
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
err = c.managers.webRTC.Shutdown()
|
|
|
|
c.logger.Err(err).Msg("webrtc manager shutdown")
|
2022-01-14 12:21:56 +13:00
|
|
|
|
2022-07-28 22:19:58 +12:00
|
|
|
err = c.managers.capture.Shutdown()
|
|
|
|
c.logger.Err(err).Msg("capture manager shutdown")
|
2022-01-14 12:21:56 +13:00
|
|
|
|
2022-07-28 22:19:58 +12:00
|
|
|
err = c.managers.desktop.Shutdown()
|
|
|
|
c.logger.Err(err).Msg("desktop manager shutdown")
|
2022-01-14 12:21:56 +13:00
|
|
|
|
2022-07-28 22:19:58 +12:00
|
|
|
err = c.managers.member.Disconnect()
|
|
|
|
c.logger.Err(err).Msg("member manager disconnect")
|
2022-01-14 12:21:56 +13:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:02:33 +13:00
|
|
|
func (c *serve) Run(cmd *cobra.Command, args []string) {
|
2022-01-17 08:35:57 +13:00
|
|
|
c.logger.Info().Msg("starting neko server")
|
2022-04-16 07:28:00 +12:00
|
|
|
c.Start(cmd)
|
2022-01-17 08:35:57 +13:00
|
|
|
c.logger.Info().Msg("neko ready")
|
2022-01-14 12:21:56 +13:00
|
|
|
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, os.Interrupt)
|
|
|
|
sig := <-quit
|
|
|
|
|
2022-01-17 08:35:57 +13:00
|
|
|
c.logger.Warn().Msgf("received %s, attempting graceful shutdown", sig)
|
|
|
|
c.Shutdown()
|
|
|
|
c.logger.Info().Msg("shutdown complete")
|
2022-01-14 12:21:56 +13:00
|
|
|
}
|