2020-10-23 03:54:50 +13:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2020-10-29 08:06:34 +13:00
|
|
|
"github.com/go-chi/chi/middleware"
|
2021-01-24 06:18:14 +13:00
|
|
|
"github.com/go-chi/cors"
|
2020-10-23 03:54:50 +13:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
"demodesk/neko/internal/config"
|
2021-02-15 02:40:17 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2020-11-15 06:16:25 +13:00
|
|
|
"demodesk/neko/internal/utils"
|
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
|
2021-01-24 06:18:14 +13:00
|
|
|
router.Use(cors.Handler(cors.Options{
|
2021-02-15 02:40:17 +13:00
|
|
|
AllowOriginFunc: func(r *http.Request, origin string) bool {
|
2021-01-24 06:18:14 +13:00
|
|
|
return conf.AllowOrigin(origin)
|
|
|
|
},
|
|
|
|
AllowedMethods: []string{"GET", "POST", "DELETE", "OPTIONS"},
|
|
|
|
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
|
|
|
ExposedHeaders: []string{"Link"},
|
2021-03-02 06:20:17 +13:00
|
|
|
AllowCredentials: true,
|
2021-01-24 06:18:14 +13:00
|
|
|
MaxAge: 300, // Maximum value not ignored by any of major browsers
|
|
|
|
}))
|
2020-10-23 03:54:50 +13:00
|
|
|
router.Use(middleware.RequestID) // Create a request ID for each request
|
2021-02-15 02:40:17 +13:00
|
|
|
router.Use(Logger) // Log API request calls using custom logger function
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2020-11-15 06:16:25 +13:00
|
|
|
router.Route("/api", ApiManager.Route)
|
2020-11-02 04:54:06 +13:00
|
|
|
|
2021-01-30 11:02:29 +13:00
|
|
|
router.Get("/api/ws", func(w http.ResponseWriter, r *http.Request) {
|
2021-01-24 06:18:14 +13:00
|
|
|
WebSocketManager.Upgrade(w, r, func(r *http.Request) bool {
|
|
|
|
return conf.AllowOrigin(r.Header.Get("Origin"))
|
|
|
|
})
|
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
|
|
|
|
2020-11-15 06:16:25 +13:00
|
|
|
router.NotFound(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
utils.HttpNotFound(w)
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
func (manager *HttpManagerCtx) Start() {
|
|
|
|
if manager.conf.Cert != "" && manager.conf.Key != "" {
|
2020-10-23 03:54:50 +13:00
|
|
|
go func() {
|
2021-02-15 05:11:21 +13:00
|
|
|
if err := manager.http.ListenAndServeTLS(manager.conf.Cert, manager.conf.Key); err != http.ErrServerClosed {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to start https server")
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}()
|
2021-02-15 05:11:21 +13:00
|
|
|
manager.logger.Info().Msgf("https listening on %s", manager.http.Addr)
|
2020-10-23 03:54:50 +13:00
|
|
|
} else {
|
|
|
|
go func() {
|
2021-02-15 05:11:21 +13:00
|
|
|
if err := manager.http.ListenAndServe(); err != http.ErrServerClosed {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to start http server")
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}()
|
2021-02-15 05:11:21 +13:00
|
|
|
manager.logger.Info().Msgf("http listening on %s", manager.http.Addr)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
func (manager *HttpManagerCtx) Shutdown() error {
|
|
|
|
return manager.http.Shutdown(context.Background())
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|