restroute

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package restroute matches REST-JSON requests against smithy-style URI templates and writes AWS REST-JSON errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EscapedPath

func EscapedPath(r *http.Request) string

EscapedPath returns the request path with percent-encoding preserved (labels may contain "/").

func MaxResultsQuery

func MaxResultsQuery(q url.Values, name string) (int, error)

MaxResultsQuery parses an optional integer query parameter; absent yields 0.

func Paginate

func Paginate[T any](all []T, token string, maxResults, defaultLimit, maxAllowed int) (page.Page[T], error)

Paginate validates maxResults (0 = default, otherwise 1..maxAllowed) and the token, then slices all.

func WriteError

func WriteError(c *echo.Context, status int, code, message string, extra map[string]any) error

WriteError writes a REST-JSON error. The body carries both "message" and "Message" because legacy and schema-serde SDK clients match the modeled key case-sensitively.

Types

type Error

type Error struct {
	Extra   map[string]any
	Code    string
	Message string
	Status  int
}

Error is an AWS REST-JSON exception: HTTP status, error code, message and optional extra body members.

func Conflict

func Conflict(format string, args ...any) *Error

Conflict builds a 409 ConflictException.

func NewError

func NewError(status int, code, format string, args ...any) *Error

NewError builds an Error with an arbitrary status and code.

func NotFound

func NotFound(format string, args ...any) *Error

NotFound builds a 404 ResourceNotFoundException.

func QuotaExceeded

func QuotaExceeded(format string, args ...any) *Error

QuotaExceeded builds a 402 ServiceQuotaExceededException.

func Validation

func Validation(format string, args ...any) *Error

Validation builds a 400 ValidationException.

func (*Error) Error

func (e *Error) Error() string

func (*Error) With

func (e *Error) With(key string, value any) *Error

With attaches an extra body member (for example a modeled "reason") and returns e.

type OpFunc

type OpFunc func(ctx context.Context, req *Request) (result any, status int, err error)

OpFunc runs one operation. A nil result is sent as an empty object; a zero status means 200.

func Typed

func Typed[I, O any](fn func(ctx context.Context, in *I) (*O, error)) OpFunc

Typed adapts fn to an OpFunc. Fields tagged `path:"Label"` or `query:"name"` are filled from the URL; every other exported field comes from the JSON body, so URL-bound fields must also carry `json:"-"`.

type Request

type Request struct {
	Params map[string]string
	Query  url.Values
	Body   []byte
}

Request is the decoded input of one operation.

func (*Request) Decode

func (r *Request) Decode(v any) error

Decode unmarshals the JSON body into v; an empty body leaves v untouched.

type Route

type Route struct {
	Method string
	Path   string
	Op     string
}

Route binds a method and URI template ("/cells/{CellName}") to an operation name.

type Router

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

Router resolves requests to operations; the zero value matches nothing.

func New

func New(routes []Route) *Router

New compiles routes. Every {Label} matches exactly one (URL-decoded) path segment.

func (*Router) Handles

func (r *Router) Handles(escapedPath string) bool

Handles reports whether any route (of any method) has the shape of escapedPath.

func (*Router) Match

func (r *Router) Match(method, escapedPath string) (string, map[string]string)

Match returns the operation and the captured labels for method and the request's escaped path.

type Server

type Server struct {
	Router *Router
	Ops    map[string]OpFunc
	// Context derives the per-request context (region, caller identity, ...); nil uses the request context.
	Context func(r *http.Request) context.Context
}

Server dispatches matched requests to OpFuncs.

func (*Server) Handler

func (s *Server) Handler() echo.HandlerFunc

Handler serves a request: route, read body, run the op, write JSON or the mapped error.

func (*Server) Operation

func (s *Server) Operation(r *http.Request) (string, map[string]string)

Operation returns the op name and path labels for r, or "" when no route matches.

func (*Server) WriteErr

func (s *Server) WriteErr(c *echo.Context, err error) error

WriteErr maps err to its REST-JSON response; unknown errors become a 500 InternalServerException.

Jump to

Keyboard shortcuts

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