hypermedia

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package hypermedia provides templates and [http.Handler]s for rendering chat sessions.

Index

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 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 AddFavIcon added in v0.0.3

func AddFavIcon(mux *http.ServeMux, image []byte) string

func AddFavIconIfAbsent added in v0.0.3

func AddFavIconIfAbsent(mux *http.ServeMux, image []byte) (found string)

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 NewAsset

func NewAsset(contentType string, body []byte) http.HandlerFunc

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 NewStaticPage(ctx context.Context, r Renderable, b *i18n.Bundle, prioritizeLanguages ...string) http.Handler

func SortLanguageTags added in v0.0.3

func SortLanguageTags(target []language.Tag, acceptLanguageHeader string) error

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 Error

type Error interface {
	error
	HyperTextStatusCode() int
}

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 Handler

type Handler func(main io.Writer, r *http.Request) error
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 Renderable interface {
	Render(context.Context, io.Writer, *i18n.Localizer) error
}

type RenderableError added in v0.0.3

type RenderableError interface {
	Error
	Renderable
	Describe() (title, description *i18n.LocalizeConfig)
}

type RendererFunc added in v0.0.3

type RendererFunc func(context.Context, io.Writer, *i18n.Localizer) error

func (RendererFunc) Render added in v0.0.3

func (f RendererFunc) Render(ctx context.Context, w io.Writer, l *i18n.Localizer) error

Jump to

Keyboard shortcuts

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