github.com/lenfree/buffalo@v0.7.3-0.20170207163156-891616ea4064/options.go (about)

     1  package buffalo
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  
     8  	"github.com/gobuffalo/envy"
     9  	"github.com/gorilla/sessions"
    10  	"github.com/markbates/going/defaults"
    11  )
    12  
    13  // Options are used to configure and define how your application should run.
    14  type Options struct {
    15  	// Env is the "environment" in which the App is running. Default is "development".
    16  	Env string
    17  	// LogLevel defaults to "debug".
    18  	LogLevel string
    19  	// Logger to be used with the application. A default one is provided.
    20  	Logger Logger
    21  	// MethodOverride allows for changing of the request method type. See the default
    22  	// implementation at buffalo.MethodOverride
    23  	MethodOverride http.HandlerFunc
    24  	// SessionStore is the `github.com/gorilla/sessions` store used to back
    25  	// the session. It defaults to use a cookie store and the ENV variable
    26  	// `SESSION_SECRET`.
    27  	SessionStore sessions.Store
    28  	// SessionName is the name of the session cookie that is set. This defaults
    29  	// to "_buffalo_session".
    30  	SessionName string
    31  	// Host that this application will be available at. Default is "http://127.0.0.1:[$PORT|3000]".
    32  	Host   string
    33  	prefix string
    34  }
    35  
    36  // NewOptions returns a new Options instance with sensible defaults
    37  func NewOptions() Options {
    38  	return optionsWithDefaults(Options{})
    39  }
    40  
    41  func optionsWithDefaults(opts Options) Options {
    42  	opts.Env = defaults.String(opts.Env, envy.Get("GO_ENV", "development"))
    43  	opts.LogLevel = defaults.String(opts.LogLevel, "debug")
    44  
    45  	if opts.Logger == nil {
    46  		opts.Logger = NewLogger(opts.LogLevel)
    47  	}
    48  
    49  	if opts.SessionStore == nil {
    50  		secret := envy.Get("SESSION_SECRET", "")
    51  		// In production a SESSION_SECRET must be set!
    52  		if opts.Env == "production" && secret == "" {
    53  			log.Println("WARNING! Unless you set SESSION_SECRET env variable, your session storage is not protected!")
    54  		}
    55  		opts.SessionStore = sessions.NewCookieStore([]byte(secret))
    56  	}
    57  	opts.SessionName = defaults.String(opts.SessionName, "_buffalo_session")
    58  	opts.Host = defaults.String(opts.Host, fmt.Sprintf("http://127.0.0.1:%s", envy.Get("PORT", "3000")))
    59  	return opts
    60  }