Documentation ¶
Index ¶
- func IsBidiStreamRequest(c *gin.Context) bool
- func IsUnidiStreamRequest(c *gin.Context) bool
- func NewStatsCollector() prometheus.Collector
- type Attributes
- type ErrorHandle
- type ErrorHandler
- type ErrorResponse
- type HTTPHandle
- type HTTPHandler
- type Handle
- func If(match func(*gin.Context) bool, then Handle) Handle
- func Only(match func(*gin.Context) bool) Handle
- func OnlyLocalIP() Handle
- func Per(hashRequest func(*gin.Context) string, provideHandler func() Handle) Handle
- func PerIP(provideHandler func() Handle) Handle
- func RequestCounting(max int, wait time.Duration) Handle
- func RequestShaping(qps, slack int, latency time.Duration) Handle
- func RequestThrottling(qps, burst int) Handle
- type Handler
- type IHandler
- type IResourceHandler
- type IRouter
- type RequestAttributesType
- type RequestBidiStream
- func (r RequestBidiStream) Cancel()
- func (r RequestBidiStream) Deadline() (deadline time.Time, ok bool)
- func (r RequestBidiStream) Done() <-chan struct{}
- func (r RequestBidiStream) Err() error
- func (r RequestBidiStream) Read(p []byte) (n int, err error)
- func (r RequestBidiStream) RecvJSON(i any) error
- func (r RequestBidiStream) RecvMsg() ([]byte, error)
- func (r RequestBidiStream) SendJSON(i any) error
- func (r RequestBidiStream) SendMsg(data []byte) error
- func (r RequestBidiStream) Value(key any) any
- func (r RequestBidiStream) Write(p []byte) (n int, err error)
- type RequestPagination
- type RequestUnidiStream
- func (r RequestUnidiStream) Cancel()
- func (r RequestUnidiStream) Deadline() (deadline time.Time, ok bool)
- func (r RequestUnidiStream) Done() <-chan struct{}
- func (r RequestUnidiStream) Err() error
- func (r RequestUnidiStream) SendJSON(i any) error
- func (r RequestUnidiStream) SendMsg(data []byte) error
- func (r RequestUnidiStream) Value(key any) any
- func (r RequestUnidiStream) Write(p []byte) (n int, err error)
- type ResourceProfile
- type ResponseAttributesType
- type ResponseCollection
- type ResponseFile
- type ResponsePagination
- type Route
- type RouteAdviceProvider
- type RouteAdviceReceiver
- type RouteAuthorizeFunc
- type RouteAuthorizer
- type RouteProfile
- type Router
- func (rt *Router) Delete(path string, handler IHandler) IRouter
- func (rt *Router) Get(path string, handler IHandler) IRouter
- func (rt *Router) Group(relativePath string) IRouter
- func (rt *Router) GroupIn(relativePath string, doGroupRoute func(IRouter)) IRouter
- func (rt *Router) GroupRelativePath() string
- func (rt *Router) Patch(path string, handler IHandler) IRouter
- func (rt *Router) Post(path string, handler IHandler) IRouter
- func (rt *Router) Put(path string, handler IHandler) IRouter
- func (rt *Router) Routes(handler IHandler) IRouter
- func (rt *Router) ServeHTTP(resp http.ResponseWriter, req *http.Request)
- func (rt *Router) Static(p string, fs http.FileSystem) IRouter
- func (rt *Router) Use(handlers ...IHandler) IRouter
- type RouterOption
- func ExposeOpenAPI() RouterOption
- func SkipLoggingPaths(paths ...string) RouterOption
- func WithDefaultHandler(handler IHandler) RouterOption
- func WithDefaultWriter(w io.Writer) RouterOption
- func WithResourceAuthorizer(authorizer RouteAuthorizer) RouterOption
- func WithRouteAdviceProviders(providers ...RouteAdviceProvider) RouterOption
- type RouterOptions
- type StaticHttpFile
- type StaticHttpFileSystem
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsBidiStreamRequest ¶
IsBidiStreamRequest returns true if the incoming request is a websocket request.
func IsUnidiStreamRequest ¶
IsUnidiStreamRequest returns true if the incoming request is a watching request.
func NewStatsCollector ¶
func NewStatsCollector() prometheus.Collector
Types ¶
type Attributes ¶
type Attributes uint64
func (*Attributes) HasAll ¶
func (t *Attributes) HasAll(u Attributes) bool
func (*Attributes) HasAny ¶
func (t *Attributes) HasAny(u Attributes) bool
func (*Attributes) With ¶
func (t *Attributes) With(u Attributes)
type ErrorHandle ¶
type ErrorHandler ¶
type ErrorResponse ¶
type HTTPHandle ¶
type HTTPHandle = http.HandlerFunc
type HTTPHandler ¶
type Handle ¶
func If ¶
If is a gin middleware, which is used for judging the incoming request, execute given handle if matched.
func Only ¶
Only is a gin middleware, which is used for judging the incoming request, aborts with 403 if not match.
func OnlyLocalIP ¶
func OnlyLocalIP() Handle
OnlyLocalIP judges the incoming request whether is from localhost, aborts with 403 if not match.
func Per ¶
Per is a gin middleware, which is used for providing new handler for different incoming request.
func RequestCounting ¶
RequestCounting limits the request count in the given maximum, returns 429 if the new request has waited with the given duration.
func RequestShaping ¶
RequestShaping arranges all requests to be received on the given qps, returns 429 if the new request can be allowed within the given latency, if the given latency is not positive, RequestShaping will never return 429.
func RequestThrottling ¶
RequestThrottling controls the request count per second and allows bursting, returns 429 if the new request is not allowed.
type IResourceHandler ¶
func Alias ¶
func Alias(handler IResourceHandler, withKind string) IResourceHandler
Alias wraps the given resource handler with a new alias kind.
type IRouter ¶
type IRouter interface { http.Handler // Use attaches a global middleware to the router. Use(...IHandler) IRouter // Group creates a new router group with the given string, // and returns the new router group. Group(string) IRouter // GroupIn creates a new router group with the given string, // but returns the original router group, // the new router group is passed to the given function. GroupIn(string, func(groupRouter IRouter)) IRouter // GroupRelativePath returns the relative path of router group. GroupRelativePath() string // Static registers GET/HEAD routers to serve the handler. Static(string, http.FileSystem) IRouter // Get registers GET router to serve the handler. Get(string, IHandler) IRouter // Post registers POST router to serve the handler. Post(string, IHandler) IRouter // Delete registers DELETE router to serve the handler. Delete(string, IHandler) IRouter // Patch registers PATCH router to serve the handler. Patch(string, IHandler) IRouter // Put registers PUT router to serve the handler. Put(string, IHandler) IRouter // Routes registers the reflected routes of a IHandler. // // Routes reflects the function descriptors as the below rules, // if the handler implements IResourceHandler as well. // // Input : struct type. // Output: any types. // // * Basic APIs // // func Create(<Input>) (<Output>, error) // -> POST /<plural> // func Get(<Input>) (<Output>, error) // -> GET /<plural>/:id(?watch=true) // func Update(<Input>) error // -> PUT /<plural>/:id // func Delete(<Input>) error // -> DELETE /<plural>/:id // func CollectionCreate(<Input>) (<Output>, error) // -> POST /<plural>/_/batch // func CollectionGet(<Input>) (<Output>, (int,) error) // -> GET /<plural>(?watch=true) // func CollectionUpdate(<Input>) error // -> PUT /<plural> // func CollectionDelete(<Input>) error // -> DELETE /<plural> // // * Extensional APIs // // func Route<Something>(<Input(route:method=subpath)>) ((<Output>), (int,) error) // -> method /<plural>/:id/<subpath>(?watch=true) // func CollectionRoute<Something>(<Input(route:method=subpath)>) ((<Output>), (int,) error) // -> method /<plural>/_/<subpath>(?watch=true) // // Otherwise, Routes tries to reflect the function descriptors as the below rules. // // Input : struct type. // Output: any types. // // func <Anything>(<Input(route:method=path)>) ((<Output>), (int,) error) // -> method /<path>(?watch=true) // Routes(IHandler) IRouter }
func NewRouter ¶
func NewRouter(options ...RouterOption) IRouter
type RequestAttributesType ¶
type RequestAttributesType = Attributes
const ( RequestWithValidate RequestAttributesType = 1 << iota RequestWithGinContext RequestWithUnidiStream RequestWithBidiStream RequestWithBindingForm RequestWithBindingJSON RequestWithBindingHeader RequestWithBindingQuery RequestWithBindingPath )
type RequestBidiStream ¶
type RequestBidiStream struct {
// contains filtered or unexported fields
}
RequestBidiStream holds the request for dual-directions stream.
func (RequestBidiStream) Cancel ¶
func (r RequestBidiStream) Cancel()
Cancel cancels the underlay context.Context.
func (RequestBidiStream) Deadline ¶
func (r RequestBidiStream) Deadline() (deadline time.Time, ok bool)
Deadline implements context.Context.
func (RequestBidiStream) Done ¶
func (r RequestBidiStream) Done() <-chan struct{}
Done implements context.Context.
func (RequestBidiStream) Err ¶
func (r RequestBidiStream) Err() error
Err implements context.Context.
func (RequestBidiStream) Read ¶
func (r RequestBidiStream) Read(p []byte) (n int, err error)
Read implements io.Reader.
func (RequestBidiStream) RecvJSON ¶
func (r RequestBidiStream) RecvJSON(i any) error
RecvJSON receives JSON message from client and unmarshals into the given object.
func (RequestBidiStream) RecvMsg ¶
func (r RequestBidiStream) RecvMsg() ([]byte, error)
RecvMsg receives message from client.
func (RequestBidiStream) SendJSON ¶
func (r RequestBidiStream) SendJSON(i any) error
SendJSON marshals the given object as JSON and sends to client.
func (RequestBidiStream) SendMsg ¶
func (r RequestBidiStream) SendMsg(data []byte) error
SendMsg sends the given data to client.
func (RequestBidiStream) Value ¶
func (r RequestBidiStream) Value(key any) any
Value implements context.Context.
type RequestPagination ¶
type RequestPagination struct { // Page specifies the page number for querying, // i.e. /v1/repositories?page=1&perPage=10. Page int `query:"page,default=1"` // PerPage specifies the page size for querying, // i.e. /v1/repositories?page=1&perPage=10. PerPage int `query:"perPage,default=100"` }
RequestPagination holds the requesting pagination data.
func (RequestPagination) Limit ¶
func (r RequestPagination) Limit() int
Limit returns the limit of paging.
func (RequestPagination) Offset ¶
func (r RequestPagination) Offset() int
Offset returns the offset of paging.
func (RequestPagination) Paging ¶
func (r RequestPagination) Paging() (limit, offset int, request bool)
Paging returns the limit and offset of paging, returns false if there is no pagination requesting.
type RequestUnidiStream ¶
type RequestUnidiStream struct {
// contains filtered or unexported fields
}
RequestUnidiStream holds the request for single-direction stream.
func (RequestUnidiStream) Cancel ¶
func (r RequestUnidiStream) Cancel()
Cancel cancels the underlay context.Context.
func (RequestUnidiStream) Deadline ¶
func (r RequestUnidiStream) Deadline() (deadline time.Time, ok bool)
Deadline implements context.Context.
func (RequestUnidiStream) Done ¶
func (r RequestUnidiStream) Done() <-chan struct{}
Done implements context.Context.
func (RequestUnidiStream) Err ¶
func (r RequestUnidiStream) Err() error
Err implements context.Context.
func (RequestUnidiStream) SendJSON ¶
func (r RequestUnidiStream) SendJSON(i any) error
SendJSON marshals the given object as JSON and sends to client.
func (RequestUnidiStream) SendMsg ¶
func (r RequestUnidiStream) SendMsg(data []byte) error
SendMsg sends the given data to client.
func (RequestUnidiStream) Value ¶
func (r RequestUnidiStream) Value(key any) any
Value implements context.Context.
type ResourceProfile ¶
type ResourceProfile struct { // Kinds holds the hierarchical kinds of the given route. Kinds []string // Resources holds the hierarchical resources of the given route. Resources []string // ResourcePaths holds the hierarchical resource paths of the given route. ResourcePaths []string // ResourcePathRefers holds the hierarchical resource path IDs of the given route. ResourcePathRefers []string }
ResourceProfile holds the profile of a resource.
func (*ResourceProfile) DeepCopy ¶
func (p *ResourceProfile) DeepCopy() (o ResourceProfile)
DeepCopy returns a deep copy of the resource profile.
func (*ResourceProfile) PluralPath ¶
func (p *ResourceProfile) PluralPath() string
PluralPath returns the plural path of the resource.
func (*ResourceProfile) Prepend ¶
func (p *ResourceProfile) Prepend(rp ResourceProfile)
Prepend prepends the given resource profile.
func (*ResourceProfile) SingularPath ¶
func (p *ResourceProfile) SingularPath() string
SingularPath returns the singular path of the resource.
type ResponseAttributesType ¶
type ResponseAttributesType = Attributes
const (
ResponseWithPage ResponseAttributesType = 1 << iota
)
type ResponseCollection ¶
type ResponseCollection struct { Type string `json:"type,omitempty"` Items any `json:"items"` Pagination *ResponsePagination `json:"pagination,omitempty"` }
ResponseCollection holds the response data of collection with a pagination.
func FullPageResponse ¶
func FullPageResponse(data any, dataTotalSize int) *ResponseCollection
FullPageResponse returns the given data in a pagination, which treats the given data as a full page.
func NoPageResponse ¶
func NoPageResponse(data any) *ResponseCollection
NoPageResponse returns the given data without pagination.
func PageResponse ¶
func PageResponse(page, perPage int, data any, dataTotalSize int) *ResponseCollection
PageResponse returns the given data in a pagination, which calculates the pagination info with the given request page and perPage.
func TypedResponse ¶
func TypedResponse(typ string, data any) *ResponseCollection
TypedResponse returns the given data in typed.
type ResponseFile ¶
type ResponseFile struct { ContentType string ContentLength int64 Headers map[string]string Reader io.ReadCloser }
ResponseFile is similar to render.Reader, but be able to close the file reader out of the handler processing.
func (ResponseFile) Close ¶
func (r ResponseFile) Close() error
func (ResponseFile) Render ¶
func (r ResponseFile) Render(w http.ResponseWriter) (err error)
func (ResponseFile) WriteContentType ¶
func (r ResponseFile) WriteContentType(w http.ResponseWriter)
type ResponsePagination ¶
type ResponsePagination struct { Page int `json:"page"` PerPage int `json:"perPage"` Total int `json:"total"` TotalPage int `json:"totalPage"` Partial bool `json:"partial"` NextPage int `json:"nextPage,omitempty"` }
ResponsePagination holds the pagination data.
type Route ¶
type Route struct { // RouteProfile holds the profile of a route. RouteProfile // GoCaller holds the reflect.Value of the method to call. GoCaller reflect.Value // GoPackage observes the package of the GoType. GoPackage string // GoType observes the type of the GoFunc. GoType string // GoFunc observes the name of the GoCaller. GoFunc string // RequestType observes the reflect.Type of the method to input. RequestType reflect.Type // RequestAttributes observes the attributes of the request. RequestAttributes RequestAttributesType // ResponseType observes the reflect.Type of the method to return. ResponseType reflect.Type // ResponseAttributes observes the attributes of the response. ResponseAttributes ResponseAttributesType // contains filtered or unexported fields }
Route holds the information of a resource route.
type RouteAdviceProvider ¶
type RouteAdviceProvider interface { // CanSet validates the given RouteAdviceReceiver can be set or not in prepare phase, // returns true if the given RouteAdviceReceiver can be injected. // The given RouteAdviceReceiver is stateless, // please do not perform additional operations on it. CanSet(RouteAdviceReceiver) bool // Set injects the valid RouteAdviceReceiver by this provider before validating, // the provider should set the corresponding advice to the target. Set(RouteAdviceReceiver) }
RouteAdviceProvider is a provider to provide advice to the request of the reflected routes of a IHandler.
type RouteAdviceReceiver ¶
type RouteAdviceReceiver any
RouteAdviceReceiver represents the type that can receive advice.
type RouteAuthorizeFunc ¶
type RouteAuthorizeFunc func(*gin.Context, RouteProfile) int
RouteAuthorizeFunc is the function type of RouteAuthorizer.
func (RouteAuthorizeFunc) Authorize ¶
func (fn RouteAuthorizeFunc) Authorize(c *gin.Context, p RouteProfile) int
Authorize implements the RouteAuthorizer interface.
type RouteAuthorizer ¶
type RouteAuthorizer interface { // Authorize returns the status code of authorization result, // 200 if success, 401 if unauthorized, 403 if forbidden. Authorize(*gin.Context, RouteProfile) int }
RouteAuthorizer holds the operation of authorization.
type RouteProfile ¶
type RouteProfile struct { // ResourceProfile holds the resource profile of a route, // if the route is no belong to a IResourceHandler, // the ResourceProfile will be zero. ResourceProfile // Summary holds the brief of the route. Summary string // Description holds the detail of the route. Description string // Method holds the method of the route. Method string // Path holds the path of the route. Path string // Collection indicates the route that works for a collection of resources. Collection bool // Sub indicates the route is a sub route or not. Sub bool // Custom indicates the route is a custom route or not. Custom bool // CustomName indicates the real name of the custom route if Custom is true. CustomName string }
RouteProfile holds the profile of a route.
func (RouteProfile) DeepCopy ¶
func (p RouteProfile) DeepCopy() (o RouteProfile)
DeepCopy returns a deep copy of the resource route profile.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
func (*Router) GroupRelativePath ¶
type RouterOption ¶
type RouterOption interface {
// contains filtered or unexported methods
}
func ExposeOpenAPI ¶
func ExposeOpenAPI() RouterOption
ExposeOpenAPI is a RouterOption to add route to serve the OpenAPI schema spec, and provide the SwaggerUI as well.
func SkipLoggingPaths ¶
func SkipLoggingPaths(paths ...string) RouterOption
SkipLoggingPaths is a RouterOption to ignore logging for the given paths.
func WithDefaultHandler ¶
func WithDefaultHandler(handler IHandler) RouterOption
WithDefaultHandler is a RouterOption to configure the default handler for gin.
func WithDefaultWriter ¶
func WithDefaultWriter(w io.Writer) RouterOption
WithDefaultWriter is a RouterOption to configure the default writer for gin.
func WithResourceAuthorizer ¶
func WithResourceAuthorizer(authorizer RouteAuthorizer) RouterOption
WithResourceAuthorizer if a RouterOption to configure the authorizer for the routes of IResourceHandler.
func WithRouteAdviceProviders ¶
func WithRouteAdviceProviders(providers ...RouteAdviceProvider) RouterOption
WithRouteAdviceProviders is a RouterOption to configure the advice providers for the reflected routes of a IHandler.
type RouterOptions ¶
type RouterOptions []RouterOption
func (RouterOptions) Apply ¶
func (opts RouterOptions) Apply(fn func(o RouterOption) bool) (rOpts RouterOptions)
Apply applies the options one by one, and returns the residual options.
type StaticHttpFile ¶
type StaticHttpFileSystem ¶
type StaticHttpFileSystem struct { http.FileSystem Listable bool Embedded bool }
Source Files ¶
- handler.go
- metrics.go
- middleware_error.go
- middleware_filter.go
- middleware_flowcontrol.go
- middleware_observation.go
- middleware_recovery.go
- middleware_route.go
- request.go
- request_stream.go
- response.go
- router.go
- router_advice.go
- router_options.go
- router_route.go
- router_static.go
- router_stream.go
- router_validation.go
- scheme_route.go
- scheme_route_extension.go
- scheme_route_openapi.go