Documentation
¶
Index ¶
- Variables
- func BindDeepObjectQuery(ctx *gin.Context, dst any) error
- func BindJSONBody(ctx *gin.Context, dst any) error
- func GetPathMetaFromCtx(ctx *gin.Context) []any
- func HandleError(ctx *gin.Context, conf config.Config, err error, nonFWFormat string, ...)
- func NewDefaultErrorGinWriter(logger log.Logger) io.Writer
- func NewDefaultGinWriter(logger log.Logger) io.Writer
- func ParseQueryDeepObject(ctx *gin.Context) map[string]any
- func ProvideAsResource(provider any, anns ...fx.Annotation) fx.Option
- type GinRouterGroup
- type HTTPHandler
- type HTTPMethod
- type MetaMapping
- type Resource
- type Resources
- type URLPath
Constants ¶
This section is empty.
Variables ¶
var Module = fx.Options( fx.Invoke(NewResources), fx.Provide(NewHTTPHandler), )
Functions ¶
func BindDeepObjectQuery ¶
BindDeepObjectQuery binds the url.URL.Query() to the dst parameter expecting the query to follow openapi3 deepObject serialization. The binding will be done using mapstructure.WeakDecode. It will abort with error http.StatusBadRequest if the binding fails.
It will run validations if the dst struct implements validation.Validatable interface. It will abort with error http.StatusUnprocessableEntity if validation fails.
func BindJSONBody ¶
BindJSONBody binds the body to the dst parameter expecting the body to be JSON. It will abort with error http.StatusBadRequest if the JSON deserialization fails.
It will run validations if the dst struct implements validation.Validatable interface. It will abort with http.StatusUnprocessableEntity if validation fails
func GetPathMetaFromCtx ¶
func HandleError ¶
func ParseQueryDeepObject ¶
ParseQueryDeepObject will parse the url.URL.Query() expecting it to follow [openapi3 deepObject] serialization. [openapi3 deepObject]: https://swagger.io/docs/specification/serialization/#:~:text=b%7Cc.-,deepObject,-%E2%80%93%20simple%20non%2Dnested
func ProvideAsResource ¶
func ProvideAsResource(provider any, anns ...fx.Annotation) fx.Option
Types ¶
type GinRouterGroup ¶
type GinRouterGroup interface {
MetaMapping() MetaMapping
Use(handlers ...gin.HandlerFunc) GinRouterGroup
GET(relativePath string, handlers ...gin.HandlerFunc) GinRouterGroup
GETWithMeta(relativePath string, metadata any, handlers ...gin.HandlerFunc) GinRouterGroup
POST(relativePath string, handlers ...gin.HandlerFunc) GinRouterGroup
POSTWithMeta(relativePath string, metadata any, handlers ...gin.HandlerFunc) GinRouterGroup
PUT(relativePath string, handlers ...gin.HandlerFunc) GinRouterGroup
PUTWithMeta(relativePath string, metadata any, handlers ...gin.HandlerFunc) GinRouterGroup
PATCH(relativePath string, handlers ...gin.HandlerFunc) GinRouterGroup
PATCHWithMeta(relativePath string, metadata any, handlers ...gin.HandlerFunc) GinRouterGroup
DELETE(relativePath string, handlers ...gin.HandlerFunc) GinRouterGroup
DELETEWithMeta(relativePath string, metadata any, handlers ...gin.HandlerFunc) GinRouterGroup
HEAD(relativePath string, handlers ...gin.HandlerFunc) GinRouterGroup
HEADWithMeta(relativePath string, metadata any, handlers ...gin.HandlerFunc) GinRouterGroup
OPTIONS(relativePath string, handlers ...gin.HandlerFunc) GinRouterGroup
OPTIONSWithMeta(relativePath string, metadata any, handlers ...gin.HandlerFunc) GinRouterGroup
Handle(httpMethod string, relativePath string, handlers ...gin.HandlerFunc) GinRouterGroup
HandleWithMeta(httpMethod string, relativePath string, metadata any, handlers ...gin.HandlerFunc) GinRouterGroup
Group(relativePath string, handlers ...gin.HandlerFunc) GinRouterGroup
GroupWithMeta(relativePath string, metadata any, handlers ...gin.HandlerFunc) GinRouterGroup
}
GinRouterGroup is a drop-in replacement for *gin.RouterGroup, but it is not binary compatible, that also allows to register metadata in the path with new function flavors WithMeta.
Metadata registration follows the handlers chain rules for registration. Example:
router.
GroupWithMeta("resources", "first_level").
GET(":id", getHandler). // ["first_level"]
GroupWithMeta("sub-resources", "second_level").
GETWithMeta("", "list", getHandler). // ["first_level", "second_level", "list"]
GET(":id", getHandler). // ["first_level", "second_level"]
GroupWithMeta("lowest-resource", "third_level").
POSTWithMeta(":id", "create", postHandler) // ["first_level", "second_level","third_level", "create"]
router.
Group("resources").
POST(":id", postHandler). // []
GroupWithMeta("", "first_level").
PATCHWithMeta(":id", "update", patchHandler) // ["first_level", "update"]
The output is:
{
"GET": {
"/resources/subresource/:id": [
"first_level",
"second_level"
]
}
}
func NewGinRouterGroup ¶
func NewGinRouterGroup(routerGroup *gin.RouterGroup) GinRouterGroup
type HTTPHandler ¶
type HTTPHandler struct {
Engine *gin.Engine
Root GinRouterGroup
BasePath string
}
func NewHTTPHandler ¶
func NewHTTPHandler( conf config.Config, lf *log.LoggerFactory, lc fx.Lifecycle, ) HTTPHandler
NewHTTPHandler creates a new request handler
type HTTPMethod ¶
type HTTPMethod string
type MetaMapping ¶
type MetaMapping map[URLPath]map[HTTPMethod][]any
type Resource ¶
type Resource interface {
Setup(httpHandler HTTPHandler)
}
type Resources ¶
type Resources []Resource
func NewResources ¶
func NewResources(in struct {
fx.In
Resources []Resource `group:"resources"`
HTTPHandler HTTPHandler `optional:"true"`
}) Resources
func (Resources) Setup ¶
func (rs Resources) Setup(httpHandler HTTPHandler)