From 5f6093d2ce3d173185e56705dd8e83f9d4f27adf Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L de Mello" Date: Fri, 27 Jun 2025 22:02:39 -0300 Subject: [PATCH] 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. --- multiplexer/multiplexer.go | 25 +++++++++++++++++++++++++ options.go | 3 +++ smalltrip.go | 15 +++++---------- 3 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 multiplexer/multiplexer.go diff --git a/multiplexer/multiplexer.go b/multiplexer/multiplexer.go new file mode 100644 index 0000000..8ba09fe --- /dev/null +++ b/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/options.go b/options.go index 5c573ce..0907ebd 100644 --- a/options.go +++ b/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.go b/smalltrip.go index b8940c4..d5ddcd4 100644 --- a/smalltrip.go +++ b/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 }