2020-01-19 12:30:09 +13:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-20 09:33:49 +13:00
|
|
|
"encoding/json"
|
2020-01-19 12:30:09 +13:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2021-10-06 09:38:24 +13:00
|
|
|
"m1k1o/neko/internal/http/middleware"
|
|
|
|
"m1k1o/neko/internal/types"
|
|
|
|
"m1k1o/neko/internal/types/config"
|
2020-01-19 12:30:09 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
logger zerolog.Logger
|
|
|
|
router *chi.Mux
|
|
|
|
http *http.Server
|
|
|
|
conf *config.Server
|
|
|
|
}
|
|
|
|
|
2020-01-27 22:00:49 +13:00
|
|
|
func New(conf *config.Server, webSocketHandler types.WebSocketHandler) *Server {
|
2021-03-20 09:33:49 +13:00
|
|
|
logger := log.With().Str("module", "http").Logger()
|
2020-01-19 12:30:09 +13:00
|
|
|
|
|
|
|
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.Logger) // Log API request calls
|
|
|
|
|
|
|
|
router.Get("/ws", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
webSocketHandler.Upgrade(w, r)
|
|
|
|
})
|
|
|
|
|
2021-03-20 09:33:49 +13:00
|
|
|
router.Get("/stats", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
password := r.URL.Query().Get("pwd")
|
|
|
|
isAdmin, err := webSocketHandler.IsAdmin(password)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isAdmin {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
fmt.Fprint(w, "bad authorization")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-03-20 10:06:40 +13:00
|
|
|
|
|
|
|
stats := webSocketHandler.Stats()
|
|
|
|
if err := json.NewEncoder(w).Encode(stats); err != nil {
|
2021-03-20 09:33:49 +13:00
|
|
|
logger.Warn().Err(err).Msg("failed writing json error response")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-03-20 02:42:54 +13:00
|
|
|
router.Get("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte("true"))
|
|
|
|
})
|
|
|
|
|
2020-01-19 12:30:09 +13:00
|
|
|
fs := http.FileServer(http.Dir(conf.Static))
|
|
|
|
router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
2021-03-11 10:52:41 +13:00
|
|
|
if _, err := os.Stat(conf.Static + r.URL.Path); !os.IsNotExist(err) {
|
2020-01-19 12:30:09 +13:00
|
|
|
fs.ServeHTTP(w, r)
|
2021-03-11 10:08:04 +13:00
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
fmt.Fprint(w, "404 page not found")
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: conf.Bind,
|
|
|
|
Handler: router,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Server{
|
|
|
|
logger: logger,
|
|
|
|
router: router,
|
|
|
|
http: server,
|
|
|
|
conf: conf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Start() {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
s.logger.Warn().Msgf("http listening on %s", s.http.Addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Shutdown() error {
|
|
|
|
return s.http.Shutdown(context.Background())
|
|
|
|
}
|