From 629e6a0e0c3a24f35888036f957ee3a631e62816 Mon Sep 17 00:00:00 2001 From: tjp Date: Mon, 13 Nov 2023 07:27:36 -0700 Subject: add nex protocol support --- contrib/fs/nex.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 contrib/fs/nex.go (limited to 'contrib/fs/nex.go') 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:])) -- cgit v1.2.3