summaryrefslogtreecommitdiff
path: root/nex.go
diff options
context:
space:
mode:
authortjp <tjp@ctrl-c.club>2023-11-14 18:58:06 -0700
committertjp <tjp@ctrl-c.club>2023-11-14 18:58:06 -0700
commit2cdc569840f36dad139132c37de1b62e21a971b7 (patch)
tree2fd4b5c5626a50230ffcd0b6f28dcb048482ef56 /nex.go
parenta2b05982a2bbd2c7baee1efb7e98db8de9123330 (diff)
initial support for nex protocol
Diffstat (limited to 'nex.go')
-rw-r--r--nex.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/nex.go b/nex.go
new file mode 100644
index 0000000..bd9bdce
--- /dev/null
+++ b/nex.go
@@ -0,0 +1,79 @@
+package main
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/go-kit/log/level"
+
+ sr "tildegit.org/tjp/sliderule"
+ "tildegit.org/tjp/sliderule/contrib/cgi"
+ "tildegit.org/tjp/sliderule/contrib/fs"
+ "tildegit.org/tjp/sliderule/logging"
+ "tildegit.org/tjp/sliderule/nex"
+)
+
+func buildNexServer(server Server, config *Configuration) (sr.Server, error) {
+ addr := fmt.Sprintf("%s:%d", server.IP.String(), server.Port)
+
+ baselog := BaseLogger(config)
+ info := level.Info(baselog)
+ errlog := level.Error(baselog)
+
+ if server.TLS != nil {
+ return nex.NewTLSServer(
+ context.Background(),
+ server.Hostnames[0],
+ "tcp",
+ addr,
+ logging.LogRequests(info)(routes(server)),
+ errlog,
+ server.TLS,
+ )
+ } else {
+ return nex.NewServer(
+ context.Background(),
+ server.Hostnames[0],
+ "tcp",
+ addr,
+ logging.LogRequests(info)(routes(server)),
+ errlog,
+ )
+ }
+}
+
+func addNexRoute(router *sr.Router, route RouteDirective) {
+ switch route.Type {
+ case "static":
+ addNexStaticRoute(router, route)
+ case "cgi":
+ buildAndAddRoute(router, route, func(route RouteDirective) sr.Handler {
+ return cgi.NexCGIDirectory(route.FsPath, route.URLPath, route.Modifiers.ExecCmd)
+ })
+ }
+}
+
+func addNexStaticRoute(router *sr.Router, route RouteDirective) {
+ buildAndAddRoute(router, route, func(route RouteDirective) sr.Handler {
+ handlers := []sr.Handler{}
+
+ if route.Modifiers.Exec {
+ handlers = append(handlers, cgi.NexCGIDirectory(route.FsPath, route.URLPath, route.Modifiers.ExecCmd))
+ }
+
+ handlers = append(handlers, fs.NexFileHandler(route.FsPath, route.URLPath))
+
+ if route.Modifiers.DirDefault != "" {
+ handlers = append(
+ handlers,
+ fs.NexDirectoryDefault(route.FsPath, route.URLPath, route.Modifiers.DirDefault),
+ )
+ }
+
+ if route.Modifiers.DirList {
+ handlers = append(handlers, fs.NexDirectoryListing(route.FsPath, route.URLPath, nil))
+ }
+
+ return sr.FallthroughHandler(handlers...)
+ })
+}