mux

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mux

type Mux struct {
	// contains filtered or unexported fields
}

Mux is a multiplexer for JSONAPI requests. Routes are determined by the request resource type. Mux implements http.Handler and can be used directly with http.ListenAndServe() or any function that accepts an http.Handler.

The muxer is configured with a set of default options. To modify these options, use the MuxOptions:

router := server.New(
	server.WithRequestContextResolver(CustomContextResolver{}),
	server.WithMiddleware(LoggingMiddleware{}),
	server.WithMiddleware(middleware.ResolvesSortParams()),
	server.WithMiddleware(middleware.ResolvesFilterParams()),
	server.WithMiddleware(middleware.ResolvesPagination()),
	middleware.ResolvesIncludes(),
	middleware.ResolvesRelatedResources(),
	server.WithRoute("posts", PostHandler{}),
	server.WithRoute("users", server.ResourceRoute{
		Create: UserCreateHandler{},
		Update: UserUpdateHandler{},
		Delete: UserDeleteHandler{},
		Get:    UserGetHandler{},
		List:   UserListHandler{},
	}),
)

func New

func New(options ...Options) Mux

New creates a new Mux with the given options.

By default, Mux determines context from the request url:

":base/:type"                        // for resource type
":base/:type/:id"                    // for resource type and resource id
":base/:type/:id/relationships/:ref" // for resource type, id, and relationship name
":base/:type/:id/:ref"               // for type, id, relationship, and if the request is for related resources.

This and other behaviors can be modified via configuration options. Request instance.

func (Mux) RouteJSONAPI

func (m Mux) RouteJSONAPI(ctx *jsonapi.RequestContext, r *http.Request) jsonapi.Response

RouteJSONAPI routes a JSON:API request to the appropriate handler.

func (Mux) ServeJSONAPI

func (m Mux) ServeJSONAPI(r *http.Request) jsonapi.Response

ServeJSONAPI handles a JSON:API request.

type Options

type Options = func(*Mux)

Options are options for configuring a Muxer.

func WithMiddleware

func WithMiddleware(middleware jsonapi.Middleware) Options

WithMiddleware adds middleware to the muxer.

func WithRoute

func WithRoute(resourceType string, handler jsonapi.RequestHandler) Options

WithRoute associates the handler with the supplied resource type.

type Route

type Route struct {
	Create    jsonapi.RequestHandler // Create handles requests to create new resources.
	List      jsonapi.RequestHandler // List handles requests to list resource collections.
	Get       jsonapi.RequestHandler // Get handles requests to fetch a specific resource.
	GetRef    jsonapi.RequestHandler // GetRef handles requests to fetch a specific resource relationship.
	Update    jsonapi.RequestHandler // Update handles requests to update a specific resource.
	UpdateRef jsonapi.RequestHandler // UpdateRef handles requests to update a specific resource relationship.
	AddRef    jsonapi.RequestHandler // AddRef handles requests to add a specific resource relationship.
	RemoveRef jsonapi.RequestHandler // RemoveRef handles requests to remove a specific resource relationship.
	Delete    jsonapi.RequestHandler // Delete handles requests to delete a specific resource.
}

Route is a collection of handlers for a single resource type. Each handler corresponds to a specific JSON:API resource operation, such as Create, List, Get, Update, etc. The request type is determined by the HTTP method and the JSON:API context.

The handlers are optional. If the corresponding request handler is nil, the request is rejected with a 404 Not Found response.

func (Route) RouteJSONAPI

func (r Route) RouteJSONAPI(ctx *jsonapi.RequestContext, req *http.Request) jsonapi.Response

RouteJSONAPI routes incoming JSON:API requests to the appropriate resource operation.

func (Route) ServeJSONAPI

func (r Route) ServeJSONAPI(req *http.Request) jsonapi.Response

ServeJSONAPI handles incoming JSON:API requests.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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