Documentation ¶
Overview ¶
Package hypermedia provides templates and [http.Handler]s for rendering chat sessions.
Index ¶
- Constants
- Variables
- func AddFavIcon(mux *http.ServeMux, image []byte) string
- func AddFavIconIfAbsent(mux *http.ServeMux, image []byte) (found string)
- func DatastarHandler(w http.ResponseWriter, r *http.Request)
- func DatastarMapHandler(w http.ResponseWriter, r *http.Request)
- func FavIconHandler(w http.ResponseWriter, r *http.Request)
- func NewAsset(contentType string, body []byte) http.HandlerFunc
- func NewPage(source Renderable, eh ErrorHandler, bundle *i18n.Bundle) http.Handler
- func NewPageRenderer(head Head) func(Renderable) Renderable
- func NewStaticPage(ctx context.Context, r Renderable, b *i18n.Bundle, ...) http.Handler
- func SortLanguageTags(target []language.Tag, acceptLanguageHeader string) error
- type Error
- type ErrorHandler
- type ErrorHandlerFunc
- type ErrorRenderer
- type Handler
- type Head
- type Renderable
- type RenderableError
- type RendererFunc
Constants ¶
View Source
const ( ErrNotFound = renderableError(http.StatusNotFound) ErrForbidden = renderableError(http.StatusForbidden) ErrInternalServerError = renderableError(http.StatusInternalServerError) )
View Source
const PageContentType = "text/html; charset=utf-8"
Variables ¶
View Source
var DefaultErrorHandler = NewErrorPageHandler( i18n.NewBundle(DefaultLanguage), Head{}, []RenderableError{ ErrNotFound, ErrInternalServerError, })
View Source
var DefaultLanguage = language.AmericanEnglish
View Source
var ( ErrNotFoundRenderer = RendererFunc( func(ctx context.Context, w io.Writer, l *i18n.Localizer) error { title, err := l.Localize(&i18n.LocalizeConfig{ DefaultMessage: &i18n.Message{ ID: "hypermedia.NotFoundError.Title", Other: "Requested Page Does Not Exist", }, }) if err != nil { return err } description, err := l.Localize(&i18n.LocalizeConfig{ DefaultMessage: &i18n.Message{ ID: "hypermedia.NotFoundError.Description", Other: "There is no content for this link.", }, }) if err != nil { return err } return errorTemplate.Execute(w, errorTemplateValues{ Title: title, Description: description, }) }, ) )
View Source
var PlainTextErrorHandler = ErrorHandlerFunc( func(w http.ResponseWriter, r *http.Request, err error) { statusCode := http.StatusInternalServerError var errorWithStatusCode Error if errors.As(err, &errorWithStatusCode) { statusCode = errorWithStatusCode.HyperTextStatusCode() } http.Error(w, err.Error(), statusCode) }, )
Functions ¶
func AddFavIconIfAbsent ¶ added in v0.0.3
TODO: requires test! does not seem to work
func DatastarHandler ¶
func DatastarHandler(w http.ResponseWriter, r *http.Request)
func DatastarMapHandler ¶
func DatastarMapHandler(w http.ResponseWriter, r *http.Request)
func FavIconHandler ¶
func FavIconHandler(w http.ResponseWriter, r *http.Request)
func NewPage ¶ added in v0.0.3
func NewPage(source Renderable, eh ErrorHandler, bundle *i18n.Bundle) http.Handler
func NewPageRenderer ¶
func NewPageRenderer( head Head, ) func(Renderable) Renderable
func NewStaticPage ¶ added in v0.0.3
func SortLanguageTags ¶ added in v0.0.3
SortLanguageTags prioritizes a list of language tags based on HTTP Accept-Language header definition as used by language.ParseAcceptLanguage. More desirable languages are moved to the front.
Types ¶
type ErrorHandler ¶
type ErrorHandler interface {
HandlerError(http.ResponseWriter, *http.Request, error)
}
func ErrorHandlerWithLogger ¶ added in v0.0.3
func ErrorHandlerWithLogger( after ErrorHandler, logger *slog.Logger, ) ErrorHandler
func NewErrorPageHandler ¶ added in v0.0.3
func NewErrorPageHandler( bundle *i18n.Bundle, head Head, renderableErrors []RenderableError, prioritizeLanguages ...string, ) ErrorHandler
NewErrorPageHandler creates a pre-rendered localized error reponses based on [Error.HyperTextStatusCode]. Falls back on the renderer associated with http.StatusInternalServerError. If that renderer is missing, creates one.
type ErrorHandlerFunc ¶ added in v0.0.3
type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, error)
func (ErrorHandlerFunc) HandlerError ¶ added in v0.0.3
func (f ErrorHandlerFunc) HandlerError(w http.ResponseWriter, r *http.Request, err error)
type ErrorRenderer ¶ added in v0.0.3
type ErrorRenderer struct { StatusCode int Renderer Renderable }
type Head ¶ added in v0.0.3
type Head struct { Title *i18n.LocalizeConfig Description *i18n.LocalizeConfig Image string FavIconPNG string Scripts []string StyleSheets []string }
type Renderable ¶ added in v0.0.3
type RenderableError ¶ added in v0.0.3
type RenderableError interface { Error Renderable Describe() (title, description *i18n.LocalizeConfig) }
Click to show internal directories.
Click to hide internal directories.