apirouter

package module
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2023 License: MIT Imports: 22 Imported by: 1 module, 11 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GoDoc

API router

Returning errors

When returning an error, it is good practice to use the Error object.

User

Setting up a request hook that will check the authorization header and call Context.SetUser allows passing an object to other methods.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTargetMissing   = errors.New("missing target")
	ErrAccessDenied    = &Error{Message: "Access denied", Token: "error_access_denied", Code: http.StatusForbidden}
	ErrInternal        = &Error{Message: "An internal error occured", Token: "error_internal", Code: http.StatusInternalServerError}
	ErrInsecureRequest = &Error{Message: "Request must use POST and have the appropriate tokens", Token: "error_insecure_request", Code: http.StatusBadRequest}
	ErrTeapot          = &Error{Message: "A teapot has appeared", Token: "error_teapot", Code: http.StatusTeapot}
)
View Source
var (
	RequestHooks  []RequestHook
	ResponseHooks []ResponseHook
)

Functions

func CSRFHeaderHook added in v0.1.8

func CSRFHeaderHook(c *Context) error

CSRFHeaderHook is a sample hook for checking a specific middleware header for csrf validation

func GetHeader added in v0.0.15

func GetHeader(ctx context.Context, hdr string) string

GetHeader returns the requested header or an empty string if not found

func GetObject added in v0.1.6

func GetObject[T any](ctx context.Context, typ string) *T

func GetParam added in v0.1.8

func GetParam[T any](ctx context.Context, v string) (T, bool)

func GetRequestBody added in v0.0.15

func GetRequestBody(ctx context.Context) (io.ReadCloser, error)

GetRequestBody returns the current request's body if any, or an error

func GetUser added in v0.1.8

func GetUser[T any](ctx context.Context) *T

GetUser will return the user object if it matches type T. If there is no user object or it is not of the right type, nil will be returned

func HTTP added in v0.0.2

func HTTP(rw http.ResponseWriter, req *http.Request)

apirouter.HTTP can be used as a handler function, or as a handler via http.HandlerFunc(apirouter.HTTP)

func SecurePost added in v0.1.7

func SecurePost(ctx context.Context) error

SecurePost ensures request was a POST request and has the required headers

Types

type Context

type Context struct {
	context.Context
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context, path, verb string) *Context

func NewHttp

func NewHttp(rw http.ResponseWriter, req *http.Request) (*Context, error)

func (*Context) Call

func (c *Context) Call() (any, error)

func (*Context) CallSpecial

func (c *Context) CallSpecial() (any, error)

func (*Context) GetDomain added in v0.0.10

func (c *Context) GetDomain() string

func (*Context) GetExtraResponse added in v0.0.6

func (c *Context) GetExtraResponse(k string) any

func (*Context) GetObject added in v0.0.3

func (c *Context) GetObject(typ string) any

func (*Context) GetParam

func (c *Context) GetParam(v string) any

GetParam returns one individual value from the current parameters, and can lookup valuyes in submaps/etc by adding a dot between values.

func (*Context) GetParamTo

func (c *Context) GetParamTo(v string, obj any) bool

func (*Context) GetParams

func (c *Context) GetParams() map[string]any

GetParams returns all the parameters associated with this request

func (*Context) GetPath

func (c *Context) GetPath() string

func (*Context) GetQuery added in v0.0.5

func (c *Context) GetQuery(v string) any

func (*Context) GetQueryFull added in v0.0.8

func (c *Context) GetQueryFull() map[string]any

func (*Context) NewRequest

func (c *Context) NewRequest(target string) (*http.Request, error)

NewRequest returns a http request for this context (for example for forwarding, etc)

func (*Context) RequestId added in v0.0.20

func (c *Context) RequestId() string

func (*Context) Response

func (c *Context) Response() (res *Response, err error)

func (*Context) ServeHTTP

func (c *Context) ServeHTTP(rw http.ResponseWriter, req *http.Request)

func (*Context) SetCsrfValidated added in v0.1.8

func (c *Context) SetCsrfValidated(ok bool)

SetCsrfValidated is to be used in request hook to tell apirouter if the request came with a valid and appropriate CSRF token.

func (*Context) SetExtraResponse added in v0.0.6

func (c *Context) SetExtraResponse(k string, v any)

func (*Context) SetHttp

func (c *Context) SetHttp(rw http.ResponseWriter, req *http.Request) error

func (*Context) SetParam

func (c *Context) SetParam(name string, v any)

SetParam allows setting one individual parameter to the request

func (*Context) SetParams

func (c *Context) SetParams(v map[string]any)

SetParams sets the params passed to the API

func (*Context) SetPath

func (c *Context) SetPath(p string)

func (*Context) SetUser added in v0.1.8

func (c *Context) SetUser(user any)

SetUser sets the user object for the associated context, which can be fetched with GetUser[T](ctx). This method will typically be called in a RequestHook.

func (*Context) Value

func (c *Context) Value(v any) any

type Deletable added in v0.0.4

type Deletable interface {
	ApiDelete(ctx *Context) error
}

type Error added in v0.0.17

type Error struct {
	Message string // error message
	Code    int    // HTTP status code for error
	Token   string // optional error token
}

func (*Error) Error added in v0.0.17

func (e *Error) Error() string

func (*Error) HTTPStatus added in v0.0.17

func (e *Error) HTTPStatus() int

type RequestHook added in v0.0.20

type RequestHook func(c *Context) error

type Response

type Response struct {
	Result       string   `json:"result"` // error|success|redirect
	Error        string   `json:"error,omitempty"`
	Token        string   `json:"token,omitempty"`
	Code         int      `json:"code,omitempty"`
	Debug        string   `json:"debug,omitempty"`
	RequestId    string   `json:"request_id,omitempty"`
	Time         float64  `json:"time"`
	Data         any      `json:"data"`
	RedirectURL  *url.URL `json:"redirect_url,omitempty"`
	RedirectCode int      `json:"redirect_code,omitempty"`
	// contains filtered or unexported fields
}

func (*Response) ServeHTTP

func (r *Response) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type ResponseHook added in v0.0.20

type ResponseHook func(r *Response) error

type Updatable added in v0.0.4

type Updatable interface {
	ApiUpdate(ctx *Context) error
}

Jump to

Keyboard shortcuts

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