feat: ServerMux to gorilla/mux, time route

This commit is contained in:
2025-11-22 23:25:00 +03:00
parent 51f30b516e
commit 659369c42b
7 changed files with 208 additions and 12 deletions

View File

@@ -10,13 +10,14 @@ import (
// setupRoutes configures all routes
func (s *Server) setupRoutes() {
// Add request ID middleware to all routes
s.router.Handle("/", middleware.RequestIDMiddleware(
middleware.LoggingMiddleware(
handlers.NewHomeHandler(),
),
))
// Apply global middleware to all routes
s.router.Use(middleware.RequestIDMiddleware)
s.router.Use(middleware.LoggingMiddleware)
// Swagger UI
s.router.Handle("/swagger/", httpSwagger.WrapHandler)
// Register routes
s.router.Handle("/", handlers.NewHomeHandler()).Methods("GET")
s.router.Handle("/time", handlers.NewTimeHandler()).Methods("GET")
// Swagger UI (no middleware needed)
s.router.PathPrefix("/swagger/").Handler(httpSwagger.WrapHandler)
}

View File

@@ -7,6 +7,7 @@ import (
"git.nwaifu.su/sergey/MyGoServer/cmd/apiserver/config"
"git.nwaifu.su/sergey/MyGoServer/internal/apiserver/logger"
"github.com/gorilla/mux"
)
type contextKey struct {
@@ -22,7 +23,7 @@ func saveConnInContext(ctx context.Context, c net.Conn) context.Context {
// Server represents the HTTP server
type Server struct {
config *config.Config
router *http.ServeMux
router *mux.Router
server *http.Server
}
@@ -36,7 +37,7 @@ func NewServer(cfg *config.Config) *Server {
logger.Initialize(cfg.Logging.Level, cfg.Logging.Format, cfg.Logging.Output)
// Create router
s.router = http.NewServeMux()
s.router = mux.NewRouter()
s.setupRoutes()
// Create HTTP server
@@ -55,7 +56,7 @@ func (s *Server) Start() error {
}
// GetRouter returns the HTTP router
func (s *Server) GetRouter() *http.ServeMux {
func (s *Server) GetRouter() *mux.Router {
return s.router
}