diff --git a/smalltrip/multiplexer/multiplexer.go b/smalltrip/multiplexer/multiplexer.go new file mode 100644 index 0000000..8ba09fe --- /dev/null +++ b/smalltrip/multiplexer/multiplexer.go @@ -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 +} diff --git a/smalltrip/options.go b/smalltrip/options.go index 5c573ce..0907ebd 100644 --- a/smalltrip/options.go +++ b/smalltrip/options.go @@ -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 } } diff --git a/smalltrip/smalltrip.go b/smalltrip/smalltrip.go index b8940c4..d5ddcd4 100644 --- a/smalltrip/smalltrip.go +++ b/smalltrip/smalltrip.go @@ -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 }