api

package
v0.0.0-...-2d2b994 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2022 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnhandled = Error{
		Status:  http.StatusInternalServerError,
		Code:    "internal_server_error",
		Message: "Oops, sorry! There's an unhandled error in here somewhere.",
	}
	ErrNotFound = Error{
		Status:  http.StatusNotFound,
		Code:    "not_found",
		Message: "The requested route does not exist.",
	}
	ErrMethodNotAllowed = Error{
		Status: http.StatusMethodNotAllowed,
		Code:   "method_not_allowed",

		Message: "The requested HTTP method cannot be handled by this route.",
	}
	ErrMissingAuthz = Error{
		Status:  http.StatusUnauthorized,
		Code:    "missing_authorization",
		Message: "The Authorization header was missing or the wrong format.",
	}
	ErrInvalidAuthz = Error{
		Status:  http.StatusUnauthorized,
		Code:    "invalid_authorization",
		Message: "The provided credentials were not valid.",
	}
)
View Source
var ErrFromVar = errors.New("fromVar: unhandled type")

Functions

func FromBody

func FromBody(body io.Reader, v interface{}) error

FromBody reads the request body and unmarshals the JSON into v. For types that implement Validator, this returns v.Validate() after unmarshaling.

func FromVars

func FromVars(vars map[string]string, v interface{}) error

FromVars unpacks URL parameters into v. For types that implement Validator, this returns v.Validate() after unpacking.

This is intended to be called with the result of mux.Vars.

Unpacking is done with json.Marshal, with some overrides for specific types:

- If a type implements Param, its FromParam method will be used. - For string and []byte, the value will be returned as-is. - Complex numbers will use strconv.ParseComplex with the correct bit size.

func LogRequests

func LogRequests(log *zap.Logger) mux.MiddlewareFunc

func Pairs

func Pairs(params interface{}) []string

Pairs flattens a route params struct into a slice of key-value pairs suitable for use in calls to mux.Route.URL().

Types

type Context

type Context struct {
	context.Context

	Log   *zap.Logger
	Tx    *sql.Tx
	Clock clock.Clock
}

func (*Context) Close

func (c *Context) Close() error

type Error

type Error struct {
	Status  httpStatus `json:"status"`
	Code    ErrorCode  `json:"code"`
	Message string     `json:"message"`
}

func NoResourceError

func NoResourceError(resource, id string) Error

func ValidationError

func ValidationError(field string, value interface{}, message string) Error

func (Error) Error

func (e Error) Error() string

type ErrorCode

type ErrorCode string

type Param

type Param interface {
	FromParam(string) error
}

type Server

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

func NewServer

func NewServer(log *zap.Logger, db *sql.DB) *Server

func (*Server) Authenticate

func (s *Server) Authenticate(ctx Context, r *http.Request) (*User, error)

func (*Server) Context

func (s *Server) Context(r *http.Request) (Context, error)

func (*Server) Respond

func (s *Server) Respond(w http.ResponseWriter, v interface{}, err error)

type User

type User struct {
	ID uuid.UUID `json:"id"`
}

type Validator

type Validator interface {
	Validate() error
}

Jump to

Keyboard shortcuts

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