diff --git a/defaults/defaults.go b/defaults/defaults.go index 0a6b997..6859335 100644 --- a/defaults/defaults.go +++ b/defaults/defaults.go @@ -13,16 +13,16 @@ import ( "github.com/volatiletech/authboss" ) -// SetDefaultCore creates instances of all the default pieces -// -// Assumes you have a ViewRenderer already set. -func SetDefaultCore(config *authboss.Config, useUsername bool) { +// SetCore creates instances of all the default pieces +// with the exception of ViewRenderer which should be already set +// before calling this method. +func SetCore(config *authboss.Config, useUsername bool) { logger := NewLogger(os.Stdout) config.Core.Router = NewRouter() - config.Core.ErrorHandler = ErrorHandler{LogWriter: logger} - config.Core.Responder = &Responder{Renderer: config.Core.ViewRenderer} - config.Core.Redirector = &Redirector{Renderer: config.Core.ViewRenderer, FormValueName: "redir"} + config.Core.ErrorHandler = NewErrorHandler(logger) + config.Core.Responder = NewResponder(config.Core.ViewRenderer) + config.Core.Redirector = NewRedirector(config.Core.ViewRenderer, RedirectFormValueName) config.Core.BodyReader = NewHTTPFormReader(useUsername) config.Core.Mailer = NewLogMailer(os.Stdout) config.Core.Logger = logger diff --git a/defaults/error_handler.go b/defaults/error_handler.go index 010e69c..0530534 100644 --- a/defaults/error_handler.go +++ b/defaults/error_handler.go @@ -16,6 +16,11 @@ type ErrorHandler struct { LogWriter authboss.Logger } +// NewErrorHandler constructor +func NewErrorHandler(logger authboss.Logger) ErrorHandler { + return ErrorHandler{LogWriter: logger} +} + // Wrap an http handler with an error func (e ErrorHandler) Wrap(handler func(w http.ResponseWriter, r *http.Request) error) http.Handler { return errorHandler{ diff --git a/defaults/responder.go b/defaults/responder.go index 55aa9cb..22d48f4 100644 --- a/defaults/responder.go +++ b/defaults/responder.go @@ -6,11 +6,22 @@ import ( "github.com/volatiletech/authboss" ) +const ( + // RedirectFormValueName is the name of the form field + // in the http request that will be used when redirecting + RedirectFormValueName = "redir" +) + // Responder helps respond to http requests type Responder struct { Renderer authboss.Renderer } +// NewResponder constructor +func NewResponder(renderer authboss.Renderer) *Responder { + return &Responder{Renderer: renderer} +} + // Respond to an HTTP request. It's main job is to merge data that comes in from // various middlewares via the context with the data sent by the controller and render that. func (r *Responder) Respond(w http.ResponseWriter, req *http.Request, code int, templateName string, data authboss.HTMLData) error { @@ -37,10 +48,15 @@ func isAPIRequest(r *http.Request) bool { // Redirector for http requests type Redirector struct { + Renderer authboss.Renderer + // FormValueName for the redirection FormValueName string +} - Renderer authboss.Renderer +// NewRedirector constructor +func NewRedirector(renderer authboss.Renderer, formValueName string) *Redirector { + return &Redirector{FormValueName: formValueName, Renderer: renderer} } // Redirect the client elsewhere. If it's an API request it will simply render