github.com/webonyx/up@v0.7.4-0.20180808230834-91b94e551323/handler/handler.go (about) 1 // Package handler provides what is essentially the core of Up's 2 // reverse proxy, complete with all middleware for handling 3 // logging, redirectcs, static file serving and so on. 4 package handler 5 6 import ( 7 "net/http" 8 9 "github.com/pkg/errors" 10 11 "github.com/apex/up" 12 "github.com/apex/up/http/cors" 13 "github.com/apex/up/http/errorpages" 14 "github.com/apex/up/http/gzip" 15 "github.com/apex/up/http/headers" 16 "github.com/apex/up/http/inject" 17 "github.com/apex/up/http/logs" 18 "github.com/apex/up/http/ping" 19 "github.com/apex/up/http/poweredby" 20 "github.com/apex/up/http/redirects" 21 "github.com/apex/up/http/relay" 22 "github.com/apex/up/http/robots" 23 "github.com/apex/up/http/static" 24 ) 25 26 // FromConfig returns the handler based on user config. 27 func FromConfig(c *up.Config) (http.Handler, error) { 28 switch c.Type { 29 case "server": 30 return relay.New(c) 31 case "static": 32 return static.New(c), nil 33 default: 34 return nil, errors.Errorf("unknown .type %q", c.Type) 35 } 36 } 37 38 // New handler complete with all Up middleware. 39 func New(c *up.Config, h http.Handler) (http.Handler, error) { 40 h = poweredby.New("up", h) 41 h = robots.New(c, h) 42 h = static.NewDynamic(c, h) 43 44 h, err := headers.New(c, h) 45 if err != nil { 46 return nil, errors.Wrap(err, "headers") 47 } 48 49 h = cors.New(c, h) 50 51 h, err = errorpages.New(c, h) 52 if err != nil { 53 return nil, errors.Wrap(err, "error pages") 54 } 55 56 h, err = inject.New(c, h) 57 if err != nil { 58 return nil, errors.Wrap(err, "inject") 59 } 60 61 h, err = redirects.New(c, h) 62 if err != nil { 63 return nil, errors.Wrap(err, "redirects") 64 } 65 66 h = gzip.New(c, h) 67 68 h, err = logs.New(c, h) 69 if err != nil { 70 return nil, errors.Wrap(err, "logs") 71 } 72 73 h = ping.New(c, h) 74 75 return h, nil 76 }