github.com/mayra-cabrera/buffalo@v0.9.4-0.20170814145312-66d2e7772f11/options.go (about) 1 package buffalo 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "net/http" 8 9 "github.com/fatih/color" 10 "github.com/gobuffalo/buffalo/worker" 11 "github.com/gobuffalo/envy" 12 "github.com/gorilla/sessions" 13 "github.com/markbates/going/defaults" 14 "github.com/markbates/pop" 15 ) 16 17 // Options are used to configure and define how your application should run. 18 type Options struct { 19 Name string 20 // Env is the "environment" in which the App is running. Default is "development". 21 Env string 22 // LogLevel defaults to "debug". 23 LogLevel string 24 // Logger to be used with the application. A default one is provided. 25 Logger Logger 26 // MethodOverride allows for changing of the request method type. See the default 27 // implementation at buffalo.MethodOverride 28 MethodOverride http.HandlerFunc 29 // SessionStore is the `github.com/gorilla/sessions` store used to back 30 // the session. It defaults to use a cookie store and the ENV variable 31 // `SESSION_SECRET`. 32 SessionStore sessions.Store 33 // SessionName is the name of the session cookie that is set. This defaults 34 // to "_buffalo_session". 35 SessionName string 36 // Host that this application will be available at. Default is "http://127.0.0.1:[$PORT|3000]". 37 Host string 38 // Worker implements the Worker interface and can process tasks in the background. 39 // Default is "github.com/gobuffalo/worker.Simple. 40 Worker worker.Worker 41 // WorkerOff tells App.Start() whether to start the Worker process or not. Default is "false". 42 WorkerOff bool 43 44 Context context.Context 45 cancel context.CancelFunc 46 Prefix string 47 } 48 49 // NewOptions returns a new Options instance with sensible defaults 50 func NewOptions() Options { 51 return optionsWithDefaults(Options{}) 52 } 53 54 func optionsWithDefaults(opts Options) Options { 55 opts.Env = defaults.String(opts.Env, envy.Get("GO_ENV", "development")) 56 opts.LogLevel = defaults.String(opts.LogLevel, "debug") 57 opts.Name = defaults.String(opts.Name, "/") 58 59 if opts.Context == nil { 60 opts.Context = context.Background() 61 } 62 opts.Context, opts.cancel = context.WithCancel(opts.Context) 63 64 if opts.Logger == nil { 65 opts.Logger = NewLogger(opts.LogLevel) 66 } 67 68 pop.Log = func(s string, args ...interface{}) { 69 if pop.Debug { 70 l := opts.Logger 71 if len(args) > 0 { 72 for i, a := range args { 73 l = l.WithField(fmt.Sprintf("$%d", i+1), a) 74 } 75 } 76 if pop.Color { 77 s = color.YellowString(s) 78 } 79 l.Debug(s) 80 } 81 } 82 83 if opts.SessionStore == nil { 84 secret := envy.Get("SESSION_SECRET", "") 85 // In production a SESSION_SECRET must be set! 86 if opts.Env == "production" && secret == "" { 87 log.Println("WARNING! Unless you set SESSION_SECRET env variable, your session storage is not protected!") 88 } 89 opts.SessionStore = sessions.NewCookieStore([]byte(secret)) 90 } 91 if opts.Worker == nil { 92 w := worker.NewSimpleWithContext(opts.Context) 93 w.Logger = opts.Logger 94 opts.Worker = w 95 } 96 opts.SessionName = defaults.String(opts.SessionName, "_buffalo_session") 97 opts.Host = defaults.String(opts.Host, envy.Get("HOST", fmt.Sprintf("http://127.0.0.1:%s", envy.Get("PORT", "3000")))) 98 return opts 99 }