implement proper sever route handling

This commit is contained in:
Robin Olsen
2026-02-02 14:16:08 +01:00
parent 622e830f6e
commit c69fd84fdd
10 changed files with 290 additions and 35 deletions

View File

@@ -6,22 +6,18 @@ import (
"strings"
"time"
"github.com/LazyBachelor/LazyPM/pkg/web/handler"
"github.com/NYTimes/gziphandler"
"github.com/rs/cors"
)
type Route struct {
Pattern string
Handeler http.Handler
}
func (s *Server) RegisterRoutes(assets embed.FS, routes []Route) http.Handler {
func (s *Server) RegisterRoutes(assets embed.FS) http.Handler {
mux := http.NewServeMux()
s.handleAssets(mux, assets)
for _, route := range routes {
mux.Handle(route.Pattern, route.Handeler)
for _, route := range handler.GetRoutes(s.Services) {
mux.Handle(route.Pattern, route.Handler)
}
handler := cors.Default().Handler(mux)

View File

@@ -1,16 +1,16 @@
package server
import (
"github.com/LazyBachelor/LazyPM/internal/service"
"embed"
"net/http"
"time"
"github.com/LazyBachelor/LazyPM/internal/service"
)
type Server struct {
Address string
Assets embed.FS
Routes []Route
Services *service.Services
}
@@ -20,9 +20,11 @@ func NewServer(props Server) *http.Server {
props.Address = "localhost:8080"
}
handler := props.RegisterRoutes(props.Assets)
return &http.Server{
Addr: props.Address,
Handler: props.RegisterRoutes(props.Assets, props.Routes),
Handler: handler,
IdleTimeout: time.Minute,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,