2020-10-23 03:54:50 +13:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2022-06-15 10:34:51 +12:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2020-10-23 03:54:50 +13:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2022-07-14 10:58:22 +12:00
|
|
|
"github.com/demodesk/neko/internal/config"
|
2024-07-20 05:10:27 +12:00
|
|
|
"github.com/demodesk/neko/internal/http/legacy"
|
2022-07-14 10:58:22 +12:00
|
|
|
"github.com/demodesk/neko/pkg/types"
|
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
|
2021-08-30 01:40:58 +12:00
|
|
|
config *config.Server
|
2021-09-18 05:23:04 +12:00
|
|
|
router types.Router
|
2020-10-23 03:54:50 +13:00
|
|
|
http *http.Server
|
|
|
|
}
|
|
|
|
|
2021-08-30 01:40:58 +12:00
|
|
|
func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, config *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
|
|
|
|
2023-11-20 02:35:19 +13:00
|
|
|
opts := []RouterOption{
|
|
|
|
WithRequestID(), // create a request id for each request
|
|
|
|
}
|
|
|
|
|
|
|
|
// use real ip if behind proxy
|
|
|
|
// before logger so it can log the real ip
|
|
|
|
if config.Proxy {
|
|
|
|
opts = append(opts, WithRealIP())
|
|
|
|
}
|
|
|
|
|
|
|
|
opts = append(opts,
|
|
|
|
WithLogger(logger),
|
|
|
|
WithRecoverer(), // recover from panics without crashing server
|
|
|
|
)
|
|
|
|
|
|
|
|
if config.HasCors() {
|
|
|
|
opts = append(opts, WithCORS(config.AllowOrigin))
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2023-04-29 08:19:52 +12:00
|
|
|
if config.PathPrefix != "/" {
|
2023-11-20 02:35:19 +13:00
|
|
|
opts = append(opts, WithPathPrefix(config.PathPrefix))
|
2023-04-29 08:19:52 +12:00
|
|
|
}
|
|
|
|
|
2023-11-20 02:35:19 +13:00
|
|
|
router := newRouter(opts...)
|
|
|
|
|
2020-11-15 06:16:25 +13:00
|
|
|
router.Route("/api", ApiManager.Route)
|
2020-11-02 04:54:06 +13:00
|
|
|
|
2021-09-19 00:59:15 +12:00
|
|
|
router.Get("/api/ws", WebSocketManager.Upgrade(func(r *http.Request) bool {
|
|
|
|
return config.AllowOrigin(r.Header.Get("Origin"))
|
|
|
|
}))
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2024-07-20 05:10:27 +12:00
|
|
|
// Legacy handler
|
|
|
|
legacy.New().Route(router)
|
|
|
|
|
2023-01-10 11:18:47 +13:00
|
|
|
batch := batchHandler{
|
|
|
|
Router: router,
|
|
|
|
PathPrefix: "/api",
|
|
|
|
Excluded: []string{
|
|
|
|
"/api/batch", // do not allow batchception
|
|
|
|
"/api/ws",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
router.Post("/api/batch", batch.Handle)
|
|
|
|
|
2022-07-05 04:09:47 +12:00
|
|
|
router.Get("/health", func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
_, err := w.Write([]byte("true"))
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2022-07-05 04:26:29 +12:00
|
|
|
if config.Metrics {
|
|
|
|
router.Get("/metrics", func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
promhttp.Handler().ServeHTTP(w, r)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2022-06-15 10:34:51 +12:00
|
|
|
|
2021-08-30 01:40:58 +12:00
|
|
|
if config.Static != "" {
|
|
|
|
fs := http.FileServer(http.Dir(config.Static))
|
2021-09-17 10:24:33 +12:00
|
|
|
router.Get("/*", func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
_, err := os.Stat(config.Static + r.URL.Path)
|
2021-09-20 10:19:33 +12:00
|
|
|
if err == nil {
|
2020-11-02 05:40:22 +13:00
|
|
|
fs.ServeHTTP(w, r)
|
2021-09-20 10:19:33 +12:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return nil
|
2020-11-02 05:40:22 +13:00
|
|
|
}
|
2021-09-17 10:24:33 +12:00
|
|
|
return err
|
2020-11-02 05:40:22 +13:00
|
|
|
})
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2022-02-13 08:22:50 +13:00
|
|
|
if config.PProf {
|
|
|
|
pprofHandler(router)
|
|
|
|
}
|
|
|
|
|
2020-11-14 05:34:53 +13:00
|
|
|
return &HttpManagerCtx{
|
2020-10-23 03:54:50 +13:00
|
|
|
logger: logger,
|
2021-08-30 01:40:58 +12:00
|
|
|
config: config,
|
2020-10-23 03:54:50 +13:00
|
|
|
router: router,
|
2021-08-30 01:40:58 +12:00
|
|
|
http: &http.Server{
|
|
|
|
Addr: config.Bind,
|
|
|
|
Handler: router,
|
|
|
|
},
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
func (manager *HttpManagerCtx) Start() {
|
2021-08-30 01:40:58 +12:00
|
|
|
if manager.config.Cert != "" && manager.config.Key != "" {
|
2020-10-23 03:54:50 +13:00
|
|
|
go func() {
|
2021-08-30 01:40:58 +12:00
|
|
|
if err := manager.http.ListenAndServeTLS(manager.config.Cert, manager.config.Key); err != http.ErrServerClosed {
|
2021-02-15 05:11:21 +13:00
|
|
|
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 {
|
2021-09-02 10:00:29 +12:00
|
|
|
manager.logger.Info().Msg("shutdown")
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
return manager.http.Shutdown(context.Background())
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|