github.com/orteth01/up@v0.2.0/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/apex/log" 10 "github.com/pkg/errors" 11 12 "github.com/apex/up" 13 "github.com/apex/up/http/cors" 14 "github.com/apex/up/http/errorpages" 15 "github.com/apex/up/http/gzip" 16 "github.com/apex/up/http/headers" 17 "github.com/apex/up/http/inject" 18 "github.com/apex/up/http/logs" 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/static" 23 ) 24 25 // New reads up.json to configure and initialize 26 // the http handler chain for serving an Up application. 27 func New() (http.Handler, error) { 28 c, err := up.ReadConfig("up.json") 29 if err != nil { 30 return nil, errors.Wrap(err, "reading config") 31 } 32 33 log.WithFields(log.Fields{ 34 "name": c.Name, 35 "type": c.Type, 36 }).Info("starting") 37 38 var h http.Handler 39 40 switch c.Type { 41 case "server": 42 h, err = relay.New(c) 43 if err != nil { 44 return nil, errors.Wrap(err, "initializing relay") 45 } 46 case "static": 47 h = static.New(c) 48 } 49 50 h = poweredby.New("up", h) 51 52 h, err = headers.New(c, h) 53 if err != nil { 54 return nil, errors.Wrap(err, "initializing headers") 55 } 56 57 h, err = errorpages.New(c, h) 58 if err != nil { 59 return nil, errors.Wrap(err, "initializing error pages") 60 } 61 62 h, err = inject.New(c, h) 63 if err != nil { 64 return nil, errors.Wrap(err, "initializing inject") 65 } 66 67 h = cors.New(c, h) 68 69 h, err = redirects.New(c, h) 70 if err != nil { 71 return nil, errors.Wrap(err, "initializing redirects") 72 } 73 74 h = gzip.New(c, h) 75 76 h, err = logs.New(c, h) 77 if err != nil { 78 return nil, errors.Wrap(err, "initializing logs") 79 } 80 81 return h, nil 82 }