http

package
v0.1.31 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2020 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Debug                 bool
	Host                  string
	Port                  string
	AuthType              string
	DisableCORSAllOrigins bool
	DisableDeleteMethod   bool
	ExcludedStructs       string
}

type RouterGin

type RouterGin struct {
	Engine *gin.Engine
	// contains filtered or unexported fields
}

func NewRouterGin

func NewRouterGin(host, port string) *RouterGin

func (*RouterGin) GetEngine added in v0.1.10

func (r *RouterGin) GetEngine() *gin.Engine

func (*RouterGin) InitRegistry

func (r *RouterGin) InitRegistry() gin.HandlerFunc

Copy router config values to the Gin registry where they can be accessed by the app.

func (*RouterGin) Run

func (r *RouterGin) Run() error

Run the Gin engine.

func (*RouterGin) SetRegistry

func (r *RouterGin) SetRegistry(d map[string]interface{})

Set a map of key-value pairs that will be added to the Gin registry when the router initializes.

type Service

type Service interface {
	DebugEnabled() bool
	NewRouter() *RouterGin
	GetHandlerContext(c *gin.Context) context.Context
	BindMiddleware(r interface{}) gin.HandlerFunc
	GetRequest(c *gin.Context) interface{}
	FilterMiddleware() gin.HandlerFunc
	GetFilter(c *gin.Context) *query.Query
}

Goat writes all request logging to standard out and always enables CORS. Since Goat is intended to be used to build RESTful APIs, it is assumed that the router will run on port 80, that DELETE requests will need to be supported, and that Basic Auth will be used. By default, all origins are allows for CORS.

Requests are bound to an application-level (i.e. non-Goat) struct provided by the caller.

Request struct fields can be annotated with tags to mark them as required and to provide custom labels for use in validation error messages.

For example:

type userRequest struct {
	Name string `json:"name" binding:"required" label:"Username"`
}

Note that the `binding:"required"` annotation is not recursive. For example, if an Order request struct has an Items field that is a slice of Item structs and the Item struct also has fields that are required, the binding service will not throw an error if one of the Items is missing a field. To change this behavior, use 'dive,required' annotation to tell the binding service to check the field's own fields for required annotations as well.

For example:

type orderRequest struct {
	Items []*Item `binding:"required,dive,required"`
}

If a required field is not provided, or a request body is not sent, the binding service will automatically send a bad request response with a list of errors. The service will attempt to use the JSON names of the fields in the error messages rather than the struct field names so that the user sees the field name as they sent it, e.g. "name" instead of "Name". If a custom label annotation exists on the field, that will be used instead.

When building a validation error message, the binding service will use the fully-qualified JSON names of missing fields. For example, if a request struct has a struct field named "Item" that has a required field named "SKU", the error message will show "items.sku" instead of "SKU" to make it easier for the user to determine where the missing field should be. If yous request structs have fields that are also structs that will never have validation errors on their own internal fields (e.g. goat.ID, time.Time, etc), you should exclude these types from the error message building to avoid unnecessary recursion. For example:

BINDING_EXCLUDED_STRUCTS="goat.ID,time.Time"

Since the binding service will return an error response if an error occurs, binding is done in a middleware. The middleware will set the bound request in the Gin context where it can be accessed by subsequent handler functions.

@TODO add support for more auth types.

type ServiceGin

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

func NewServiceGin

func NewServiceGin(c Config) ServiceGin

func (ServiceGin) BindMiddleware

func (s ServiceGin) BindMiddleware(r interface{}) gin.HandlerFunc

Attempts to bind a JSON request body from the Gin Context to the provided struct. If any of the struct's required fields are missing from the request body, a 400 response is sent.

func (ServiceGin) DebugEnabled

func (s ServiceGin) DebugEnabled() bool

func (ServiceGin) FilterMiddleware added in v0.1.29

func (s ServiceGin) FilterMiddleware() gin.HandlerFunc

func (ServiceGin) GetFilter added in v0.1.29

func (s ServiceGin) GetFilter(c *gin.Context) *query.Query

func (ServiceGin) GetHandlerContext added in v0.1.29

func (s ServiceGin) GetHandlerContext(c *gin.Context) context.Context

func (ServiceGin) GetRequest added in v0.1.29

func (s ServiceGin) GetRequest(c *gin.Context) interface{}

func (ServiceGin) NewRouter

func (s ServiceGin) NewRouter() *RouterGin

Jump to

Keyboard shortcuts

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