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. --- contrib/cgi/gemini.go | 4 ++-- contrib/cgi/gopher.go | 4 ++-- contrib/fs/dir_test.go | 4 ++-- contrib/fs/file_test.go | 2 +- contrib/fs/gemini.go | 12 ++++++------ contrib/fs/gopher.go | 12 ++++++------ contrib/sharedhost/replacement.go | 6 +++--- contrib/sharedhost/replacement_test.go | 6 +++--- contrib/tlsauth/auth_test.go | 20 ++++++++++---------- contrib/tlsauth/gemini.go | 12 ++++++------ contrib/tlsauth/gemini_test.go | 20 ++++++++++---------- 11 files changed, 51 insertions(+), 51 deletions(-) (limited to 'contrib') diff --git a/contrib/cgi/gemini.go b/contrib/cgi/gemini.go index 8302e7e..1587037 100644 --- a/contrib/cgi/gemini.go +++ b/contrib/cgi/gemini.go @@ -17,7 +17,7 @@ import ( // the URI path. func GeminiCGIDirectory(pathRoot, fsRoot string) gus.Handler { fsRoot = strings.TrimRight(fsRoot, "/") - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { if !strings.HasPrefix(request.Path, pathRoot) { return nil } @@ -43,5 +43,5 @@ func GeminiCGIDirectory(pathRoot, fsRoot string) gus.Handler { return gemini.Failure(err) } return response - } + }) } diff --git a/contrib/cgi/gopher.go b/contrib/cgi/gopher.go index 29bfdba..4378eb7 100644 --- a/contrib/cgi/gopher.go +++ b/contrib/cgi/gopher.go @@ -17,7 +17,7 @@ import ( // the URI path. func GopherCGIDirectory(pathRoot, fsRoot string) gus.Handler { fsRoot = strings.TrimRight(fsRoot, "/") - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { if !strings.HasPrefix(request.Path, pathRoot) { return nil } @@ -41,5 +41,5 @@ func GopherCGIDirectory(pathRoot, fsRoot string) gus.Handler { } return gopher.File(0, stdout) - } + }) } diff --git a/contrib/fs/dir_test.go b/contrib/fs/dir_test.go index 6109a3c..9c6770d 100644 --- a/contrib/fs/dir_test.go +++ b/contrib/fs/dir_test.go @@ -47,7 +47,7 @@ func TestDirectoryDefault(t *testing.T) { require.Nil(t, err) request := &gus.Request{URL: u} - response := handler(context.Background(), request) + response := handler.Handle(context.Background(), request) if response == nil { assert.Equal(t, test.status, gemini.StatusNotFound) @@ -108,7 +108,7 @@ func TestDirectoryListing(t *testing.T) { require.Nil(t, err) request := &gus.Request{URL: u} - response := handler(context.Background(), request) + response := handler.Handle(context.Background(), request) if response == nil { assert.Equal(t, test.status, gemini.StatusNotFound) diff --git a/contrib/fs/file_test.go b/contrib/fs/file_test.go index f97b66b..3949b83 100644 --- a/contrib/fs/file_test.go +++ b/contrib/fs/file_test.go @@ -58,7 +58,7 @@ func TestFileHandler(t *testing.T) { require.Nil(t, err) request := &gus.Request{URL: u} - response := handler(context.Background(), request) + response := handler.Handle(context.Background(), request) if response == nil { assert.Equal(t, test.status, gemini.StatusNotFound) diff --git a/contrib/fs/gemini.go b/contrib/fs/gemini.go index 4193493..15677f1 100644 --- a/contrib/fs/gemini.go +++ b/contrib/fs/gemini.go @@ -14,7 +14,7 @@ import ( // // It only serves responses for paths which do not correspond to directories on disk. func GeminiFileHandler(fileSystem fs.FS) gus.Handler { - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { filepath, file, err := ResolveFile(request, fileSystem) if err != nil { return gemini.Failure(err) @@ -25,7 +25,7 @@ func GeminiFileHandler(fileSystem fs.FS) gus.Handler { } return gemini.Success(mediaType(filepath), file) - } + }) } // GeminiDirectoryDefault serves up default files for directory path requests. @@ -42,7 +42,7 @@ func GeminiFileHandler(fileSystem fs.FS) gus.Handler { // It requires that files from the provided fs.FS implement fs.ReadDirFile. If they // don't, it will produce nil responses for any directory paths. func GeminiDirectoryDefault(fileSystem fs.FS, filenames ...string) gus.Handler { - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { dirpath, dir, response := handleDirGemini(request, fileSystem) if response != nil { return response @@ -61,7 +61,7 @@ func GeminiDirectoryDefault(fileSystem fs.FS, filenames ...string) gus.Handler { } return gemini.Success(mediaType(filepath), file) - } + }) } // GeminiDirectoryListing produces a listing of the contents of any requested directories. @@ -78,7 +78,7 @@ func GeminiDirectoryDefault(fileSystem fs.FS, filenames ...string) gus.Handler { // The template may be nil, in which case DefaultGeminiDirectoryList is used instead. The // template is then processed with RenderDirectoryListing. func GeminiDirectoryListing(fileSystem fs.FS, template *template.Template) gus.Handler { - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { dirpath, dir, response := handleDirGemini(request, fileSystem) if response != nil { return response @@ -97,7 +97,7 @@ func GeminiDirectoryListing(fileSystem fs.FS, template *template.Template) gus.H } return gemini.Success("text/gemini", body) - } + }) } // DefaultGeminiDirectoryList is a template which renders a reasonable gemtext dir list. diff --git a/contrib/fs/gopher.go b/contrib/fs/gopher.go index 7b0d8bd..f63785c 100644 --- a/contrib/fs/gopher.go +++ b/contrib/fs/gopher.go @@ -16,7 +16,7 @@ import ( // // It only serves responses for paths which correspond to files, not directories. func GopherFileHandler(fileSystem fs.FS) gus.Handler { - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { filepath, file, err := ResolveFile(request, fileSystem) if err != nil { return gopher.Error(err).Response() @@ -27,7 +27,7 @@ func GopherFileHandler(fileSystem fs.FS) gus.Handler { } return gopher.File(GuessGopherItemType(filepath), file) - } + }) } // GopherDirectoryDefault serves up default files for directory path requests. @@ -40,7 +40,7 @@ func GopherFileHandler(fileSystem fs.FS) gus.Handler { // It requires that files from the provided fs.FS implement fs.ReadDirFile. If // they don't, it will produce nil responses for all directory paths. func GopherDirectoryDefault(fileSystem fs.FS, filenames ...string) gus.Handler { - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { dirpath, dir, err := ResolveDirectory(request, fileSystem) if err != nil { return gopher.Error(err).Response() @@ -59,7 +59,7 @@ func GopherDirectoryDefault(fileSystem fs.FS, filenames ...string) gus.Handler { } return gopher.File(gopher.MenuType, file) - } + }) } // GopherDirectoryListing produces a listing of the contents of any requested directories. @@ -72,7 +72,7 @@ func GopherDirectoryDefault(fileSystem fs.FS, filenames ...string) gus.Handler { // A template may be nil, in which case DefaultGopherDirectoryList is used instead. The // template is then processed with RenderDirectoryListing. func GopherDirectoryListing(fileSystem fs.FS, tpl *template.Template) gus.Handler { - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { dirpath, dir, err := ResolveDirectory(request, fileSystem) if err != nil { return gopher.Error(err).Response() @@ -91,7 +91,7 @@ func GopherDirectoryListing(fileSystem fs.FS, tpl *template.Template) gus.Handle } return gopher.File(gopher.MenuType, body) - } + }) } // GopherTemplateFunctions is a map for templates providing useful functions for gophermaps. diff --git a/contrib/sharedhost/replacement.go b/contrib/sharedhost/replacement.go index 1fb2a0d..9267530 100644 --- a/contrib/sharedhost/replacement.go +++ b/contrib/sharedhost/replacement.go @@ -19,14 +19,14 @@ import ( // "users/", "domain.com/~jim/index.gmi" maps to "domain.com/users/jim/index.gmi". func ReplaceTilde(replacement string) gus.Middleware { return func(inner gus.Handler) gus.Handler { - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { if len(request.Path) > 1 && request.Path[0] == '/' && request.Path[1] == '~' { request = cloneRequest(request) request.Path = "/" + replacement + request.Path[2:] } - return inner(ctx, request) - } + return inner.Handle(ctx, request) + }) } } diff --git a/contrib/sharedhost/replacement_test.go b/contrib/sharedhost/replacement_test.go index cab80bb..67c3754 100644 --- a/contrib/sharedhost/replacement_test.go +++ b/contrib/sharedhost/replacement_test.go @@ -43,12 +43,12 @@ func TestReplaceTilde(t *testing.T) { replacer := sharedhost.ReplaceTilde(test.replacement) request := &gus.Request{URL: u} - handler := replacer(func(_ context.Context, request *gus.Request) *gus.Response { + handler := replacer(gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { assert.Equal(t, test.replacedPath, request.Path) return &gus.Response{} - }) + })) - handler(context.Background(), request) + handler.Handle(context.Background(), request) // original request was unmodified assert.Equal(t, originalPath, request.Path) diff --git a/contrib/tlsauth/auth_test.go b/contrib/tlsauth/auth_test.go index 30b63f5..3cbc106 100644 --- a/contrib/tlsauth/auth_test.go +++ b/contrib/tlsauth/auth_test.go @@ -24,7 +24,7 @@ func TestIdentify(t *testing.T) { server, client, clientCert := setup(t, "testdata/server.crt", "testdata/server.key", "testdata/client1.crt", "testdata/client1.key", - func(_ context.Context, request *gus.Request) *gus.Response { + gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { invoked = true ident := tlsauth.Identity(request) @@ -33,7 +33,7 @@ func TestIdentify(t *testing.T) { } return nil - }, + }), ) leafCert, err := x509.ParseCertificate(clientCert.Certificate[0]) require.Nil(t, err) @@ -51,15 +51,15 @@ func TestRequiredAuth(t *testing.T) { invoked1 := false invoked2 := false - handler1 := func(_ context.Context, request *gus.Request) *gus.Response { + handler1 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { invoked1 = true return gemini.Success("", &bytes.Buffer{}) - } + }) - handler2 := func(_ context.Context, request *gus.Request) *gus.Response { + handler2 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { invoked2 = true return gemini.Success("", &bytes.Buffer{}) - } + }) authMiddleware := gus.Filter(tlsauth.RequiredAuth(tlsauth.Allow), nil) @@ -94,19 +94,19 @@ func TestOptionalAuth(t *testing.T) { invoked1 := false invoked2 := false - handler1 := func(_ context.Context, request *gus.Request) *gus.Response { + handler1 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { if !strings.HasPrefix(request.Path, "/one") { return nil } invoked1 = true return gemini.Success("", &bytes.Buffer{}) - } + }) - handler2 := func(_ context.Context, request *gus.Request) *gus.Response { + handler2 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { invoked2 = true return gemini.Success("", &bytes.Buffer{}) - } + }) mw := gus.Filter(tlsauth.OptionalAuth(tlsauth.Reject), nil) handler := gus.FallthroughHandler(mw(handler1), mw(handler2)) diff --git a/contrib/tlsauth/gemini.go b/contrib/tlsauth/gemini.go index 0db89de..40bee9e 100644 --- a/contrib/tlsauth/gemini.go +++ b/contrib/tlsauth/gemini.go @@ -14,7 +14,7 @@ import ( // not pass the approver it will be rejected with "62 certificate invalid". func GeminiAuth(approver Approver) gus.Middleware { return func(inner gus.Handler) gus.Handler { - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { identity := Identity(request) if identity == nil { return geminiMissingCert(ctx, request) @@ -23,8 +23,8 @@ func GeminiAuth(approver Approver) gus.Middleware { return geminiCertNotAuthorized(ctx, request) } - return inner(ctx, request) - } + return inner.Handle(ctx, request) + }) } } @@ -35,14 +35,14 @@ func GeminiAuth(approver Approver) gus.Middleware { // certificate, but it fails the approval. func GeminiOptionalAuth(approver Approver) gus.Middleware { return func(inner gus.Handler) gus.Handler { - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { identity := Identity(request) if identity != nil && !approver(identity) { return geminiCertNotAuthorized(ctx, request) } - return inner(ctx, request) - } + return inner.Handle(ctx, request) + }) } } diff --git a/contrib/tlsauth/gemini_test.go b/contrib/tlsauth/gemini_test.go index 8f1efda..7823de6 100644 --- a/contrib/tlsauth/gemini_test.go +++ b/contrib/tlsauth/gemini_test.go @@ -14,30 +14,30 @@ import ( ) func TestGeminiAuth(t *testing.T) { - handler1 := func(_ context.Context, request *gus.Request) *gus.Response { + handler1 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { if !strings.HasPrefix(request.Path, "/one") { return nil } return gemini.Success("", &bytes.Buffer{}) - } - handler2 := func(_ context.Context, request *gus.Request) *gus.Response { + }) + handler2 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { if !strings.HasPrefix(request.Path, "/two") { return nil } return gemini.Success("", &bytes.Buffer{}) - } - handler3 := func(_ context.Context, request *gus.Request) *gus.Response { + }) + handler3 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { if !strings.HasPrefix(request.Path, "/three") { return nil } return gemini.Success("", &bytes.Buffer{}) - } - handler4 := func(_ context.Context, request *gus.Request) *gus.Response { + }) + handler4 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { return gemini.Success("", &bytes.Buffer{}) - } + }) handler := gus.FallthroughHandler( tlsauth.GeminiAuth(tlsauth.Allow)(handler1), @@ -74,12 +74,12 @@ func TestGeminiAuth(t *testing.T) { func TestGeminiOptionalAuth(t *testing.T) { pathHandler := func(path string) gus.Handler { - return func(_ context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { if !strings.HasPrefix(request.Path, path) { return nil } return gemini.Success("", &bytes.Buffer{}) - } + }) } handler := gus.FallthroughHandler( -- cgit v1.2.3