1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
package fs
import (
"context"
"io/fs"
"strings"
"text/template"
sr "tildegit.org/tjp/sliderule"
"tildegit.org/tjp/sliderule/spartan"
)
// SpartanFileHandler builds a handler which serves up files from a filesystem.
//
// It only serves responses for paths which do not correspond to directories on disk.
func SpartanFileHandler(fileSystem fs.FS) sr.Handler {
return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response {
filepath, file, err := ResolveFile(request, fileSystem)
if err != nil {
return spartan.ClientError(err)
}
if file == nil {
return nil
}
return spartan.Success(mediaType(filepath), file)
})
}
// SpartanDirectoryDefault serves up default files for directory path requests.
//
// If any of the supported filenames are found, the contents of the file is returned
// as the spartan response.
//
// It returns nil for any paths which don't correspond to a directory.
//
// When it encounters a directory path which doesn't end in a trailing slash (/) it
// redirects to the same URL with the slash appended. This is necessary for relative
// links not in the directory's contents to function properly.
//
// 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 SpartanDirectoryDefault(fileSystem fs.FS, filenames ...string) sr.Handler {
return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response {
dirpath, dir, response := handleDirSpartan(request, fileSystem)
if response != nil {
return response
}
if dir == nil {
return nil
}
defer func() { _ = dir.Close() }()
filepath, file, err := ResolveDirectoryDefault(fileSystem, dirpath, dir, filenames)
if err != nil {
return spartan.ServerError(err)
}
if file == nil {
return nil
}
return spartan.Success(mediaType(filepath), file)
})
}
// SpartanDirectoryListing produces a listing of the contents of any requested directories.
//
// It returns "4 Resource not found" 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 redirects to a
// URL with the trailing slash appended. This is necessary for relative links not in the directory's
// contents to function properly.
//
// It requires that files provided by the fs.FS implement fs.ReadDirFile. If they don't, it will
// produce "4 Resource not found" responses for any directory paths.
//
// The tmeplate may be nil, in which cause DefaultSpartanDirectoryList is used instead. The
// template is then processed with RenderDirectoryListing.
func SpartanDirectoryListing(filesystem fs.FS, template *template.Template) sr.Handler {
return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response {
dirpath, dir, response := handleDirSpartan(request, filesystem)
if response != nil {
return response
}
if dir == nil {
return nil
}
defer func() { _ = dir.Close() }()
if template == nil {
template = DefaultSpartanDirectoryList
}
body, err := RenderDirectoryListing(dirpath, dir, template, request.Server)
if err != nil {
return spartan.ServerError(err)
}
return spartan.Success("text/gemini", body)
})
}
// DefaultSpartanDirectoryList is a template which renders a reasonable gemtext dir listing.
var DefaultSpartanDirectoryList = DefaultGeminiDirectoryList
func handleDirSpartan(request *sr.Request, filesystem fs.FS) (string, fs.ReadDirFile, *sr.Response) {
path, dir, err := ResolveDirectory(request, filesystem)
if err != nil {
return "", nil, spartan.ServerError(err)
}
if dir == nil {
return "", nil, nil
}
if !strings.HasSuffix(request.Path, "/") {
_ = dir.Close()
url := *request.URL
url.Path += "/"
return "", nil, spartan.Redirect(url.String())
}
return path, dir, nil
}
|