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