middleware

package
v0.7.23 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	UserContextKey = contextKey("user")
)

Functions

func FuncMap

func FuncMap(ss *models.SiteService, settings *settings.Settings) template.FuncMap

FuncMap some function that can be used in templates

func User

func User(r *http.Request) (*models.User, error)

User gets the user from the request context

Types

type AppContext

type AppContext struct {
	SessionService    *session.Service
	ArticleService    *models.ArticleService
	CategoryService   *models.CategoryService
	UserService       *models.UserService
	UserInviteService *models.UserInviteService
	SiteService       *models.SiteService
	FileService       *models.FileService
	TokenService      *models.TokenService
	Mailer            *models.Mailer
	ConfigService     *settings.Settings
	Templates         *template.Template
}

AppContext contains the services, session store, templates, ...

func (AppContext) AuthHandler

func (ctx AppContext) AuthHandler(handler http.Handler) http.Handler

AuthHandler checks if the user is authenticated; if not next handler in chain is not called

func (AppContext) RequireAdmin

func (ctx AppContext) RequireAdmin(handler http.Handler) http.Handler

RequireAdmin ensures that the user is an admin, if not next handler in chain is not called.

type Handler

Handler enriches handler with the AppContext

type JHandler

JHandler enriches handler with the AppContext

type JSONHandler

type JSONHandler struct {
	AppCtx  *AppContext
	Handler JHandler
}

JSONHandler marshals JSON and writes to the http response

func (JSONHandler) ServeHTTP

func (fn JSONHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)

type Template

type Template struct {
	Name         string
	Active       string
	Data         map[string]interface{}
	SuccessMsg   string
	WarnMsg      string
	RedirectPath string
	Err          error
}

Template contains the information about the template to render. Active contains the current active navigation. Data the data which is injected into the templates. SuccessMsg is an optional variable which is displayed as a green message. WarnMsg is an optional variable which is displayed as an orange message. RedirectPath contains the path where the request should be redirected. Err will be shown as red message in templates. If it's a httperror, the display message will be shown, otherwise generich 'An internal error occurred' is shown.

func NotFound

func NotFound(ctx *AppContext, rw http.ResponseWriter, r *http.Request) *Template

NotFound returned if no route matches

func (Template) RedirectURL

func (t Template) RedirectURL() string

RedirectURL builds a URL for redirecting

type TemplateHandler

type TemplateHandler struct {
	AppCtx  *AppContext
	Handler Handler
}

TemplateHandler enriches handlers with a application context containing 'services'

func (TemplateHandler) ServeHTTP

func (fn TemplateHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)

type Templates

type Templates struct {
	Directory string
	FuncMap   template.FuncMap
}

Templates defines the directory where the templates are located, the FuncMap are additional functions, which can be used in the templates.

func (Templates) Load

func (ts Templates) Load() (*template.Template, error)

Load walks threw directory and parses templates ending with html

type XHandler

XHandler enriches handler with the AppContext

type XMLHandler

type XMLHandler struct {
	AppCtx  *AppContext
	Handler XHandler
}

XMLHandler marshals XML and writes to the http response

func (XMLHandler) ServeHTTP

func (fn XMLHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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