2020-10-23 03:54:50 +13:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2020-10-29 08:06:34 +13:00
|
|
|
"github.com/go-chi/chi/middleware"
|
2020-10-23 03:54:50 +13:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2020-11-02 04:09:48 +13:00
|
|
|
"demodesk/neko/internal/config"
|
|
|
|
"demodesk/neko/internal/http/endpoint"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-11-14 05:34:53 +13:00
|
|
|
type HttpManagerCtx struct {
|
2020-10-23 03:54:50 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
router *chi.Mux
|
|
|
|
http *http.Server
|
|
|
|
conf *config.Server
|
|
|
|
}
|
|
|
|
|
2020-11-14 05:34:53 +13:00
|
|
|
func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, conf *config.Server) *HttpManagerCtx {
|
2020-10-29 08:06:34 +13:00
|
|
|
logger := log.With().Str("module", "http").Logger()
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
router := chi.NewRouter()
|
2020-10-29 08:06:34 +13:00
|
|
|
router.Use(middleware.Recoverer) // Recover from panics without crashing server
|
2020-10-23 03:54:50 +13:00
|
|
|
router.Use(middleware.RequestID) // Create a request ID for each request
|
2020-10-30 10:12:33 +13:00
|
|
|
router.Use(Logger) // Log API request calls using custom logger function
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2020-11-02 04:54:06 +13:00
|
|
|
ApiManager.Mount(router)
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
router.Get("/ws", func(w http.ResponseWriter, r *http.Request) {
|
2020-11-02 04:54:06 +13:00
|
|
|
if WebSocketManager.Upgrade(w, r) != nil {
|
2020-10-29 10:36:30 +13:00
|
|
|
//nolint
|
2020-10-29 10:28:10 +13:00
|
|
|
w.Write([]byte("unable to upgrade your connection to a websocket"))
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
})
|
|
|
|
|
2020-11-02 05:40:22 +13:00
|
|
|
if conf.Static != "" {
|
|
|
|
fs := http.FileServer(http.Dir(conf.Static))
|
|
|
|
router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if _, err := os.Stat(conf.Static + r.RequestURI); os.IsNotExist(err) {
|
|
|
|
http.StripPrefix(r.RequestURI, fs).ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
fs.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
router.NotFound(endpoint.Handle(func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
return &endpoint.HandlerError{
|
|
|
|
Status: http.StatusNotFound,
|
2020-10-30 10:12:33 +13:00
|
|
|
Message: fmt.Sprintf("Endpoint '%s' was not found.", r.RequestURI),
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2020-10-29 08:06:34 +13:00
|
|
|
http := &http.Server{
|
2020-10-23 03:54:50 +13:00
|
|
|
Addr: conf.Bind,
|
|
|
|
Handler: router,
|
|
|
|
}
|
|
|
|
|
2020-11-14 05:34:53 +13:00
|
|
|
return &HttpManagerCtx{
|
2020-10-23 03:54:50 +13:00
|
|
|
logger: logger,
|
|
|
|
router: router,
|
2020-10-29 08:06:34 +13:00
|
|
|
http: http,
|
2020-10-23 03:54:50 +13:00
|
|
|
conf: conf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 05:34:53 +13:00
|
|
|
func (s *HttpManagerCtx) Start() {
|
2020-10-23 03:54:50 +13:00
|
|
|
if s.conf.Cert != "" && s.conf.Key != "" {
|
|
|
|
go func() {
|
|
|
|
if err := s.http.ListenAndServeTLS(s.conf.Cert, s.conf.Key); err != http.ErrServerClosed {
|
|
|
|
s.logger.Panic().Err(err).Msg("unable to start https server")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
s.logger.Info().Msgf("https listening on %s", s.http.Addr)
|
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
if err := s.http.ListenAndServe(); err != http.ErrServerClosed {
|
|
|
|
s.logger.Panic().Err(err).Msg("unable to start http server")
|
|
|
|
}
|
|
|
|
}()
|
2020-11-08 08:12:37 +13:00
|
|
|
s.logger.Info().Msgf("http listening on %s", s.http.Addr)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 05:34:53 +13:00
|
|
|
func (s *HttpManagerCtx) Shutdown() error {
|
2020-10-23 03:54:50 +13:00
|
|
|
return s.http.Shutdown(context.Background())
|
|
|
|
}
|