mirror of
https://github.com/m1k1o/neko-rooms.git
synced 2026-11-02 22:51:34 -06:00
refactor server manager. (#57)
This commit is contained in:
@@ -10,6 +10,8 @@ type Server struct {
|
|||||||
Key string
|
Key string
|
||||||
Bind string
|
Bind string
|
||||||
Static string
|
Static string
|
||||||
|
Proxy bool
|
||||||
|
PProf bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Server) Init(cmd *cobra.Command) error {
|
func (Server) Init(cmd *cobra.Command) error {
|
||||||
@@ -33,6 +35,16 @@ func (Server) Init(cmd *cobra.Command) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cmd.PersistentFlags().Bool("proxy", false, "trust reverse proxy headers")
|
||||||
|
if err := viper.BindPFlag("proxy", cmd.PersistentFlags().Lookup("proxy")); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.PersistentFlags().Bool("pprof", false, "enable pprof endpoint available at /debug/pprof")
|
||||||
|
if err := viper.BindPFlag("pprof", cmd.PersistentFlags().Lookup("pprof")); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,4 +53,6 @@ func (s *Server) Set() {
|
|||||||
s.Key = viper.GetString("key")
|
s.Key = viper.GetString("key")
|
||||||
s.Bind = viper.GetString("bind")
|
s.Bind = viper.GetString("bind")
|
||||||
s.Static = viper.GetString("static")
|
s.Static = viper.GetString("static")
|
||||||
|
s.Proxy = viper.GetBool("proxy")
|
||||||
|
s.PProf = viper.GetBool("pprof")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,80 +0,0 @@
|
|||||||
package http
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-chi/chi/middleware"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Logger(next http.Handler) http.Handler {
|
|
||||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
req := map[string]interface{}{}
|
|
||||||
|
|
||||||
if reqID := middleware.GetReqID(r.Context()); reqID != "" {
|
|
||||||
req["id"] = reqID
|
|
||||||
}
|
|
||||||
|
|
||||||
scheme := "http"
|
|
||||||
if r.TLS != nil {
|
|
||||||
scheme = "https"
|
|
||||||
}
|
|
||||||
|
|
||||||
req["scheme"] = scheme
|
|
||||||
req["proto"] = r.Proto
|
|
||||||
req["method"] = r.Method
|
|
||||||
req["remote"] = r.RemoteAddr
|
|
||||||
req["agent"] = r.UserAgent()
|
|
||||||
req["uri"] = fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)
|
|
||||||
|
|
||||||
fields := map[string]interface{}{}
|
|
||||||
fields["req"] = req
|
|
||||||
|
|
||||||
entry := &entry{
|
|
||||||
fields: fields,
|
|
||||||
}
|
|
||||||
|
|
||||||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
|
||||||
t1 := time.Now()
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
entry.Write(ww.Status(), ww.BytesWritten(), time.Since(t1))
|
|
||||||
}()
|
|
||||||
|
|
||||||
next.ServeHTTP(ww, r)
|
|
||||||
}
|
|
||||||
return http.HandlerFunc(fn)
|
|
||||||
}
|
|
||||||
|
|
||||||
type entry struct {
|
|
||||||
fields map[string]interface{}
|
|
||||||
errors []map[string]interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *entry) Write(status, bytes int, elapsed time.Duration) {
|
|
||||||
res := map[string]interface{}{}
|
|
||||||
res["time"] = time.Now().UTC().Format(time.RFC1123)
|
|
||||||
res["status"] = status
|
|
||||||
res["bytes"] = bytes
|
|
||||||
res["elapsed"] = float64(elapsed.Nanoseconds()) / 1000000.0
|
|
||||||
|
|
||||||
e.fields["res"] = res
|
|
||||||
e.fields["module"] = "http"
|
|
||||||
|
|
||||||
if len(e.errors) > 0 {
|
|
||||||
e.fields["errors"] = e.errors
|
|
||||||
log.Error().Fields(e.fields).Msgf("request failed (%d)", status)
|
|
||||||
} else {
|
|
||||||
log.Debug().Fields(e.fields).Msgf("request complete (%d)", status)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *entry) Panic(v interface{}, stack []byte) {
|
|
||||||
err := map[string]interface{}{}
|
|
||||||
err["message"] = fmt.Sprintf("%+v", v)
|
|
||||||
err["stack"] = string(stack)
|
|
||||||
|
|
||||||
e.errors = append(e.errors, err)
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/pprof"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
)
|
||||||
|
|
||||||
|
const pprofPath = "/debug/pprof/"
|
||||||
|
|
||||||
|
func withPProf(router *chi.Mux) {
|
||||||
|
router.Route(pprofPath, func(r chi.Router) {
|
||||||
|
r.Get("/", pprof.Index)
|
||||||
|
|
||||||
|
r.Get("/{action}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
action := chi.URLParam(r, "action")
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case "cmdline":
|
||||||
|
pprof.Cmdline(w, r)
|
||||||
|
case "profile":
|
||||||
|
pprof.Profile(w, r)
|
||||||
|
case "symbol":
|
||||||
|
pprof.Symbol(w, r)
|
||||||
|
case "trace":
|
||||||
|
pprof.Trace(w, r)
|
||||||
|
default:
|
||||||
|
pprof.Handler(action).ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/middleware"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
type logformatter struct {
|
||||||
|
logger zerolog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logformatter) NewLogEntry(r *http.Request) middleware.LogEntry {
|
||||||
|
req := map[string]interface{}{}
|
||||||
|
|
||||||
|
if reqID := middleware.GetReqID(r.Context()); reqID != "" {
|
||||||
|
req["id"] = reqID
|
||||||
|
}
|
||||||
|
|
||||||
|
scheme := "http"
|
||||||
|
if r.TLS != nil {
|
||||||
|
scheme = "https"
|
||||||
|
}
|
||||||
|
|
||||||
|
req["scheme"] = scheme
|
||||||
|
req["proto"] = r.Proto
|
||||||
|
req["method"] = r.Method
|
||||||
|
req["remote"] = r.RemoteAddr
|
||||||
|
req["agent"] = r.UserAgent()
|
||||||
|
req["uri"] = fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)
|
||||||
|
|
||||||
|
fields := map[string]interface{}{}
|
||||||
|
fields["req"] = req
|
||||||
|
|
||||||
|
return &logentry{
|
||||||
|
fields: fields,
|
||||||
|
logger: l.logger,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type logentry struct {
|
||||||
|
logger zerolog.Logger
|
||||||
|
fields map[string]interface{}
|
||||||
|
errors []map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *logentry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
|
||||||
|
res := map[string]interface{}{}
|
||||||
|
res["time"] = time.Now().UTC().Format(time.RFC1123)
|
||||||
|
res["status"] = status
|
||||||
|
res["bytes"] = bytes
|
||||||
|
res["elapsed"] = float64(elapsed.Nanoseconds()) / 1000000.0
|
||||||
|
|
||||||
|
e.fields["res"] = res
|
||||||
|
e.fields["module"] = "http"
|
||||||
|
|
||||||
|
if len(e.errors) > 0 {
|
||||||
|
e.fields["errors"] = e.errors
|
||||||
|
e.logger.Error().Fields(e.fields).Msgf("request failed (%d)", status)
|
||||||
|
} else {
|
||||||
|
e.logger.Debug().Fields(e.fields).Msgf("request complete (%d)", status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *logentry) Panic(v interface{}, stack []byte) {
|
||||||
|
err := map[string]interface{}{}
|
||||||
|
err["message"] = fmt.Sprintf("%+v", v)
|
||||||
|
err["stack"] = string(stack)
|
||||||
|
|
||||||
|
e.errors = append(e.errors, err)
|
||||||
|
}
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
package http
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/go-chi/chi/middleware"
|
"github.com/go-chi/chi/middleware"
|
||||||
@@ -16,20 +17,27 @@ import (
|
|||||||
"github.com/m1k1o/neko-rooms/internal/types"
|
"github.com/m1k1o/neko-rooms/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HttpManagerCtx struct {
|
type ServerManagerCtx struct {
|
||||||
logger zerolog.Logger
|
logger zerolog.Logger
|
||||||
router *chi.Mux
|
router *chi.Mux
|
||||||
http *http.Server
|
server *http.Server
|
||||||
conf *config.Server
|
config *config.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(ApiManager types.ApiManager, pathPrefix string, conf *config.Server) *HttpManagerCtx {
|
func New(ApiManager types.ApiManager, pathPrefix string, config *config.Server) *ServerManagerCtx {
|
||||||
logger := log.With().Str("module", "http").Logger()
|
logger := log.With().Str("module", "server").Logger()
|
||||||
|
|
||||||
router := chi.NewRouter()
|
router := chi.NewRouter()
|
||||||
router.Use(middleware.Recoverer) // Recover from panics without crashing server
|
|
||||||
router.Use(middleware.RequestID) // Create a request ID for each request
|
router.Use(middleware.RequestID) // Create a request ID for each request
|
||||||
router.Use(Logger) // Log API request calls using custom logger function
|
|
||||||
|
// get real users ip
|
||||||
|
if config.Proxy {
|
||||||
|
router.Use(middleware.RealIP)
|
||||||
|
}
|
||||||
|
|
||||||
|
// add http logger
|
||||||
|
router.Use(middleware.RequestLogger(&logformatter{logger}))
|
||||||
|
router.Use(middleware.Recoverer) // Recover from panics without crashing server
|
||||||
|
|
||||||
// Basic CORS
|
// Basic CORS
|
||||||
// for more ideas, see: https://developer.github.com/v3/#cross-origin-resource-sharing
|
// for more ideas, see: https://developer.github.com/v3/#cross-origin-resource-sharing
|
||||||
@@ -43,10 +51,11 @@ func New(ApiManager types.ApiManager, pathPrefix string, conf *config.Server) *H
|
|||||||
|
|
||||||
router.Route("/api", ApiManager.Mount)
|
router.Route("/api", ApiManager.Mount)
|
||||||
|
|
||||||
if conf.Static != "" {
|
// serve static files
|
||||||
fs := http.FileServer(http.Dir(conf.Static))
|
if config.Static != "" {
|
||||||
|
fs := http.FileServer(http.Dir(config.Static))
|
||||||
router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if _, err := os.Stat(conf.Static + r.URL.Path); !os.IsNotExist(err) {
|
if _, err := os.Stat(config.Static + r.URL.Path); !os.IsNotExist(err) {
|
||||||
fs.ServeHTTP(w, r)
|
fs.ServeHTTP(w, r)
|
||||||
} else {
|
} else {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
@@ -58,39 +67,47 @@ func New(ApiManager types.ApiManager, pathPrefix string, conf *config.Server) *H
|
|||||||
router.Get(path.Join("/", pathPrefix, "{roomName}"), ApiManager.RoomLobby)
|
router.Get(path.Join("/", pathPrefix, "{roomName}"), ApiManager.RoomLobby)
|
||||||
router.Get(path.Join("/", pathPrefix, "{roomName}")+"/", ApiManager.RoomLobby)
|
router.Get(path.Join("/", pathPrefix, "{roomName}")+"/", ApiManager.RoomLobby)
|
||||||
|
|
||||||
router.NotFound(http.HandlerFunc(http.NotFound))
|
// mount pprof endpoint
|
||||||
|
if config.PProf {
|
||||||
http := &http.Server{
|
withPProf(router)
|
||||||
Addr: conf.Bind,
|
logger.Info().Msgf("with pprof endpoint at %s", pprofPath)
|
||||||
Handler: router,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &HttpManagerCtx{
|
// we could use custom 404
|
||||||
|
router.NotFound(http.NotFound)
|
||||||
|
|
||||||
|
return &ServerManagerCtx{
|
||||||
logger: logger,
|
logger: logger,
|
||||||
router: router,
|
router: router,
|
||||||
http: http,
|
server: &http.Server{
|
||||||
conf: conf,
|
Addr: config.Bind,
|
||||||
|
Handler: router,
|
||||||
|
},
|
||||||
|
config: config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HttpManagerCtx) Start() {
|
func (s *ServerManagerCtx) Start() {
|
||||||
if s.conf.Cert != "" && s.conf.Key != "" {
|
if s.config.Cert != "" && s.config.Key != "" {
|
||||||
go func() {
|
go func() {
|
||||||
if err := s.http.ListenAndServeTLS(s.conf.Cert, s.conf.Key); err != http.ErrServerClosed {
|
if err := s.server.ListenAndServeTLS(s.config.Cert, s.config.Key); err != http.ErrServerClosed {
|
||||||
s.logger.Panic().Err(err).Msg("unable to start https server")
|
s.logger.Panic().Err(err).Msg("unable to start https server")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
s.logger.Info().Msgf("https listening on %s", s.http.Addr)
|
s.logger.Info().Msgf("https listening on %s", s.server.Addr)
|
||||||
} else {
|
} else {
|
||||||
go func() {
|
go func() {
|
||||||
if err := s.http.ListenAndServe(); err != http.ErrServerClosed {
|
if err := s.server.ListenAndServe(); err != http.ErrServerClosed {
|
||||||
s.logger.Panic().Err(err).Msg("unable to start http server")
|
s.logger.Panic().Err(err).Msg("unable to start http server")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
s.logger.Info().Msgf("http listening on %s", s.http.Addr)
|
s.logger.Info().Msgf("http listening on %s", s.server.Addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HttpManagerCtx) Shutdown() error {
|
func (s *ServerManagerCtx) Shutdown() error {
|
||||||
return s.http.Shutdown(context.Background())
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
return s.server.Shutdown(ctx)
|
||||||
}
|
}
|
||||||
@@ -13,9 +13,9 @@ import (
|
|||||||
|
|
||||||
"github.com/m1k1o/neko-rooms/internal/api"
|
"github.com/m1k1o/neko-rooms/internal/api"
|
||||||
"github.com/m1k1o/neko-rooms/internal/config"
|
"github.com/m1k1o/neko-rooms/internal/config"
|
||||||
"github.com/m1k1o/neko-rooms/internal/http"
|
|
||||||
"github.com/m1k1o/neko-rooms/internal/pull"
|
"github.com/m1k1o/neko-rooms/internal/pull"
|
||||||
"github.com/m1k1o/neko-rooms/internal/room"
|
"github.com/m1k1o/neko-rooms/internal/room"
|
||||||
|
"github.com/m1k1o/neko-rooms/internal/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
const Header = `&34
|
const Header = `&34
|
||||||
@@ -108,11 +108,11 @@ type MainCtx struct {
|
|||||||
Version *Version
|
Version *Version
|
||||||
Configs *Configs
|
Configs *Configs
|
||||||
|
|
||||||
logger zerolog.Logger
|
logger zerolog.Logger
|
||||||
roomManager *room.RoomManagerCtx
|
roomManager *room.RoomManagerCtx
|
||||||
pullManager *pull.PullManagerCtx
|
pullManager *pull.PullManagerCtx
|
||||||
apiManager *api.ApiManagerCtx
|
apiManager *api.ApiManagerCtx
|
||||||
httpManager *http.HttpManagerCtx
|
serverManager *server.ServerManagerCtx
|
||||||
}
|
}
|
||||||
|
|
||||||
func (main *MainCtx) Preflight() {
|
func (main *MainCtx) Preflight() {
|
||||||
@@ -143,17 +143,17 @@ func (main *MainCtx) Start() {
|
|||||||
main.Configs.API,
|
main.Configs.API,
|
||||||
)
|
)
|
||||||
|
|
||||||
main.httpManager = http.New(
|
main.serverManager = server.New(
|
||||||
main.apiManager,
|
main.apiManager,
|
||||||
main.Configs.Room.PathPrefix,
|
main.Configs.Room.PathPrefix,
|
||||||
main.Configs.Server,
|
main.Configs.Server,
|
||||||
)
|
)
|
||||||
main.httpManager.Start()
|
main.serverManager.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (main *MainCtx) Shutdown() {
|
func (main *MainCtx) Shutdown() {
|
||||||
err := main.httpManager.Shutdown()
|
err := main.serverManager.Shutdown()
|
||||||
main.logger.Err(err).Msg("http manager shutdown")
|
main.logger.Err(err).Msg("server manager shutdown")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (main *MainCtx) ServeCommand(cmd *cobra.Command, args []string) {
|
func (main *MainCtx) ServeCommand(cmd *cobra.Command, args []string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user