feat(smalltrip): use multiplexer interface instead of http.ServeMux

This will be useful for the future to provide something akin to
plugins/addons to the router.
This commit is contained in:
Guz
2025-06-27 22:02:39 -03:00
parent 3fc9631fbc
commit 7ae5c14f6c
3 changed files with 33 additions and 10 deletions

View File

@@ -0,0 +1,25 @@
// Copyright 2025-present Gustavo "Guz" L. de Mello
// Copyright 2025-present The Lored.dev Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package multiplexer
import "net/http"
type Multiplexer interface {
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
Handle(pattern string, handler http.Handler)
Handler(r *http.Request) (h http.Handler, pattern string)
http.Handler
}

View File

@@ -19,6 +19,7 @@ import (
"log/slog"
"forge.capytal.company/loreddev/x/smalltrip/middleware"
"forge.capytal.company/loreddev/x/smalltrip/multiplexer"
)
type Option func(*router)
@@ -29,7 +30,9 @@ func WithLogger(logger *slog.Logger) Option {
}
}
func WithMultiplexer(mux multiplexer.Multiplexer) Option {
return func(r *router) {
r.mux = mux
}
}

View File

@@ -16,27 +16,22 @@
package smalltrip
import (
"fmt"
"io"
"log/slog"
"net/http"
"path"
"strings"
"forge.capytal.company/loreddev/x/smalltrip/middleware"
"forge.capytal.company/loreddev/x/tinyssert"
"forge.capytal.company/loreddev/x/smalltrip/multiplexer"
)
type Router interface {
Handle(pattern string, handler http.Handler)
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
Use(middleware middleware.Middleware)
http.Handler
multiplexer.Multiplexer
Use(middleware.Middleware)
}
type router struct {
mux *http.ServeMux
mux multiplexer.Multiplexer
mws []middleware.Middleware
log *slog.Logger
}