From 46ad450327111b9d28b592658d75ef57da498298 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Wed, 15 Feb 2023 16:44:29 -0700 Subject: Switch Handler to an interface. HandlerFunc is much better as a function returning a Handler, rather than a newtype for the function type itself. This way there is no confusion creating a type-inferenced variable with HandlerFunc(func(... and then using a HandlerFunc where a Handler is expected. Much better to only have one public type. --- router_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'router_test.go') diff --git a/router_test.go b/router_test.go index 6f9c915..bfc48bd 100644 --- a/router_test.go +++ b/router_test.go @@ -13,24 +13,24 @@ import ( "tildegit.org/tjp/gus/gemini" ) -func h1(_ context.Context, _ *gus.Request) *gus.Response { +var h1 = gus.HandlerFunc(func(_ context.Context, _ *gus.Request) *gus.Response { return gemini.Success("", &bytes.Buffer{}) -} +}) func mw1(h gus.Handler) gus.Handler { - return func(ctx context.Context, req *gus.Request) *gus.Response { - resp := h(ctx, req) + return gus.HandlerFunc(func(ctx context.Context, req *gus.Request) *gus.Response { + resp := h.Handle(ctx, req) resp.Body = io.MultiReader(resp.Body, bytes.NewBufferString("\nmiddleware 1")) return resp - } + }) } func mw2(h gus.Handler) gus.Handler { - return func(ctx context.Context, req *gus.Request) *gus.Response { - resp := h(ctx, req) + return gus.HandlerFunc(func(ctx context.Context, req *gus.Request) *gus.Response { + resp := h.Handle(ctx, req) resp.Body = io.MultiReader(resp.Body, bytes.NewBufferString("\nmiddleware 2")) return resp - } + }) } func TestRouterUse(t *testing.T) { -- cgit v1.2.3