mirror of
https://github.com/volatiletech/authboss.git
synced 2024-11-30 09:06:45 +02:00
Clean up defaults package a bit
This commit is contained in:
parent
9eb5731a3d
commit
7f97d632d0
@ -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
|
||||
|
@ -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{
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user