summaryrefslogtreecommitdiff
path: root/contrib/fs/nex.go
diff options
context:
space:
mode:
authortjp <tjp@ctrl-c.club>2023-11-13 07:27:36 -0700
committertjp <tjp@ctrl-c.club>2023-11-13 07:27:36 -0700
commit629e6a0e0c3a24f35888036f957ee3a631e62816 (patch)
tree76001a2311d3566bf0050b3ef2aa3119fe0823bf /contrib/fs/nex.go
parent1e0f8e0aaeaf1bd2ee39c02e922238b641bcf88b (diff)
add nex protocol support
Diffstat (limited to 'contrib/fs/nex.go')
-rw-r--r--contrib/fs/nex.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/contrib/fs/nex.go b/contrib/fs/nex.go
new file mode 100644
index 0000000..47eb83e
--- /dev/null
+++ b/contrib/fs/nex.go
@@ -0,0 +1,48 @@
+package fs
+
+import (
+ "text/template"
+
+ sr "tildegit.org/tjp/sliderule"
+ "tildegit.org/tjp/sliderule/nex"
+)
+
+// NexFileHandler builds a handler which serves up files from a file system.
+//
+// It only serves responses for paths which correspond to files, not directories.
+func NexFileHandler(fsroot, urlroot string) sr.Handler {
+ return fileHandler(nex.ServerProtocol, fsroot, urlroot)
+}
+
+// NexDirectoryDefault serves up default files for directory path requests.
+//
+// If any of the supported filenames are found in the requested directory, the
+// contents of that file is returned as the nex response.
+//
+// It returns nil for any paths which don't correspond to a directory.
+func NexDirectoryDefault(fsroot, urlroot string, filenames ...string) sr.Handler {
+ return directoryDefault(nex.ServerProtocol, fsroot, urlroot, false, filenames...)
+}
+
+// NexDirectoryListing produces a listing of the contents of any requested directories.
+//
+// It returns a nil response for any paths which don't correspond to a filesystem directory.
+//
+// When it encounters a directory path which doesn't end in a trailing slash (/) it returns
+// a nil response. Trailing slashes are necessary for relative links to work properly.
+//
+// The template may be nil, in which case DefaultNexDirectoryList is used instead. The
+// template is then processed with RenderDirectoryListing.
+func NexDirectoryListing(fsroot, urlroot string, tmpl *template.Template) sr.Handler {
+ if tmpl == nil {
+ tmpl = DefaultNexDirectoryList
+ }
+ return directoryListing(nex.ServerProtocol, fsroot, urlroot, "", false, tmpl)
+}
+
+var DefaultNexDirectoryList = template.Must(template.New("nex_dirlist").Parse(`
+{{ .DirName }} directory:
+{{ range .Entries }}
+=> ./{{ .Name }}{{ if .IsDir }}/{{ end -}}
+{{ end }}
+`[1:]))