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:
25
smalltrip/multiplexer/multiplexer.go
Normal file
25
smalltrip/multiplexer/multiplexer.go
Normal 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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user