Documentation
¶
Index ¶
- func AssertBadRequest(t *testing.T, response *Response, messages ...string)
- func AssertFail(t *testing.T, response *Response)
- func AssertForbidden(t *testing.T, response *Response, messages ...string)
- func AssertHTTPError(code int, defMessage string, t *testing.T, response *Response, ...)
- func AssertOk(t *testing.T, response *Response)
- func AssertResponseError(t *testing.T, response *Response, messages ...string)
- func AssertUnauthorized(t *testing.T, response *Response, messages ...string)
- type API
- func (api *API) Map(httpMethods string, path string, handlers ...RouteHandler)
- func (api *API) Run(addr string) error
- func (api *API) RunTLS(addr string, certFile string, keyFile string) error
- func (api *API) RunUnix(file string) error
- func (api *API) SetEndRequestHandler(handler RouteHandler)
- func (api *API) SetGlobalContext(context interface{})
- func (api *API) SetGracefulTimeout(timeoutSeconds time.Duration)
- func (api *API) SetInitRequestHandler(handler RouteHandler)
- func (api *API) SetLogger(logger ILogger)
- type ILogger
- type Request
- type Response
- func BadRequest() *Response
- func BadRequestMessage(message string) *Response
- func Error(err error) *Response
- func ErrorMessage(message string) *Response
- func Fail(code int, data interface{}, errorData ResponseError) *Response
- func Forbidden() *Response
- func ForbiddenMessage(message string) *Response
- func Next(data ...interface{}) *Response
- func Ok(data interface{}) *Response
- func Unauthorized() *Response
- func UnauthorizedMessage(message string) *Response
- type ResponseError
- type Route
- type RouteHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertBadRequest ¶
AssertBadRequest checks expected properties of BadRequest response.
func AssertFail ¶
AssertFail checks expected properties of Fail response.
func AssertForbidden ¶
AssertForbidden checks expected properties of Forbidden response.
func AssertHTTPError ¶
func AssertHTTPError( code int, defMessage string, t *testing.T, response *Response, messages ...string)
AssertHTTPError checks expected properties of HTTP error response.
func AssertResponseError ¶
AssertResponseError checks expected properties of Error response.
Types ¶
type API ¶
type API struct {
// contains filtered or unexported fields
}
API provides functionality to map handler functions on specific HTTP routes.
func (*API) Map ¶
func (api *API) Map(httpMethods string, path string, handlers ...RouteHandler)
Map assigns a chain of handlers to a specific URL path available via specific HTTP methods. List of HTTP methods should be specified as a string e.g. "get,post,put" or just "get". Path format is compatible with gin https://github.com/gin-gonic/gin#parameters-in-path Handlers must be specified in consequent order. They are called in that same order and response might be returned on every handler if it returns response with EndRequest flag.
func (*API) RunTLS ¶
RunTLS starts API on specified TCP address, serving requests via TLS. Certificate and key file paths must be specified.
func (*API) SetEndRequestHandler ¶
func (api *API) SetEndRequestHandler(handler RouteHandler)
SetEndRequestHandler sets a handler function which will be called after user defined handlers on every request specified in routes. This handler won't be called on routes without custom handlers.
func (*API) SetGlobalContext ¶
func (api *API) SetGlobalContext(context interface{})
SetGlobalContext stores an object of any kind as global context. This context is then passed to every request. For example app configuration can be passed via global context to each handler.
func (*API) SetGracefulTimeout ¶
SetGracefulTimeout sets timeout in seconds to wait for connections to finish before app restart. Default value is 1 min.
func (*API) SetInitRequestHandler ¶
func (api *API) SetInitRequestHandler(handler RouteHandler)
SetInitRequestHandler sets a handler function which will be called before user defined handlers on every request specified in routes. This handler won't be called on routes without custom handlers.
type ILogger ¶
type ILogger interface {
Debug(format string, v ...interface{})
Info(format string, v ...interface{})
Warn(format string, v ...interface{})
Error(format string, v ...interface{})
}
ILogger is a definition of an object capable of logging. When handling a request we quite often find ourselves in need to log some error or event. Implementation of such logging varies. So in this library we allow users to provide their own loggers, compatible with this interface.
type Request ¶
type Request struct {
// Context is a gin-specific request context.
// TODO it's better to hide it and expose via public functions or properties.
Context *gin.Context
// GlobalContext is something user desided to pass to every request.
// Typecally it's some sort of global configuration.
GlobalContext interface{}
// PrevHandlerResponse is a response of a previous handler.
// For the first handler it's nil.
PrevHandlerResponse *Response
// Logger is a user defined logging interface.
Logger ILogger
}
Request contains request specific data.
type Response ¶
type Response struct {
Successful bool `json:"successful"`
Error ResponseError `json:"error"`
Data interface{} `json:"data"`
HTTPCode int `json:"-"`
// EndRequest specifies whether this response should be considered as final.
EndRequest bool `json:"-"`
}
Response describes common service response format. Every HTTP response is wrapped in this structure. NOTE this structure isn't directly serialized to JSON anywhere except tests. Instead we take required fields because error field, for example, isn't always needed in response and it wounldn't be nice to bloat responses with redundant data.
func BadRequestMessage ¶
BadRequestMessage creates 400 Bad Request HTTP response with specified message.
func ErrorMessage ¶
ErrorMessage creates 500 internal error HTTP response with specified message.
func Fail ¶
func Fail(code int, data interface{}, errorData ResponseError) *Response
Fail creates unsuccessful response with error information.
func ForbiddenMessage ¶
ForbiddenMessage creates 403 Forbidden HTTP response with specified message.
func Next ¶
func Next(data ...interface{}) *Response
Next creates response which indicates that next handler in chain should be called.
func Unauthorized ¶
func Unauthorized() *Response
Unauthorized creates 401 Unauthorized HTTP response.
func UnauthorizedMessage ¶
UnauthorizedMessage creates 401 Unauthorized HTTP response with specified message.
type ResponseError ¶
type ResponseError struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
ResponseError describes error information returned in response.
type Route ¶
type Route struct {
Method string
Path string
Handlers []RouteHandler
}
Route is a definition of API endpoint with specific path, HTTP method and chain of handlers.
type RouteHandler ¶
RouteHandler is a definition of a function which handles request on specific route.