neko/server/neko.go

165 lines
3.8 KiB
Go
Raw Normal View History

2020-01-13 21:05:38 +13:00
package neko
import (
"fmt"
"os"
"os/signal"
"runtime"
2020-01-19 12:30:09 +13:00
"n.eko.moe/neko/internal/http"
"n.eko.moe/neko/internal/session"
"n.eko.moe/neko/internal/types/config"
2020-01-14 12:12:55 +13:00
"n.eko.moe/neko/internal/webrtc"
2020-01-19 12:30:09 +13:00
"n.eko.moe/neko/internal/websocket"
2020-01-13 21:05:38 +13:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
2020-01-19 12:30:09 +13:00
const Header = `&34
_ __ __
/ | / /__ / /______ \ /\
/ |/ / _ \/ //_/ __ \ ) ( ')
/ /| / __/ ,< / /_/ / ( / )
/_/ |_/\___/_/|_|\____/ \(__)|
&1&37 nurdism/neko &33%s v%s&0
`
2020-01-13 21:05:38 +13:00
var (
//
2020-01-26 23:43:08 +13:00
buildDate = "dev"
2020-01-13 21:05:38 +13:00
//
2020-01-26 23:43:08 +13:00
gitCommit = "dev"
2020-01-13 21:05:38 +13:00
//
2020-01-26 23:43:08 +13:00
gitBranch = "dev"
2020-01-13 21:05:38 +13:00
// Major version when you make incompatible API changes,
2020-04-05 16:10:18 +12:00
major = "2"
2020-01-13 21:05:38 +13:00
// Minor version when you add functionality in a backwards-compatible manner, and
2020-04-05 16:10:18 +12:00
minor = "0"
2020-01-13 21:05:38 +13:00
// Patch version when you make backwards-compatible bug fixeneko.
patch = "0"
)
var Service *Neko
func init() {
Service = &Neko{
2020-01-19 12:30:09 +13:00
Version: &Version{
2020-01-26 23:43:08 +13:00
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-01-13 21:05:38 +13:00
},
2020-01-19 12:30:09 +13:00
Root: &config.Root{},
Server: &config.Server{},
WebRTC: &config.WebRTC{},
WebSocket: &config.WebSocket{},
2020-01-13 21:05:38 +13:00
}
}
2020-01-19 12:30:09 +13:00
type Version struct {
2020-01-26 23:43:08 +13:00
Major string
Minor string
Patch string
GitCommit string
GitBranch string
BuildDate string
GoVersion string
Compiler string
Platform string
2020-01-19 12:30:09 +13:00
}
func (i *Version) String() string {
2020-01-26 23:43:08 +13:00
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),
2020-01-26 23:43:08 +13:00
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-01-19 12:30:09 +13:00
}
2020-01-13 21:05:38 +13:00
type Neko struct {
2020-01-19 12:30:09 +13:00
Version *Version
Root *config.Root
Server *config.Server
WebRTC *config.WebRTC
WebSocket *config.WebSocket
logger zerolog.Logger
server *http.Server
sessions *session.SessionManager
webRTCManager *webrtc.WebRTCManager
webSocketHandler *websocket.WebSocketHandler
2020-01-13 21:05:38 +13:00
}
func (neko *Neko) Preflight() {
2020-01-19 12:30:09 +13:00
neko.logger = log.With().Str("service", "neko").Logger()
2020-01-13 21:05:38 +13:00
}
func (neko *Neko) Start() {
2020-01-19 12:30:09 +13:00
sessions := session.New()
2020-01-14 12:12:55 +13:00
2020-01-19 12:30:09 +13:00
webRTCManager := webrtc.New(sessions, neko.WebRTC)
webRTCManager.Start()
2020-01-14 21:36:48 +13:00
2020-01-19 12:30:09 +13:00
webSocketHandler := websocket.New(sessions, webRTCManager, neko.WebSocket)
webSocketHandler.Start()
2020-01-14 12:12:55 +13:00
2020-01-19 12:30:09 +13:00
server := http.New(neko.Server, webSocketHandler)
server.Start()
2020-01-13 21:05:38 +13:00
2020-01-19 12:30:09 +13:00
neko.sessions = sessions
neko.webRTCManager = webRTCManager
neko.webSocketHandler = webSocketHandler
neko.server = server
2020-01-13 21:05:38 +13:00
}
func (neko *Neko) Shutdown() {
2020-01-19 12:30:09 +13:00
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-01-14 21:36:48 +13:00
}
2020-01-19 12:30:09 +13:00
if err := neko.webSocketHandler.Shutdown(); err != nil {
neko.logger.Err(err).Msg("websocket handler shutdown with an error")
} else {
neko.logger.Debug().Msg("websocket handler shutdown")
}
if err := neko.server.Shutdown(); err != nil {
neko.logger.Err(err).Msg("server shutdown with an error")
} else {
neko.logger.Debug().Msg("server shutdown")
2020-01-13 21:05:38 +13:00
}
}
func (neko *Neko) ServeCommand(cmd *cobra.Command, args []string) {
2020-01-19 12:30:09 +13:00
neko.logger.Info().Msg("starting neko server")
2020-01-13 21:05:38 +13:00
neko.Start()
2020-01-19 12:30:09 +13:00
neko.logger.Info().Msg("neko ready")
2020-01-13 21:05:38 +13:00
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
sig := <-quit
2020-01-19 12:30:09 +13:00
neko.logger.Warn().Msgf("received %s, attempting graceful shutdown: \n", sig)
2020-01-13 21:05:38 +13:00
neko.Shutdown()
2020-01-19 12:30:09 +13:00
neko.logger.Info().Msg("shutdown complete")
2020-01-13 21:05:38 +13:00
}