From 7baecfca55527dc82499686c29ee59ae8c4b972e Mon Sep 17 00:00:00 2001 From: tjp Date: Tue, 14 Nov 2023 21:50:15 -0700 Subject: add support for syw git views on nex --- README.gmi | 2 +- README.md | 2 +- nex.go | 20 ++++++++++++++++++++ parse.go | 4 ---- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.gmi b/README.gmi index 7e6a7e1..344f80a 100644 --- a/README.gmi +++ b/README.gmi @@ -261,7 +261,7 @@ gemini { } ``` -"git" is not supported in finger or nex servers, but otherwise it builds appropriate views according to the protocol of the server it is under (gemtext on gemini and spartan, gopher menu on gopher). +"git" is not supported in finger, but otherwise it builds appropriate views according to the protocol of the server it is under (gemtext on gemini and spartan, gopher menu on gopher, plain text on nex). The only supported modifier is "templates" - find more details on that in the section on "Git Viewing Templates". diff --git a/README.md b/README.md index 739d927..9678b96 100644 --- a/README.md +++ b/README.md @@ -266,7 +266,7 @@ gemini { } ``` -"git" is not supported in finger or nex servers, but otherwise it builds appropriate views according to the protocol of the server it is under (gemtext on gemini and spartan, gopher menu on gopher). +"git" is not supported in finger, but otherwise it builds appropriate views according to the protocol of the server it is under (gemtext on gemini and spartan, gopher menu on gopher, plain text on nex). The only supported modifier is "templates" - find more details on that in the section on "Git Viewing Templates". diff --git a/nex.go b/nex.go index d64f588..a8a5a9e 100644 --- a/nex.go +++ b/nex.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "strings" "github.com/go-kit/log/level" @@ -11,6 +12,7 @@ import ( "tildegit.org/tjp/sliderule/contrib/fs" "tildegit.org/tjp/sliderule/logging" "tildegit.org/tjp/sliderule/nex" + "tildegit.org/tjp/syw" ) func buildNexServer(server Server, config *Configuration) (sr.Server, error) { @@ -57,6 +59,8 @@ func addNexRoute(router *sr.Router, route RouteDirective) { buildAndAddRoute(router, route, func(route RouteDirective) sr.Handler { return cgi.NexCGIDirectory(route.FsPath, route.URLPath, route.Modifiers.ExecCmd) }) + case "git": + addNexGitRoute(router, route) } } @@ -84,3 +88,19 @@ func addNexStaticRoute(router *sr.Router, route RouteDirective) { return sr.FallthroughHandler(handlers...) }) } + +func addNexGitRoute(router *sr.Router, route RouteDirective) { + buildAndAddRoute(router, route, func(route RouteDirective) sr.Handler { + subrouter := syw.NexRouter(route.FsPath, route.Modifiers.Templates) + return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { + reqclone := cloneRequest(request) + reqclone.Path = strings.TrimPrefix(reqclone.Path, route.URLPath) + + handler, params := subrouter.Match(reqclone) + if handler == nil { + return nil + } + return handler.Handle(context.WithValue(ctx, sr.RouteParamsKey, params), request) + }) + }) +} diff --git a/parse.go b/parse.go index d0bf7a3..0995713 100644 --- a/parse.go +++ b/parse.go @@ -326,10 +326,6 @@ func validateRoute(serverType string, dir *RouteDirective) error { return errors.New("cgi directives only support the 'extendedgophermap' modifier") } - if dir.Type == "git" && serverType != "gemini" && serverType != "gopher" && serverType != "spartan" { - return fmt.Errorf("git directive not allowed in %s server", serverType) - } - if serverType == "finger" && (dir.Modifiers.DirDefault != "" || dir.Modifiers.DirList) { return errors.New("finger servers don't support directory 'with' modifiers") } -- cgit v1.2.3