web

package
v0.1.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2023 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package web contains utilities to help build out a web service.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleError

func HandleError(w http.ResponseWriter, err error, logger golog.Logger, context ...string) bool

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

type ErrorResponse interface {
	Error() string
	Status() int
}

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

type PanicCapture struct {
	Logger golog.Logger
}

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 Session

type Session struct {
	Data bson.M
	// contains filtered or unexported fields
}

Session representation of a session.

func (*Session) Save

Save saves a session back to the store it came freom.

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.

func (*SessionManager) Get

func (sm *SessionManager) Get(r *http.Request, createIfNotExist bool) (*Session, error)

Get get a session from the request via cookies.

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

func DirectTemplate(t *template.Template) *Template

DirectTemplate creates a template to say use this specific template.

func NamedTemplate

func NamedTemplate(called string) *Template

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

type TemplateManager interface {
	LookupTemplate(name string) (*template.Template, error)
}

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) GetBool

func (u *UserInfo) GetBool(name string) bool

GetBool gets an attribute as a bool.

func (*UserInfo) GetEmail

func (u *UserInfo) GetEmail() string

GetEmail the email.

func (*UserInfo) GetEmailVerified

func (u *UserInfo) GetEmailVerified() bool

GetEmailVerified if the email h as been verified.

Directories

Path Synopsis
Package cors wraps the cors package with needed defaults.
Package cors wraps the cors package with needed defaults.
Package protojson provides helpers to marshal proto.Message to json
Package protojson provides helpers to marshal proto.Message to json

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL