github.com/sotirispl/buffalo@v0.11.1/options.go (about) 1 package buffalo 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "strings" 8 9 "github.com/sirupsen/logrus" 10 11 "github.com/fatih/color" 12 "github.com/gobuffalo/buffalo/worker" 13 "github.com/gobuffalo/envy" 14 "github.com/gobuffalo/pop" 15 "github.com/gorilla/sessions" 16 "github.com/markbates/going/defaults" 17 ) 18 19 // Options are used to configure and define how your application should run. 20 type Options struct { 21 Name string 22 // Addr is the bind address provided to http.Server. Default is "127.0.0.1:3000" 23 // Can be set using ENV vars "ADDR" and "PORT". 24 Addr string 25 // Host that this application will be available at. Default is "http://127.0.0.1:[$PORT|3000]". 26 Host string 27 28 // Env is the "environment" in which the App is running. Default is "development". 29 Env string 30 31 // LogLevel defaults to "debug". 32 LogLevel string 33 // Logger to be used with the application. A default one is provided. 34 Logger Logger 35 36 // MethodOverride allows for changing of the request method type. See the default 37 // implementation at buffalo.MethodOverride 38 MethodOverride http.HandlerFunc 39 40 // SessionStore is the `github.com/gorilla/sessions` store used to back 41 // the session. It defaults to use a cookie store and the ENV variable 42 // `SESSION_SECRET`. 43 SessionStore sessions.Store 44 // SessionName is the name of the session cookie that is set. This defaults 45 // to "_buffalo_session". 46 SessionName string 47 48 // Worker implements the Worker interface and can process tasks in the background. 49 // Default is "github.com/gobuffalo/worker.Simple. 50 Worker worker.Worker 51 // WorkerOff tells App.Start() whether to start the Worker process or not. Default is "false". 52 WorkerOff bool 53 54 // PreHandlers are http.Handlers that are called between the http.Server 55 // and the buffalo Application. 56 PreHandlers []http.Handler 57 // PreWare takes an http.Handler and returns and http.Handler 58 // and acts as a pseudo-middleware between the http.Server and 59 // a Buffalo application. 60 PreWares []PreWare 61 62 Context context.Context 63 64 // LooseSlash defines the trailing slash behavior for new routes. The initial value is false. 65 // This is the opposite of http://www.gorillatoolkit.org/pkg/mux#Router.StrictSlash 66 LooseSlash bool 67 68 cancel context.CancelFunc 69 Prefix string 70 } 71 72 // PreWare takes an http.Handler and returns and http.Handler 73 // and acts as a pseudo-middleware between the http.Server and 74 // a Buffalo application. 75 type PreWare func(http.Handler) http.Handler 76 77 // NewOptions returns a new Options instance with sensible defaults 78 func NewOptions() Options { 79 return optionsWithDefaults(Options{}) 80 } 81 82 func optionsWithDefaults(opts Options) Options { 83 opts.Env = defaults.String(opts.Env, envy.Get("GO_ENV", "development")) 84 opts.LogLevel = defaults.String(opts.LogLevel, "debug") 85 opts.Name = defaults.String(opts.Name, "/") 86 addr := "0.0.0.0" 87 if opts.Env == "development" { 88 addr = "127.0.0.1" 89 } 90 envAddr := envy.Get("ADDR", addr) 91 92 if strings.HasPrefix(envAddr, "unix:") { 93 // UNIX domain socket doesn't have a port 94 opts.Addr = envAddr 95 } else { 96 // TCP case 97 opts.Addr = defaults.String(opts.Addr, fmt.Sprintf("%s:%s", envAddr, envy.Get("PORT", "3000"))) 98 } 99 100 if opts.PreWares == nil { 101 opts.PreWares = []PreWare{} 102 } 103 if opts.PreHandlers == nil { 104 opts.PreHandlers = []http.Handler{} 105 } 106 107 if opts.Context == nil { 108 opts.Context = context.Background() 109 } 110 opts.Context, opts.cancel = context.WithCancel(opts.Context) 111 112 if opts.Logger == nil { 113 opts.Logger = NewLogger(opts.LogLevel) 114 } 115 116 pop.Log = func(s string, args ...interface{}) { 117 if pop.Debug { 118 l := opts.Logger 119 if len(args) > 0 { 120 for i, a := range args { 121 l = l.WithField(fmt.Sprintf("$%d", i+1), a) 122 } 123 } 124 if pop.Color { 125 s = color.YellowString(s) 126 } 127 l.Debug(s) 128 } 129 } 130 131 if opts.SessionStore == nil { 132 secret := envy.Get("SESSION_SECRET", "") 133 // In production a SESSION_SECRET must be set! 134 if opts.Env == "production" && secret == "" { 135 logrus.Warn("Unless you set SESSION_SECRET env variable, your session storage is not protected!") 136 } 137 opts.SessionStore = sessions.NewCookieStore([]byte(secret)) 138 } 139 if opts.Worker == nil { 140 w := worker.NewSimpleWithContext(opts.Context) 141 w.Logger = opts.Logger 142 opts.Worker = w 143 } 144 opts.SessionName = defaults.String(opts.SessionName, "_buffalo_session") 145 opts.Host = defaults.String(opts.Host, envy.Get("HOST", fmt.Sprintf("http://127.0.0.1:%s", envy.Get("PORT", "3000")))) 146 return opts 147 }