Documentation ¶
Overview ¶
Package web contains utilities to help build out a web service.
Index ¶
- func HandleError(w http.ResponseWriter, err error, logger golog.Logger, context ...string) bool
- func InstallAuth0(ctx context.Context, mux *goji.Mux, sessions *SessionManager, ...) (io.Closer, error)
- type APIHandler
- type APIMiddleware
- type Auth0Config
- type ErrorResponse
- type PanicCapture
- type Session
- type SessionManager
- type Store
- type Template
- type TemplateHandler
- type TemplateHandlerFunc
- type TemplateManager
- func NewTemplateManagerEmbed(fs fs.ReadDirFS, srcDir string) (TemplateManager, error)
- func NewTemplateManagerEmbedWithOptions(fs fs.ReadDirFS, srcDir string, opts protojson.MarshalingOptions) (TemplateManager, error)
- func NewTemplateManagerFS(srcDir string) (TemplateManager, error)
- func NewTemplateManagerFSWithOptions(srcDir string, opts protojson.MarshalingOptions) (TemplateManager, error)
- type TemplateMiddleware
- type UserInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HandleError ¶
HandleError returns true if there was an error and you should stop.
func InstallAuth0 ¶
func InstallAuth0( ctx context.Context, mux *goji.Mux, sessions *SessionManager, config Auth0Config, logger golog.Logger, ) (io.Closer, error)
InstallAuth0 does initial setup and installs routes for auth0.
Types ¶
type APIHandler ¶
type APIHandler interface { // return (result, error) // if both are null, do nothing ServeAPI(w http.ResponseWriter, r *http.Request) (interface{}, error) }
APIHandler what a user has to implement to use APIMiddleware.
type APIMiddleware ¶
type APIMiddleware struct { protojson.MarshalingOptions Handler APIHandler Logger golog.Logger // Recover from panics with a proper error logs. PanicCapture }
APIMiddleware simple layer between http.Handler interface that does json marshalling and error handling.
func NewAPIMiddleware ¶ added in v0.0.6
func NewAPIMiddleware(h APIHandler, logger golog.Logger) *APIMiddleware
NewAPIMiddleware returns a configured APIMiddleware with a panic capture configured.
func (*APIMiddleware) ServeHTTP ¶
func (am *APIMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP call the api.
type Auth0Config ¶
type Auth0Config struct { Domain string ClientID string Secret string BaseURL string EnableTest bool }
Auth0Config config for auth0.
type ErrorResponse ¶
ErrorResponse lets you specify a status code.
func ErrorResponseStatus ¶
func ErrorResponseStatus(code int) ErrorResponse
ErrorResponseStatus creates an error response with a specific code.
type PanicCapture ¶ added in v0.0.6
PanicCapture allows recovery during a request handler from panics. It prints a formatted log to the underlying logger.
func (*PanicCapture) Recover ¶ added in v0.0.6
func (p *PanicCapture) Recover(w http.ResponseWriter, r *http.Request)
Recover captures and prints the error if recover() has an error.
type SessionManager ¶
type SessionManager struct {
// contains filtered or unexported fields
}
SessionManager handles working with sessions from http.
func NewSessionManager ¶
func NewSessionManager(theStore Store, logger golog.Logger) *SessionManager
NewSessionManager creates a new SessionManager.
func (*SessionManager) DeleteSession ¶
func (sm *SessionManager) DeleteSession(ctx context.Context, r *http.Request, w http.ResponseWriter)
DeleteSession deletes a session.
type Store ¶
type Store interface { Delete(ctx context.Context, id string) error Get(ctx context.Context, id string) (*Session, error) Save(ctx context.Context, s *Session) error SetSessionManager(*SessionManager) }
Store actually stores raw data somewhere.
func NewMemorySessionStore ¶
func NewMemorySessionStore() Store
NewMemorySessionStore creates a new memory session store.
func NewMongoDBSessionStore ¶
func NewMongoDBSessionStore(coll *mongo.Collection) Store
NewMongoDBSessionStore new MongoDB backed store.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template specifies which template to render.
func DirectTemplate ¶
DirectTemplate creates a template to say use this specific template.
func NamedTemplate ¶
NamedTemplate creates a Template with a name.
type TemplateHandler ¶
type TemplateHandler interface { // return (template name, thing to pass to template, error) Serve(w http.ResponseWriter, r *http.Request) (*Template, interface{}, error) }
TemplateHandler implement this to be able to use middleware.
type TemplateHandlerFunc ¶
type TemplateHandlerFunc func(w http.ResponseWriter, r *http.Request) (*Template, interface{}, error)
TemplateHandlerFunc the func version of the handler.
func (TemplateHandlerFunc) Serve ¶
func (f TemplateHandlerFunc) Serve(w http.ResponseWriter, r *http.Request) (*Template, interface{}, error)
Serve does the work.
type TemplateManager ¶
TemplateManager responsible for managing, caching, finding templates.
func NewTemplateManagerEmbed ¶
func NewTemplateManagerEmbed(fs fs.ReadDirFS, srcDir string) (TemplateManager, error)
NewTemplateManagerEmbed creates a TemplateManager from an embedded file system.
func NewTemplateManagerEmbedWithOptions ¶ added in v0.0.6
func NewTemplateManagerEmbedWithOptions(fs fs.ReadDirFS, srcDir string, opts protojson.MarshalingOptions) (TemplateManager, error)
NewTemplateManagerEmbedWithOptions creates a TemplateManager from an embedded file system. Allows optional protojson.MarshalingOptions.
func NewTemplateManagerFS ¶
func NewTemplateManagerFS(srcDir string) (TemplateManager, error)
NewTemplateManagerFS creates a new TemplateManager from the file system.
func NewTemplateManagerFSWithOptions ¶ added in v0.0.6
func NewTemplateManagerFSWithOptions(srcDir string, opts protojson.MarshalingOptions) (TemplateManager, error)
NewTemplateManagerFSWithOptions creates a new TemplateManager from the file system. Allows optional protojson.MarshalingOptions.
type TemplateMiddleware ¶
type TemplateMiddleware struct { Templates TemplateManager Handler TemplateHandler Logger golog.Logger // Recover from panics with a proper error logs. PanicCapture }
TemplateMiddleware handles the rendering of the template from the data and finding of the template.
func NewTemplateMiddleware ¶ added in v0.0.6
func NewTemplateMiddleware(template TemplateManager, h TemplateHandler, logger golog.Logger) *TemplateMiddleware
NewTemplateMiddleware returns a configured TemplateMiddleWare with a panic capture configured.
func (*TemplateMiddleware) ServeHTTP ¶
func (tm *TemplateMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request)
type UserInfo ¶
type UserInfo struct { LoggedIn bool `json:"loggedIn"` Properties map[string]interface{} `json:"properties"` }
UserInfo basic info about a user from a session.
func GetLoggedInUserInfo ¶
func GetLoggedInUserInfo(sessions *SessionManager, r *http.Request) (UserInfo, error)
GetLoggedInUserInfo figures out if the session is associated with a user.
func (*UserInfo) GetEmailVerified ¶
GetEmailVerified if the email h as been verified.