opengist/internal/web/server/server.go

72 lines
1.6 KiB
Go
Raw Normal View History

2024-12-03 01:18:04 +00:00
package server
import (
"errors"
2024-12-29 10:40:23 +00:00
"github.com/thomiceli/opengist/internal/utils"
2024-12-03 01:18:04 +00:00
"net/http"
"strings"
"github.com/gorilla/sessions"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/thomiceli/opengist/internal/config"
"github.com/thomiceli/opengist/internal/i18n"
)
type Server struct {
echo *echo.Echo
flashStore *sessions.CookieStore // session store for flash messages
UserStore *sessions.FilesystemStore // session store for user sessions
dev bool
sessionsPath string
ignoreCsrf bool
}
func NewServer(isDev bool, sessionsPath string, ignoreCsrf bool) *Server {
e := echo.New()
e.HideBanner = true
e.HidePort = true
s := &Server{echo: e, dev: isDev, sessionsPath: sessionsPath, ignoreCsrf: ignoreCsrf}
s.useCustomContext()
if err := i18n.Locales.LoadAll(); err != nil {
log.Fatal().Err(err).Msg("Failed to load locales")
}
2025-01-02 19:55:33 +00:00
s.registerMiddlewares()
2024-12-03 01:18:04 +00:00
s.setFuncMap()
2024-12-29 10:40:23 +00:00
s.echo.HTTPErrorHandler = s.errorHandler
2024-12-03 01:18:04 +00:00
e.Validator = utils.NewValidator()
if !s.dev {
2025-01-02 19:55:33 +00:00
s.parseManifestEntries()
2024-12-03 01:18:04 +00:00
}
2025-01-02 19:55:33 +00:00
s.registerRoutes()
2024-12-03 01:18:04 +00:00
return s
}
func (s *Server) Start() {
addr := config.C.HttpHost + ":" + config.C.HttpPort
log.Info().Msg("Starting HTTP server on http://" + addr)
if err := s.echo.Start(addr); err != nil && err != http.ErrServerClosed {
log.Fatal().Err(err).Msg("Failed to start HTTP server")
}
}
func (s *Server) Stop() {
if err := s.echo.Close(); err != nil {
log.Fatal().Err(err).Msg("Failed to stop HTTP server")
}
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.echo.ServeHTTP(w, r)
}