Documentation
¶
Overview ¶
Defines a standard way to define routes
Index ¶
- func CompileValidationErrors(payload any) map[string]string
- func SetupState(s UAPIState)
- type APIRouter
- type AuthData
- type AuthType
- type HttpResponse
- func DefaultResponse(statusCode int) HttpResponse
- func MarshalReq(r *http.Request, dst any) (resp HttpResponse, ok bool)
- func MarshalReqWithHeaders(r *http.Request, dst any, headers map[string]string) (resp HttpResponse, ok bool)
- func ValidatorErrorResponse(compiled map[string]string, v validator.ValidationErrors) HttpResponse
- type Method
- type Route
- type RouteData
- type Router
- type UAPIConstants
- type UAPIDefaultResponder
- type UAPIInitData
- type UAPIState
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompileValidationErrors ¶
func SetupState ¶
func SetupState(s UAPIState)
Types ¶
type HttpResponse ¶
type HttpResponse struct {
// Data is the data to be sent to the client
Data string
// Optional, can be used in place of Data
Bytes []byte
// Json body to be sent to the client
Json any
// Headers to set
Headers map[string]string
// Status is the HTTP status code to send
Status int
// Redirect to a URL
Redirect string
}
func DefaultResponse ¶
func DefaultResponse(statusCode int) HttpResponse
Creates a default HTTP response based on the status code 200 is treated as 204 No Content
func MarshalReq ¶
func MarshalReq(r *http.Request, dst any) (resp HttpResponse, ok bool)
func MarshalReqWithHeaders ¶
func ValidatorErrorResponse ¶
func ValidatorErrorResponse(compiled map[string]string, v validator.ValidationErrors) HttpResponse
type Route ¶
type Route struct {
Method Method
Pattern string
Aliases map[string]string // Aliases for the route, this is useful for e.g. URL rewrites, format is pattern: reason
OpId string
Handler func(d RouteData, r *http.Request) HttpResponse
Setup func()
Docs func() *docs.Doc
Auth []AuthType
ExtData map[string]any
AuthOptional bool
SanityCheck func() error
// Disables sanity check that ensures all variables are followed by a /
//
// e.g. /{foo}s/
DisablePathSlashCheck bool
}
Represents a route on the API
type Router ¶
type Router interface {
Get(pattern string, h http.HandlerFunc)
Post(pattern string, h http.HandlerFunc)
Patch(pattern string, h http.HandlerFunc)
Put(pattern string, h http.HandlerFunc)
Delete(pattern string, h http.HandlerFunc)
Head(pattern string, h http.HandlerFunc)
}
type UAPIConstants ¶
type UAPIDefaultResponder ¶
type UAPIInitData ¶
type UAPIInitData struct {
Tag string
}
type UAPIState ¶
type UAPIState struct {
Logger *zap.Logger
Authorize func(r Route, req *http.Request) (AuthData, HttpResponse, bool)
AuthTypeMap map[string]string // E.g. bot => Bot, user => User etc.
RouteDataMiddleware func(rd *RouteData, req *http.Request) (*RouteData, error)
BaseSanityCheck func(r Route) error
PatchDocs func(d *docs.Doc) *docs.Doc
Context context.Context
Constants *UAPIConstants
DefaultResponder UAPIDefaultResponder
InitData UAPIInitData
}
* * UAPIState is the state of the UAPI plugin. * It is used to store the logger, authorize function, and other data.
var (
State *UAPIState
)
func (*UAPIState) SetCurrentTag ¶ added in v0.2.0
Click to show internal directories.
Click to hide internal directories.