handler

package
v0.0.0-...-4ffef1b Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2025 License: MIT Imports: 41 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoIndex = errors.New("no index file")

Functions

func ErrorMessage

func ErrorMessage(err error) i18n.Message

func ErrorStatus

func ErrorStatus(err error) int

func NewFileServer

func NewFileServer(mux *router.ServeMux, renderer *Renderer, errorHandler FileServerErrorHandler) http.HandlerFunc

func NewTemplateFuncs

func NewTemplateFuncs(custom template.FuncMap) template.FuncMap

func TmplAdd

func TmplAdd(values ...any) (int, error)

func TmplAddf

func TmplAddf(values ...any) (float64, error)

func TmplAtoi

func TmplAtoi(a string) int

func TmplDiv

func TmplDiv(values ...any) (int, error)

func TmplDivf

func TmplDivf(values ...any) (float64, error)

func TmplFields

func TmplFields(str string) []string

func TmplFormatDuration

func TmplFormatDuration(d time.Duration) string

func TmplFormatDurationStat

func TmplFormatDurationStat(d time.Duration) string

func TmplFormatSizeIEC

func TmplFormatSizeIEC(bytes any) (string, error)

func TmplFormatSizeSI

func TmplFormatSizeSI(bytes any) (string, error)

func TmplFormatTime

func TmplFormatTime(t time.Time, format string) string

func TmplGetOr

func TmplGetOr(src any, key string, fallback any) any

func TmplHasPrefix

func TmplHasPrefix(value, prefix any) bool

func TmplHasString

func TmplHasString(haystack []string, value any) bool

func TmplHasSuffix

func TmplHasSuffix(value, suffix any) bool

func TmplInts

func TmplInts(start, end int) []int

func TmplJoin

func TmplJoin(strs []string, sep string) string

func TmplMap

func TmplMap(pairs ...any) (map[string]any, error)

func TmplMarshalIndentJSON

func TmplMarshalIndentJSON(value any, prefix, indent string) (string, error)

func TmplMarshalJSON

func TmplMarshalJSON(value any) (string, error)

func TmplMax

func TmplMax(values ...any) (int, error)

func TmplMaxf

func TmplMaxf(values ...any) (float64, error)

func TmplMin

func TmplMin(values ...any) (int, error)

func TmplMinf

func TmplMinf(values ...any) (float64, error)

func TmplMod

func TmplMod(values ...any) (int, error)

func TmplMul

func TmplMul(values ...any) (int, error)

func TmplMulf

func TmplMulf(values ...any) (float64, error)

func TmplNeg

func TmplNeg(x any) (int, error)

func TmplNegf

func TmplNegf(x any) (float64, error)

func TmplQueryReplace

func TmplQueryReplace(q url.Values, pairs ...any) (template.URL, error)

func TmplQueryString

func TmplQueryString(q url.Values) template.URL

func TmplReplaceAll

func TmplReplaceAll(value any, old, new string) string

func TmplSlice

func TmplSlice(elements ...any) []any

func TmplSliceContains

func TmplSliceContains(haystack []any, needle any) bool

func TmplSplit

func TmplSplit(str, sep string) []string

func TmplStatusText

func TmplStatusText(code int) string

func TmplSub

func TmplSub(values ...any) (int, error)

func TmplSubf

func TmplSubf(values ...any) (float64, error)

func TmplTimeSince

func TmplTimeSince(t time.Time) time.Duration

func TmplToStrings

func TmplToStrings(value any) ([]string, error)

func TmplTrimPrefix

func TmplTrimPrefix(value, prefix any) string

func TmplTrimSuffix

func TmplTrimSuffix(value, suffix any) string

func TmplUnescapeHTML

func TmplUnescapeHTML(s string) template.HTML

func TmplUnescapeHTMLAttr

func TmplUnescapeHTMLAttr(s string) template.HTMLAttr

func TmplUnescapeJS

func TmplUnescapeJS(s string) template.JS

Types

type AccountReader

type AccountReader interface {
	account.Reader

	FindRoleByName(ctx context.Context, name string) (*account.Role, error)
	FindRoles(ctx context.Context, sortTopID int) ([]*account.Role, int, error)
	FindRolesPageBySearch(ctx context.Context, page, size, sortTopID int, sorts []string, search string) ([]*account.Role, int, error)

	CountUsersByRoleID(ctx context.Context, roleID int) (int, error)
	FindUsersPageBySearch(ctx context.Context, page, size, sortTopID int, sorts []string, search string) ([]*account.User, int, error)
}

type AppData

type AppData struct {
	Name        string
	ShortName   string
	Description string
	ThemeColor  string
	BasePath    string
}

type AssetPipeline

type AssetPipeline struct {
	// contains filtered or unexported fields
}
func (a *AssetPipeline) CSSLinks() string

func (*AssetPipeline) HTMLTemplates

func (a *AssetPipeline) HTMLTemplates() string

func (*AssetPipeline) JSImportMap

func (a *AssetPipeline) JSImportMap() string

func (*AssetPipeline) JSImports

func (a *AssetPipeline) JSImports() string

func (*AssetPipeline) Load

func (a *AssetPipeline) Load(asset string, args ...string) any

func (*AssetPipeline) Prefetch

func (a *AssetPipeline) Prefetch(asset string) string

func (*AssetPipeline) Prefetches

func (a *AssetPipeline) Prefetches() string

func (*AssetPipeline) Preload

func (a *AssetPipeline) Preload(asset string) string

func (*AssetPipeline) Preloads

func (a *AssetPipeline) Preloads() string

func (*AssetPipeline) Tag

func (a *AssetPipeline) Tag(asset string) string

func (*AssetPipeline) TagJSImport

func (a *AssetPipeline) TagJSImport(asset string) string
func (a *AssetPipeline) WriteCSSLinks() template.HTML

func (*AssetPipeline) WriteHTMLTemplates

func (a *AssetPipeline) WriteHTMLTemplates() template.HTML

func (*AssetPipeline) WriteJSImportMap

func (a *AssetPipeline) WriteJSImportMap() template.HTML

func (*AssetPipeline) WriteJSImports

func (a *AssetPipeline) WriteJSImports() template.HTML

func (*AssetPipeline) WritePrefetches

func (a *AssetPipeline) WritePrefetches() template.HTML

func (*AssetPipeline) WritePreloads

func (a *AssetPipeline) WritePreloads() template.HTML

type CSRF

type CSRF struct {
	Ctx context.Context
}

func (CSRF) Token

func (c CSRF) Token() string

type FileServerErrorHandler

type FileServerErrorHandler func(w http.ResponseWriter, r *http.Request, err error)

type Form

type Form struct {
	url.Values
}

func (Form) GetAll

func (f Form) GetAll(key string) []string

func (Form) GetAllOr

func (f Form) GetAllOr(key string, fallback any) ([]string, error)

func (Form) GetIndexOr

func (f Form) GetIndexOr(i int, key string, fallback any) string

func (Form) GetOr

func (f Form) GetOr(key string, fallback any) string

type GuardPredicateFunc

type GuardPredicateFunc func(p guard.Passport) bool

type Handler

type Handler struct {
	*Tenant

	Session Session
	// contains filtered or unexported fields
}

func New

func New(tenant *Tenant) *Handler

func (*Handler) AttachContext

func (h *Handler) AttachContext(next http.HandlerFunc) http.HandlerFunc

func (*Handler) AttachContextLogger

func (h *Handler) AttachContextLogger(next http.HandlerFunc) http.HandlerFunc

func (*Handler) Config

func (h *Handler) Config(ctx context.Context) *system.Config

func (*Handler) Locale

func (h *Handler) Locale(ctx context.Context) string

func (*Handler) Logger

func (h *Handler) Logger(ctx context.Context) *slog.Logger

func (*Handler) Passport

func (h *Handler) Passport(ctx context.Context) guard.Passport

func (*Handler) PassportByEmail

func (h *Handler) PassportByEmail(ctx context.Context, email string) (guard.Passport, error)

func (*Handler) PassportByUser

func (h *Handler) PassportByUser(ctx context.Context, user *account.User) guard.Passport

func (*Handler) RenewSession

func (h *Handler) RenewSession(ctx context.Context) ([]byte, error)

func (*Handler) SendEmail

func (h *Handler) SendEmail(ctx context.Context, templateFiles fs.FS, templatePatterns TemplatePatternsFunc, funcs template.FuncMap, wrapI18nRuntime WrapI18nRuntimeFunc, from, to, view string, vars Vars) error

func (*Handler) SendSMS

func (h *Handler) SendSMS(ctx context.Context, to, body string) error

func (*Handler) SendTOTPSMS

func (h *Handler) SendTOTPSMS(email, tel string) error

func (*Handler) Template

func (h *Handler) Template(files fs.FS, patterns TemplatePatternsFunc, funcs template.FuncMap, name string) *template.Template

func (*Handler) User

func (h *Handler) User(ctx context.Context) *account.User

type I18nRuntime

type I18nRuntime struct {
	i18n.Runtime
	// contains filtered or unexported fields
}
func (r I18nRuntime) Link(label, href, target i18n.Value) i18n.RawString

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func (Logger) Error

func (l Logger) Error(msg string, args ...any) string

func (Logger) Info

func (l Logger) Info(msg string, args ...any) string

func (Logger) Warn

func (l Logger) Warn(msg string, args ...any) string

type Mux

type Mux interface {
	TryPath(name string, paramArgPairs ...any) (string, error)
}

type Query

type Query struct {
	url.Values
}

func (Query) GetAll

func (q Query) GetAll(key string) []string

func (Query) GetAllOr

func (q Query) GetAllOr(key string, fallback any) ([]string, error)

func (Query) GetOr

func (q Query) GetOr(key string, fallback any) string

func (Query) Replace

func (q Query) Replace(pairs ...any) (template.URL, error)

func (Query) String

func (q Query) String() template.URL

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

func NewRenderer

func NewRenderer(config RendererConfig) *Renderer

func (*Renderer) Asset

func (rn *Renderer) Asset(r *http.Request, ap *AssetPipeline, upath string) (string, time.Time, []byte, error)

func (*Renderer) ErrorView

func (rn *Renderer) ErrorView(w http.ResponseWriter, r *http.Request, msg string, err error, view string, vars Vars)

func (*Renderer) ErrorViewFunc

func (rn *Renderer) ErrorViewFunc(w http.ResponseWriter, r *http.Request, msg string, err error, view string, dataFunc ViewDataFunc)

func (*Renderer) FindAssetByTagged

func (rn *Renderer) FindAssetByTagged(tagged string) (string, bool)

func (*Renderer) FindTaggedByAsset

func (rn *Renderer) FindTaggedByAsset(asset string) (string, bool)

func (*Renderer) HTML

func (rn *Renderer) HTML(buf *bytes.Buffer, assetPipeline *AssetPipeline, r *http.Request, status int, html string, vars Vars) error

func (*Renderer) SetViewVars

func (rn *Renderer) SetViewVars(name string, vars ViewVarsFunc)

func (*Renderer) StreamView

func (rn *Renderer) StreamView(w http.ResponseWriter, r *http.Request, status int, view string, vars Vars) func()

func (*Renderer) TagAsset

func (rn *Renderer) TagAsset(key, asset, tagged string)

func (*Renderer) Text

func (rn *Renderer) Text(buf *bytes.Buffer, assetPipeline *AssetPipeline, r *http.Request, status int, text string, vars Vars) error

func (*Renderer) View

func (rn *Renderer) View(w http.ResponseWriter, r *http.Request, status int, view string, vars Vars)

func (*Renderer) ViewFunc

func (rn *Renderer) ViewFunc(w http.ResponseWriter, r *http.Request, status int, view string, dataFunc ViewDataFunc)

type RendererConfig

type RendererConfig struct {
	Handler          *Handler
	AssetTags        *cache.Cache[string, string]
	AssetFiles       fs.FS
	TemplateFiles    fs.FS
	TemplatePatterns TemplatePatternsFunc
	Funcs            template.FuncMap
	T                TFunc
	WrapI18nRuntime  WrapI18nRuntimeFunc
	Process          TemplateProcessFunc
	ViewErrorFunc    ViewErrorFunc
}

type Repo

type Repo struct {
	Account AccountReader
	System  SystemReader
	Web     WebReadWriter
}

type Session

type Session struct {
	*session.Manager
}

func (*Session) DeleteEmail

func (s *Session) DeleteEmail(ctx context.Context)

func (*Session) DeleteFlash

func (s *Session) DeleteFlash(ctx context.Context)

func (*Session) DeleteFlashError

func (s *Session) DeleteFlashError(ctx context.Context)

func (*Session) DeleteFlashImportant

func (s *Session) DeleteFlashImportant(ctx context.Context)

func (*Session) DeleteFlashWarning

func (s *Session) DeleteFlashWarning(ctx context.Context)

func (*Session) DeleteHasActivatedTOTP

func (s *Session) DeleteHasActivatedTOTP(ctx context.Context)

func (*Session) DeleteHighlightID

func (s *Session) DeleteHighlightID(ctx context.Context)

func (*Session) DeleteIsAwaitingTOTP

func (s *Session) DeleteIsAwaitingTOTP(ctx context.Context)

func (*Session) DeleteIsSignedIn

func (s *Session) DeleteIsSignedIn(ctx context.Context)

func (*Session) DeleteKnownPasswordBreachCount

func (s *Session) DeleteKnownPasswordBreachCount(ctx context.Context)

func (*Session) DeleteLastSignInAttemptAt

func (s *Session) DeleteLastSignInAttemptAt(ctx context.Context)

func (*Session) DeleteRedirect

func (s *Session) DeleteRedirect(ctx context.Context)

func (*Session) DeleteSignInAttempts

func (s *Session) DeleteSignInAttempts(ctx context.Context)

func (*Session) DeleteSortTopID

func (s *Session) DeleteSortTopID(ctx context.Context)

func (*Session) DeleteTOTPMethod

func (s *Session) DeleteTOTPMethod(ctx context.Context)

func (*Session) DeleteUserID

func (s *Session) DeleteUserID(ctx context.Context)

func (*Session) Email

func (s *Session) Email(ctx context.Context) string

func (*Session) Flash

func (s *Session) Flash(ctx context.Context) []string

func (*Session) FlashError

func (s *Session) FlashError(ctx context.Context) []string

func (*Session) FlashImportant

func (s *Session) FlashImportant(ctx context.Context) []string

func (*Session) FlashWarning

func (s *Session) FlashWarning(ctx context.Context) []string

func (*Session) HasActivatedTOTP

func (s *Session) HasActivatedTOTP(ctx context.Context) bool

func (*Session) HighlightID

func (s *Session) HighlightID(ctx context.Context) int

func (*Session) IsAwaitingTOTP

func (s *Session) IsAwaitingTOTP(ctx context.Context) bool

func (*Session) IsSignedIn

func (s *Session) IsSignedIn(ctx context.Context) bool

func (*Session) KnownPasswordBreachCount

func (s *Session) KnownPasswordBreachCount(ctx context.Context) int

func (*Session) LastSignInAttemptAt

func (s *Session) LastSignInAttemptAt(ctx context.Context) time.Time

func (*Session) LastView

func (s *Session) LastView(ctx context.Context) string

func (*Session) PopEmail

func (s *Session) PopEmail(ctx context.Context) string

func (*Session) PopFlash

func (s *Session) PopFlash(ctx context.Context) []string

func (*Session) PopFlashError

func (s *Session) PopFlashError(ctx context.Context) []string

func (*Session) PopFlashImportant

func (s *Session) PopFlashImportant(ctx context.Context) []string

func (*Session) PopFlashWarning

func (s *Session) PopFlashWarning(ctx context.Context) []string

func (*Session) PopHasActivatedTOTP

func (s *Session) PopHasActivatedTOTP(ctx context.Context) bool

func (*Session) PopHighlightID

func (s *Session) PopHighlightID(ctx context.Context) int

func (*Session) PopIsAwaitingTOTP

func (s *Session) PopIsAwaitingTOTP(ctx context.Context) bool

func (*Session) PopIsSignedIn

func (s *Session) PopIsSignedIn(ctx context.Context) bool

func (*Session) PopKnownPasswordBreachCount

func (s *Session) PopKnownPasswordBreachCount(ctx context.Context) int

func (*Session) PopLastSignInAttemptAt

func (s *Session) PopLastSignInAttemptAt(ctx context.Context) time.Time

func (*Session) PopLastView

func (s *Session) PopLastView(ctx context.Context) string

func (*Session) PopRedirect

func (s *Session) PopRedirect(ctx context.Context) string

func (*Session) PopSignInAttempts

func (s *Session) PopSignInAttempts(ctx context.Context) int

func (*Session) PopSortTopID

func (s *Session) PopSortTopID(ctx context.Context) int

func (*Session) PopTOTPMethod

func (s *Session) PopTOTPMethod(ctx context.Context) string

func (*Session) PopURLValues

func (s *Session) PopURLValues(ctx context.Context) url.Values

func (*Session) PopUserID

func (s *Session) PopUserID(ctx context.Context) int

func (*Session) Redirect

func (s *Session) Redirect(ctx context.Context) string

func (*Session) SetEmail

func (s *Session) SetEmail(ctx context.Context, value string)

func (*Session) SetFlash

func (s *Session) SetFlash(ctx context.Context, value []string)

func (*Session) SetFlashError

func (s *Session) SetFlashError(ctx context.Context, value []string)

func (*Session) SetFlashImportant

func (s *Session) SetFlashImportant(ctx context.Context, value []string)

func (*Session) SetFlashWarning

func (s *Session) SetFlashWarning(ctx context.Context, value []string)

func (*Session) SetHasActivatedTOTP

func (s *Session) SetHasActivatedTOTP(ctx context.Context, value bool)

func (*Session) SetHighlightID

func (s *Session) SetHighlightID(ctx context.Context, value int)

func (*Session) SetIsAwaitingTOTP

func (s *Session) SetIsAwaitingTOTP(ctx context.Context, value bool)

func (*Session) SetIsSignedIn

func (s *Session) SetIsSignedIn(ctx context.Context, value bool)

func (*Session) SetKnownPasswordBreachCount

func (s *Session) SetKnownPasswordBreachCount(ctx context.Context, value int)

func (*Session) SetLastSignInAttemptAt

func (s *Session) SetLastSignInAttemptAt(ctx context.Context, value time.Time)

func (*Session) SetLastView

func (s *Session) SetLastView(ctx context.Context, value string)

func (*Session) SetRedirect

func (s *Session) SetRedirect(ctx context.Context, value string)

func (*Session) SetSignInAttempts

func (s *Session) SetSignInAttempts(ctx context.Context, value int)

func (*Session) SetSortTopID

func (s *Session) SetSortTopID(ctx context.Context, value int)

func (*Session) SetTOTPMethod

func (s *Session) SetTOTPMethod(ctx context.Context, value string)

func (*Session) SetURLValues

func (s *Session) SetURLValues(ctx context.Context, value url.Values)

func (*Session) SetUserID

func (s *Session) SetUserID(ctx context.Context, value int)

func (*Session) SignInAttempts

func (s *Session) SignInAttempts(ctx context.Context) int

func (*Session) SortTopID

func (s *Session) SortTopID(ctx context.Context) int

func (*Session) TOTPMethod

func (s *Session) TOTPMethod(ctx context.Context) string

func (*Session) URLValues

func (s *Session) URLValues(ctx context.Context) url.Values

func (*Session) UserID

func (s *Session) UserID(ctx context.Context) int

type SessionData

type SessionData struct {
	// General session keys
	Flash          []string
	FlashWarning   []string
	FlashImportant []string
	FlashError     []string
	Redirect       string
	HighlightID    int

	// Account session keys
	UserID                   int
	Email                    string
	TOTPMethod               string
	HasActivatedTOTP         bool
	IsAwaitingTOTP           bool
	IsSignedIn               bool
	KnownPasswordBreachCount int
}

type State

type State struct {
	// contains filtered or unexported fields
}

func (*State) Get

func (s *State) Get(key string) any

func (*State) Once

func (s *State) Once(key string) bool

func (*State) Set

func (s *State) Set(key string, value any)

func (*State) Store

func (s *State) Store(key string, value any) bool

type Svc

type Svc struct {
	Account *account.Service
	System  *system.Service
}

type SystemReader

type SystemReader system.Reader

type TFunc

type TFunc func(ctx context.Context, message i18n.Message) string

type TemplatePatternsFunc

type TemplatePatternsFunc func(view string) []string

type TemplateProcessFunc

type TemplateProcessFunc func(w http.ResponseWriter, r *http.Request)

type Templater

type Templater interface {
	Execute(wr io.Writer, data any) error
}

type Tenant

type Tenant struct {
	Key              string
	Kind             string
	Scheme           string
	Host             string
	Hosts            map[string]string
	Data             string
	Dev              bool
	Insecure         bool
	IPWhitelist      []string
	Proxies          []string
	SMTPEnvelopeFrom string
	Broker           event.Broker
	Email            smtp.Mailer
	Logger           *slog.Logger
	Metrics          *expvar.Map

	Svc       Svc
	Repo      Repo
	SuperRole *account.Role
}

type URL

type URL struct {
	Scheme string
	Host   string
	Path   template.URL
	Query  Query
}

type Vars

type Vars map[string]any

func (Vars) Merge

func (v Vars) Merge(rhs Vars) Vars

type ViewData

type ViewData struct {
	Asset        *AssetPipeline
	View         string
	Stream       string
	Status       int
	CSRF         CSRF
	Locale       string
	I18nRuntime  i18n.Runtime
	ErrorMessage string
	Errors       errsx.Map
	Now          time.Time
	Form         Form
	URL          URL
	App          AppData
	Session      SessionData
	Config       *system.Config
	User         *account.User
	Passport     guard.Passport
	Props        map[string]any
	State        *State
	Log          Logger
	Vars         Vars
}

func (ViewData) T

func (v ViewData) T(msg any, args ...any) (any, error)

func (ViewData) WithProps

func (v ViewData) WithProps(pairs ...any) (ViewData, error)

type ViewDataFunc

type ViewDataFunc func(data *ViewData) error

type ViewErrorFunc

type ViewErrorFunc func(w http.ResponseWriter, r *http.Request, message string, err error)

type ViewVarsFunc

type ViewVarsFunc func(r *http.Request) (Vars, error)

type WebReadWriter

type WebReadWriter interface {
	session.ReadWriter

	AddEmailVerificationToken(ctx context.Context, email string, ttl time.Duration) (string, error)
	FindEmailVerificationTokenEmail(ctx context.Context, token string) (string, error)
	ConsumeEmailVerificationToken(ctx context.Context, token string) error

	AddResetPasswordToken(ctx context.Context, email string, ttl time.Duration) (string, error)
	FindResetPasswordTokenEmail(ctx context.Context, token string) (string, error)
	ConsumeResetPasswordToken(ctx context.Context, token string) error

	AddSignInMagicLinkToken(ctx context.Context, email string, ttl time.Duration) (string, error)
	FindSignInMagicLinkTokenEmail(ctx context.Context, token string) (string, error)
	ConsumeSignInMagicLinkToken(ctx context.Context, token string) error

	AddTOTPResetVerifyToken(ctx context.Context, email string, ttl time.Duration) (string, error)
	FindTOTPResetVerifyTokenEmail(ctx context.Context, token string) (string, error)
	ConsumeTOTPResetVerifyToken(ctx context.Context, token string) error

	AddResetTOTPToken(ctx context.Context, email string, ttl time.Duration) (string, error)
	FindResetTOTPTokenEmail(ctx context.Context, token string) (string, error)
	ConsumeResetTOTPToken(ctx context.Context, token string) error

	LogDomainEvent(ctx context.Context, kind, name, data string, createdAt time.Time) error
}

type WrapI18nRuntimeFunc

type WrapI18nRuntimeFunc func(rt i18n.Runtime) i18n.Runtime

func NewI18nRuntimeWrapper

func NewI18nRuntimeWrapper(mux Mux) WrapI18nRuntimeFunc

Jump to

Keyboard shortcuts

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