forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
55 lines (45 loc) · 1.63 KB
/
static.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
package builtin
import (
"fmt"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/filters/serve"
"net/http"
)
type static struct {
handler http.Handler
}
// Returns a filter Spec to serve static content from a file system
// location. Behaves similarly to net/http.FileServer. It shunts the route.
//
// Filter instances of this specification expect two parameters: a
// request path prefix and a local directory path. When processing a
// request, it clips the prefix from the request path, and appends the
// rest of the path to the directory path. Then, it uses the resulting
// path to serve static content from the file system.
//
// Name: "static".
func NewStatic() filters.Spec { return &static{} }
// "static"
func (spec *static) Name() string { return StaticName }
// Creates instances of the static filter. Expects two parameters: request path
// prefix and file system root.
func (spec *static) CreateFilter(config []interface{}) (filters.Filter, error) {
if len(config) != 2 {
return nil, fmt.Errorf("invalid number of args: %d, expected 1", len(config))
}
webRoot, ok := config[0].(string)
if !ok {
return nil, fmt.Errorf("invalid parameter type, expected string for web root prefix")
}
root, ok := config[1].(string)
if !ok {
return nil, fmt.Errorf("invalid parameter type, expected string for path to root dir")
}
return &static{http.StripPrefix(webRoot, http.FileServer(http.Dir(root)))}, nil
}
// Serves content from the file system and marks the request served.
func (f *static) Request(ctx filters.FilterContext) {
serve.ServeHTTP(ctx, f.handler)
}
// Noop.
func (f *static) Response(filters.FilterContext) {}