github.com/cheikhshift/buffalo@v0.9.5/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  	// PreHandlers are http.Handlers that are called between the http.Server
    45  	// and the buffalo Application.
    46  	PreHandlers []http.Handler
    47  	// PreWare takes an http.Handler and returns and http.Handler
    48  	// and acts as a pseudo-middleware between the http.Server and
    49  	// a Buffalo application.
    50  	PreWares []PreWare
    51  
    52  	Context context.Context
    53  	cancel  context.CancelFunc
    54  	Prefix  string
    55  }
    56  
    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  type PreWare func(http.Handler) http.Handler
    61  
    62  // NewOptions returns a new Options instance with sensible defaults
    63  func NewOptions() Options {
    64  	return optionsWithDefaults(Options{})
    65  }
    66  
    67  func optionsWithDefaults(opts Options) Options {
    68  	opts.Env = defaults.String(opts.Env, envy.Get("GO_ENV", "development"))
    69  	opts.LogLevel = defaults.String(opts.LogLevel, "debug")
    70  	opts.Name = defaults.String(opts.Name, "/")
    71  
    72  	if opts.PreWares == nil {
    73  		opts.PreWares = []PreWare{}
    74  	}
    75  	if opts.PreHandlers == nil {
    76  		opts.PreHandlers = []http.Handler{}
    77  	}
    78  
    79  	if opts.Context == nil {
    80  		opts.Context = context.Background()
    81  	}
    82  	opts.Context, opts.cancel = context.WithCancel(opts.Context)
    83  
    84  	if opts.Logger == nil {
    85  		opts.Logger = NewLogger(opts.LogLevel)
    86  	}
    87  
    88  	pop.Log = func(s string, args ...interface{}) {
    89  		if pop.Debug {
    90  			l := opts.Logger
    91  			if len(args) > 0 {
    92  				for i, a := range args {
    93  					l = l.WithField(fmt.Sprintf("$%d", i+1), a)
    94  				}
    95  			}
    96  			if pop.Color {
    97  				s = color.YellowString(s)
    98  			}
    99  			l.Debug(s)
   100  		}
   101  	}
   102  
   103  	if opts.SessionStore == nil {
   104  		secret := envy.Get("SESSION_SECRET", "")
   105  		// In production a SESSION_SECRET must be set!
   106  		if opts.Env == "production" && secret == "" {
   107  			log.Println("WARNING! Unless you set SESSION_SECRET env variable, your session storage is not protected!")
   108  		}
   109  		opts.SessionStore = sessions.NewCookieStore([]byte(secret))
   110  	}
   111  	if opts.Worker == nil {
   112  		w := worker.NewSimpleWithContext(opts.Context)
   113  		w.Logger = opts.Logger
   114  		opts.Worker = w
   115  	}
   116  	opts.SessionName = defaults.String(opts.SessionName, "_buffalo_session")
   117  	opts.Host = defaults.String(opts.Host, envy.Get("HOST", fmt.Sprintf("http://127.0.0.1:%s", envy.Get("PORT", "3000"))))
   118  	return opts
   119  }