-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathhttp_file_server.go
59 lines (54 loc) Β· 1.48 KB
/
http_file_server.go
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
package expr
import (
"fmt"
"path"
"strings"
)
type (
// HTTPFileServerExpr defines an endpoint that serves static assets
// through HTTP.
HTTPFileServerExpr struct {
// Service is the parent service.
Service *HTTPServiceExpr
// Description for docs
Description string
// Docs points to the service external documentation
Docs *DocsExpr
// FilePath is the file path to the static asset(s)
FilePath string
// RequestPaths is the list of HTTP paths that serve the assets.
RequestPaths []string
// Meta is a list of key/value pairs
Meta MetaExpr
}
)
// EvalName returns the generic definition name used in error messages.
func (f *HTTPFileServerExpr) EvalName() string {
suffix := fmt.Sprintf("file server %s", f.FilePath)
var prefix string
if f.Service != nil {
prefix = f.Service.EvalName() + " "
}
return prefix + suffix
}
// Finalize normalizes the request path.
func (f *HTTPFileServerExpr) Finalize() {
current := f.RequestPaths[0]
paths := f.Service.Paths
if len(paths) == 0 {
paths = []string{"/"}
}
f.RequestPaths = make([]string, len(paths))
for i, sp := range paths {
p := path.Join(Root.API.HTTP.Path, sp, current)
// Make sure request path starts with a "/" so codegen can rely on it.
if !strings.HasPrefix(p, "/") {
p = "/" + p
}
f.RequestPaths[i] = p
}
}
// IsDir returns true if the file server serves a directory, false otherwise.
func (f *HTTPFileServerExpr) IsDir() bool {
return HTTPWildcardRegex.MatchString(f.RequestPaths[0])
}