rest

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Functions

func BindDeepObjectQuery

func BindDeepObjectQuery(ctx *gin.Context, dst any) error

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

func BindJSONBody(ctx *gin.Context, dst any) error

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 GetPathMetaFromCtx(ctx *gin.Context) []any

func HandleError

func HandleError(ctx *gin.Context, conf config.Config, err error, nonFWFormat string, args ...any)

func NewDefaultErrorGinWriter

func NewDefaultErrorGinWriter(logger log.Logger) io.Writer

func NewDefaultGinWriter

func NewDefaultGinWriter(logger log.Logger) io.Writer

func ParseQueryDeepObject

func ParseQueryDeepObject(ctx *gin.Context) map[string]any

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)

type URLPath

type URLPath string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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