-
-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathhandler.go
73 lines (60 loc) · 1.6 KB
/
handler.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Package handler provides what is essentially the core of Up's
// reverse proxy, complete with all middleware for handling
// logging, redirectcs, static file serving and so on.
package handler
import (
"net/http"
"github.com/pkg/errors"
"github.com/apex/up"
"github.com/apex/up/http/cors"
"github.com/apex/up/http/errorpages"
"github.com/apex/up/http/gzip"
"github.com/apex/up/http/headers"
"github.com/apex/up/http/inject"
"github.com/apex/up/http/logs"
"github.com/apex/up/http/poweredby"
"github.com/apex/up/http/redirects"
"github.com/apex/up/http/relay"
"github.com/apex/up/http/robots"
"github.com/apex/up/http/static"
)
// FromConfig returns the handler based on user config.
func FromConfig(c *up.Config) (http.Handler, error) {
switch c.Type {
case "server":
return relay.New(c)
case "static":
return static.New(c), nil
default:
return nil, errors.Errorf("unknown .type %q", c.Type)
}
}
// New handler complete with all Up middleware.
func New(c *up.Config, h http.Handler) (http.Handler, error) {
h = poweredby.New("up", h)
h = robots.New(c, h)
h = static.NewDynamic(c, h)
h, err := headers.New(c, h)
if err != nil {
return nil, errors.Wrap(err, "headers")
}
h = cors.New(c, h)
h, err = errorpages.New(c, h)
if err != nil {
return nil, errors.Wrap(err, "error pages")
}
h, err = inject.New(c, h)
if err != nil {
return nil, errors.Wrap(err, "inject")
}
h, err = redirects.New(c, h)
if err != nil {
return nil, errors.Wrap(err, "redirects")
}
h = gzip.New(c, h)
h, err = logs.New(c, h)
if err != nil {
return nil, errors.Wrap(err, "logs")
}
return h, nil
}