web

package
v0.13.15 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2025 License: BSD-3-Clause Imports: 20 Imported by: 6

Documentation

Index

Constants

View Source
const (
	HeaderPaginationCount = "Pagination-Count"
	HeaderPaginationPage  = "Pagination-Page"
	HeaderPaginationLimit = "Pagination-Limit"
)

Variables

View Source
var (
	SrcFromQuery  = ArgSource("FromQuery")
	SrcFromForm   = ArgSource("FromForm")
	SrcFromPath   = ArgSource("FromPath")
	SrcFromHeader = ArgSource("FromHeader")
	SrcFromCookie = ArgSource("FromCookie")
)
View Source
var (
	ErrNotFound    = errors.Error("not found: %v")
	ErrUnexpected  = errors.Error("unexpected: %v")
	ErrInvalidType = errors.Error("invalid type: %T (expected %T)")
	ErrMissing     = errors.Error("missing: %s")
	ErrForbidden   = errors.Error("forbidden: %s")
)
View Source
var (
	ErrWrongContentType = errors.Error("wrong content type: %s (expected %s)")
	ErrValidation       = errors.Error("validation error: %w")
)
View Source
var (
	ErrArgNotFound     = errors.Error("argument with name %q could not be found")
	ErrArgExists       = errors.Error("argument with name %q already exists")
	ErrArgMissing      = errors.Error("missing argument %q of source %q")
	ErrArgNotInRequest = errors.Error("argument %q of type %s not in request")
	ErrArgUnknown      = errors.Error("unknown argument %q given")
	ErrArgInvalidType  = errors.Error("invalid type for argument %s: %T (expected %T)")
)
View Source
var (
	ErrPathArgNotRegistered           = errors.Error("path argument %q from path string %q not registered")
	ErrPathArgRegisteredWithWrongType = errors.Error("path argument %q from path string %q registered with wrong path type (%q)")
	ErrPathArgRegisteredNotInPath     = errors.Error("path argument %q registered, but not inside path string %q")
)
View Source
var (
	ErrClientRequest           = errors.Error("error while making a request as a client: %w")
	ErrFormArgsAndBody         = errors.Error("form arguments (%#v) and a body can't be passed at the same time")
	ErrBodyNotAllowedForMethod = errors.Error("transmitting a form for method %s is not allowed")
)
View Source
var (
	ErrFormArg = errors.Error("form argument %q given")
)

Functions

func BodyAsCSS

func BodyAsCSS(req *http.Request, res *string) error

func BodyAsHTML

func BodyAsHTML(req *http.Request, res *string) error

func BodyAsJSON

func BodyAsJSON(req *http.Request, objref any) error

func BodyAsJavascript

func BodyAsJavascript(req *http.Request, res *string) error

func BodyAsText

func BodyAsText(req *http.Request, res *string) error

func BodyAsXML

func BodyAsXML(req *http.Request, objref any) error

func CheckRoute

func CheckRoute[R routeCheck[R]](r R) bool

the check is done via the routeCheck and this is compilation time

func GetContentFromRequest

func GetContentFromRequest(req *http.Request, content Content) error

func GetPaginationHeader

func GetPaginationHeader(r *http.Request, count, limit, page *int) error

func NewPath

func NewPath(path string, args ...Argument) string

func NewRequest

func NewRequest(method string, path string, body Content, _args ...Argument) (*http.Request, error)

func NewURL

func NewURL(path string, _args ...Argument) string

func PutPathArgsToCtx

func PutPathArgsToCtx(req *http.Request, _args ...Argument) *http.Request

func ReadFromBody

func ReadFromBody(req *http.Request, content Content) error

func ServeContent

func ServeContent(c Content, rw http.ResponseWriter) error

func SetPaginationHeader

func SetPaginationHeader(rw http.ResponseWriter, count, limit, page int)

func SplitArg added in v0.13.4

func SplitArg[C ArgValueTypesCore](argVal string, val *value.Var[[]C]) (err error)

func StringToA

func StringToA[T ArgValueTypes](s string) (T, error)

func StringsToA added in v0.13.4

func StringsToA[T ArgValueTypesCore](s []string) ([]T, error)

Types

type Arg

type Arg interface {
	IsSet() bool
	IsRequired() bool
	Source() ArgSource
	Name() string
	Value() any
	Err() error
}

type ArgScanner

type ArgScanner interface {
	Scan(targets ...any) error
}

type ArgSource

type ArgSource string

type ArgValueTypes

type ArgValueTypes interface {
	ArgValueTypesCore |
		[]string | []int | []uint | []bool | []float64 | []float32 |
		map[string]string | map[string]any
}

type ArgValueTypesCore added in v0.13.4

type ArgValueTypesCore interface {
	string | []byte |
		int | int8 | int16 | int32 | int64 |
		uint | uint8 | uint16 | uint32 | uint64 |
		float64 | float32 |
		bool
}

type Argument

type Argument interface {
	Arg
	ReadValue(req *http.Request)
	SetName(string)
	AddErr(error)
	VarValue() value.Value
}

type Arguments

type Arguments []Argument

func (Arguments) Vars

func (a Arguments) Vars() []value.Value

type CSS

type CSS struct {
	Data string
}

func NewCSS

func NewCSS(data string) *CSS

func (CSS) ContentType

func (s CSS) ContentType() string

func (*CSS) ReadFrom

func (t *CSS) ReadFrom(rd io.Reader) error

func (CSS) WriteTo

func (t CSS) WriteTo(wr io.Writer) error

type Content

type Content interface {
	ContentType() string
	WriteTo(w io.Writer) error
	ReadFrom(rd io.Reader) error
}

type Contexter

type Contexter interface {
	Route() Route
	Request() *http.Request
	ResponseWriter() http.ResponseWriter
}

type CookieArg

type CookieArg[T ArgValueTypes] struct {
	// contains filtered or unexported fields
}

func NewCookieArg

func NewCookieArg[T ArgValueTypes](name string) CookieArg[T]

func NewCookieArgOpt

func NewCookieArgOpt[T ArgValueTypes](name string) CookieArg[T]

func (CookieArg) IsRequired

func (b CookieArg) IsRequired() bool

func (CookieArg) IsSet

func (b CookieArg) IsSet() bool

func (CookieArg[T]) New

func (q CookieArg[T]) New() *CookieArg[T]

func (*CookieArg[T]) ReadValue

func (q *CookieArg[T]) ReadValue(req *http.Request)

func (*CookieArg[T]) Set

func (q *CookieArg[T]) Set(s T) *CookieArg[T]

func (*CookieArg) SetName

func (b *CookieArg) SetName(name string)

func (CookieArg) Source

func (b CookieArg) Source() ArgSource

func (*CookieArg) VarValue

func (b *CookieArg) VarValue() value.Value

type CoreContext

type CoreContext struct {
	IsHandled       bool // the route was already been handled
	IsHeaderWritten bool
	Debug           bool // debug/development mode
	// contains filtered or unexported fields
}

func NewCoreContext

func NewCoreContext(rt Route, rw http.ResponseWriter, req *http.Request) *CoreContext

func (*CoreContext) BodyAsCSS

func (m *CoreContext) BodyAsCSS(res *string) error

func (*CoreContext) BodyAsHTML

func (m *CoreContext) BodyAsHTML(res *string) error

func (*CoreContext) BodyAsJSON

func (m *CoreContext) BodyAsJSON(object_ref any) error

func (*CoreContext) BodyAsJavascript

func (m *CoreContext) BodyAsJavascript(res *string) error

func (*CoreContext) BodyAsText

func (m *CoreContext) BodyAsText(res *string) error

func (*CoreContext) BodyAsXML

func (m *CoreContext) BodyAsXML(object_ref any) error

func (*CoreContext) GetPaginationHeader

func (m *CoreContext) GetPaginationHeader(count, limit, page *int) error

func (*CoreContext) Header

func (c *CoreContext) Header() http.Header

func (*CoreContext) HxRefreshOrRedirect

func (e *CoreContext) HxRefreshOrRedirect(url string) error

func (*CoreContext) HxWantsPartial

func (e *CoreContext) HxWantsPartial() bool

func (*CoreContext) IsHxBoosted

func (e *CoreContext) IsHxBoosted() bool

func (*CoreContext) IsHxHistoryRestoreRequest

func (e *CoreContext) IsHxHistoryRestoreRequest() bool

func (*CoreContext) IsHxRequest

func (e *CoreContext) IsHxRequest() bool

func (*CoreContext) Log

func (c *CoreContext) Log(msg string)

func (*CoreContext) LogErr

func (c *CoreContext) LogErr(err error)

func (*CoreContext) MovedPermanently

func (m *CoreContext) MovedPermanently(url string) error

func (*CoreContext) MovedTemporary

func (m *CoreContext) MovedTemporary(url string) error

func (*CoreContext) Redirect

func (m *CoreContext) Redirect(url string) error

func (*CoreContext) Request

func (e *CoreContext) Request() *http.Request

func (*CoreContext) ResponseWriter

func (e *CoreContext) ResponseWriter() http.ResponseWriter

func (*CoreContext) Route

func (e *CoreContext) Route() Route

func (*CoreContext) SendArgErrs

func (c *CoreContext) SendArgErrs(targets ...Argument) (hadErrors bool)

func (*CoreContext) SendCSS

func (m *CoreContext) SendCSS(data string) error

func (*CoreContext) SendErr

func (c *CoreContext) SendErr(err error) (hadErrors bool)

func (*CoreContext) SendErrInvalidData

func (c *CoreContext) SendErrInvalidData(msg string)

func (*CoreContext) SendErrNotFound

func (c *CoreContext) SendErrNotFound(msg string)

func (*CoreContext) SendErrorMap added in v0.13.1

func (c *CoreContext) SendErrorMap(m map[string]string) bool

func (*CoreContext) SendFormErrs

func (c *CoreContext) SendFormErrs(generalErr error, formvals ...value.Value) (hadErrors bool)

func (*CoreContext) SendHTML

func (m *CoreContext) SendHTML(data any) error

func (*CoreContext) SendHxRefresh

func (e *CoreContext) SendHxRefresh() error

func (*CoreContext) SendJSON

func (m *CoreContext) SendJSON(data any) error

func (*CoreContext) SendJavaScript

func (m *CoreContext) SendJavaScript(data string) error

func (*CoreContext) SendText

func (m *CoreContext) SendText(data string) error

func (*CoreContext) SendXML

func (m *CoreContext) SendXML(data any) error

func (*CoreContext) SetHeader

func (m *CoreContext) SetHeader(key, value string)

func (*CoreContext) SetPaginationHeader

func (m *CoreContext) SetPaginationHeader(count, limit, page int)

func (*CoreContext) Status

func (m *CoreContext) Status(code int) error

func (*CoreContext) StatusHtmxCancelPolling

func (m *CoreContext) StatusHtmxCancelPolling() error

func (*CoreContext) StatusNotFound

func (m *CoreContext) StatusNotFound() error

func (*CoreContext) StatusOK

func (m *CoreContext) StatusOK() error

func (*CoreContext) Write

func (c *CoreContext) Write(bt []byte) (int, error)

func (*CoreContext) WriteHeader

func (c *CoreContext) WriteHeader(statusCode int)

type FormArg

type FormArg[T ArgValueTypes] struct {
	// contains filtered or unexported fields
}

func NewFormArg

func NewFormArg[T ArgValueTypes](name string) FormArg[T]

func NewFormArgOpt

func NewFormArgOpt[T ArgValueTypes](name string) FormArg[T]

func (FormArg) IsRequired

func (b FormArg) IsRequired() bool

func (FormArg) IsSet

func (b FormArg) IsSet() bool

func (FormArg[T]) New

func (q FormArg[T]) New() *FormArg[T]

func (*FormArg[T]) ReadValue

func (q *FormArg[T]) ReadValue(req *http.Request)

func (*FormArg[T]) Set

func (q *FormArg[T]) Set(s T) *FormArg[T]

func (*FormArg) SetName

func (b *FormArg) SetName(name string)

func (FormArg) Source

func (b FormArg) Source() ArgSource

func (*FormArg) VarValue

func (b *FormArg) VarValue() value.Value

type HTML

type HTML struct {
	Data string
}

func NewHTML

func NewHTML(data any) *HTML

func (HTML) ContentType

func (s HTML) ContentType() string

func (*HTML) ReadFrom

func (t *HTML) ReadFrom(rd io.Reader) error

func (HTML) WriteTo

func (t HTML) WriteTo(wr io.Writer) error

type HandlerFunc

type HandlerFunc[T Contexter] func(T)

type HeaderArg

type HeaderArg[T ArgValueTypes] struct {
	// contains filtered or unexported fields
}

func NewHeaderArg

func NewHeaderArg[T ArgValueTypes](name string) HeaderArg[T]

func NewHeaderArgOpt

func NewHeaderArgOpt[T ArgValueTypes](name string) HeaderArg[T]

func (HeaderArg) IsRequired

func (b HeaderArg) IsRequired() bool

func (HeaderArg) IsSet

func (b HeaderArg) IsSet() bool

func (HeaderArg[T]) New

func (q HeaderArg[T]) New() *HeaderArg[T]

func (*HeaderArg[T]) ReadValue

func (q *HeaderArg[T]) ReadValue(req *http.Request)

func (*HeaderArg[T]) Set

func (q *HeaderArg[T]) Set(s T) *HeaderArg[T]

func (*HeaderArg) SetName

func (b *HeaderArg) SetName(name string)

func (HeaderArg) Source

func (b HeaderArg) Source() ArgSource

func (*HeaderArg) VarValue

func (b *HeaderArg) VarValue() value.Value

type JSON

type JSON struct {
	Data any
}

func NewJSON

func NewJSON(data any) *JSON

func (JSON) ContentType

func (s JSON) ContentType() string

func (*JSON) ReadFrom

func (s *JSON) ReadFrom(rd io.Reader) error

func (*JSON) WriteTo

func (s *JSON) WriteTo(wr io.Writer) error

type JavaScript

type JavaScript struct {
	Data string
}

func NewJavaScript

func NewJavaScript(data string) *JavaScript

func (JavaScript) ContentType

func (s JavaScript) ContentType() string

func (*JavaScript) ReadFrom

func (t *JavaScript) ReadFrom(rd io.Reader) error

func (JavaScript) WriteTo

func (t JavaScript) WriteTo(wr io.Writer) error
type Link [2]string
func NewLink(url, name string) Link

func (Link) AHref

func (rl Link) AHref() *html.Element

func (Link) Name

func (rl Link) Name() string

func (Link) URL

func (rl Link) URL() string

type PathArg

type PathArg[T ArgValueTypes] struct {
	// contains filtered or unexported fields
}

func NewPathArg

func NewPathArg[T ArgValueTypes](name string) PathArg[T]

func (PathArg) IsRequired

func (b PathArg) IsRequired() bool

func (PathArg) IsSet

func (b PathArg) IsSet() bool

func (PathArg[T]) New

func (q PathArg[T]) New() *PathArg[T]

func (*PathArg[T]) ReadValue

func (q *PathArg[T]) ReadValue(req *http.Request)

func (*PathArg[T]) Set

func (q *PathArg[T]) Set(s T) *PathArg[T]

func (*PathArg) SetName

func (b *PathArg) SetName(name string)

func (PathArg) Source

func (b PathArg) Source() ArgSource

func (*PathArg) VarValue

func (b *PathArg) VarValue() value.Value

type QueryArg

type QueryArg[T ArgValueTypes] struct {
	// contains filtered or unexported fields
}

func NewQueryArg

func NewQueryArg[T ArgValueTypes](name string) QueryArg[T]

func NewQueryArgOpt

func NewQueryArgOpt[T ArgValueTypes](name string) QueryArg[T]

func (QueryArg) IsRequired

func (b QueryArg) IsRequired() bool

func (QueryArg) IsSet

func (b QueryArg) IsSet() bool

func (QueryArg[T]) New

func (q QueryArg[T]) New() *QueryArg[T]

New() returns a new copy which we need within the handler

func (*QueryArg[T]) ReadValue

func (q *QueryArg[T]) ReadValue(req *http.Request)

func (*QueryArg[T]) Set

func (q *QueryArg[T]) Set(s T) *QueryArg[T]

func (*QueryArg) SetName

func (b *QueryArg) SetName(name string)

func (QueryArg) Source

func (b QueryArg) Source() ArgSource

func (*QueryArg) VarValue

func (b *QueryArg) VarValue() value.Value

type Route

type Route interface {
	Args() []Arg
	Method() string
	Link(name string, argVals ...Argument) Link
	URL(argVals ...Argument) string
	Path() string
	HasHandler() bool
	Call(body Content, args ...Argument) (*http.Response, error)
	MustCall(body Content, args ...Argument) *http.Response
	GetContent(response Content, args ...Argument) error
	GetContentWithPlayload(payload Content, response Content, args ...Argument) error
	Scan(req *http.Request, targets ...Argument)
}

type RouteHandler

type RouteHandler[T Contexter] struct {
	// contains filtered or unexported fields
}

func (*RouteHandler[T]) Args

func (r *RouteHandler[T]) Args() []Arg

func (*RouteHandler[T]) Call

func (r *RouteHandler[T]) Call(payload Content, _args ...Argument) (*http.Response, error)

func (*RouteHandler[T]) Do

func (r *RouteHandler[T]) Do(fn func(ctx T)) *RouteHandler[T]

func (*RouteHandler[T]) GetContent

func (r *RouteHandler[T]) GetContent(response Content, args ...Argument) error

func (*RouteHandler[T]) GetContentWithPlayload

func (r *RouteHandler[T]) GetContentWithPlayload(payload Content, response Content, args ...Argument) error

func (*RouteHandler[T]) HasHandler

func (r *RouteHandler[T]) HasHandler() bool
func (r *RouteHandler[T]) Link(name string, argVals ...Argument) Link

func (*RouteHandler[T]) Method

func (r *RouteHandler[T]) Method() string

func (*RouteHandler[T]) Mount

func (r *RouteHandler[T]) Mount(comp *Router[T])

func (*RouteHandler[T]) MountPath

func (r *RouteHandler[T]) MountPath() string

func (*RouteHandler[T]) MustCall

func (r *RouteHandler[T]) MustCall(payload Content, args ...Argument) *http.Response

MustCall is like Call and panics on errors

func (*RouteHandler[T]) Path

func (r *RouteHandler[T]) Path() string

func (*RouteHandler[T]) Scan

func (r *RouteHandler[T]) Scan(req *http.Request, targets ...Argument)

func (*RouteHandler[T]) ServeHTTP

func (r *RouteHandler[T]) ServeHTTP(wr http.ResponseWriter, req *http.Request)

func (*RouteHandler[T]) URL

func (r *RouteHandler[T]) URL(argVals ...Argument) string

panics when arg is missing

func (*RouteHandler[T]) WithArgs

func (r *RouteHandler[T]) WithArgs(_args ...Arg) *RouteHandler[T]

type Router

type Router[T Contexter] struct {
	Name   string
	Routes []*RouteHandler[T]
	// contains filtered or unexported fields
}

func New

func New[T Contexter](name string, mkContext func(Route, http.ResponseWriter, *http.Request) T) *Router[T]

func (*Router[T]) DELETE

func (c *Router[T]) DELETE(localpath string) *RouteHandler[T]

func (*Router[T]) GET

func (c *Router[T]) GET(localpath string) *RouteHandler[T]

func (*Router[T]) HEAD

func (c *Router[T]) HEAD(localpath string) *RouteHandler[T]

func (*Router[T]) Mount

func (c *Router[T]) Mount(rt *router.Router, pathPrefix string)

func (*Router[T]) OPTIONS

func (c *Router[T]) OPTIONS(localpath string) *RouteHandler[T]

func (*Router[T]) PATCH

func (c *Router[T]) PATCH(localpath string) *RouteHandler[T]

func (*Router[T]) POST

func (c *Router[T]) POST(localpath string) *RouteHandler[T]

func (*Router[T]) PUT

func (c *Router[T]) PUT(localpath string) *RouteHandler[T]

func (*Router[T]) Path

func (c *Router[T]) Path() string

func (*Router[T]) SetVerbose

func (c *Router[T]) SetVerbose() *Router[T]

func (*Router[T]) SubRouter

func (rt *Router[T]) SubRouter(name string, prefix string) *Router[T]

SubRouter returns a new router that is a copy of rt, but with a different name and empty/different routes and a prefix that is applied when mounting a subrouter can be mounted directly or via its parent (subrouter are tracked within their parents)

func (*Router[T]) Use

func (rt *Router[T]) Use(fn func(next func(T)) func(T)) *Router[T]

type Text

type Text struct {
	Data string
}

func NewText

func NewText(data string) *Text

func (Text) ContentType

func (s Text) ContentType() string

func (*Text) ReadFrom

func (t *Text) ReadFrom(rd io.Reader) error

func (Text) WriteTo

func (t Text) WriteTo(wr io.Writer) error

type XML

type XML struct {
	Data any
}

func NewXML

func NewXML(data any) *XML

func (XML) ContentType

func (s XML) ContentType() string

func (*XML) ReadFrom

func (s *XML) ReadFrom(rd io.Reader) error

func (*XML) WriteTo

func (s *XML) WriteTo(wr io.Writer) error

Jump to

Keyboard shortcuts

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