Documentation
¶
Overview ¶
Package api is the atomic api helper library
Index ¶
- func Log(ctx context.Context) log.Interface
- func Request(ctx context.Context) (*http.Request, http.ResponseWriter)
- func RequestBody(ctx context.Context) []byte
- func RequestHost(ctx context.Context) string
- type Authorizer
- type ContextFunc
- type EncodeFunc
- type Encoder
- type Option
- func WithAddr(addr string) Option
- func WithBasepath(base string) Option
- func WithCORS(origin ...string) Option
- func WithCache(ttl time.Duration) Option
- func WithListener(l net.Listener) Option
- func WithLog(l log.Interface) Option
- func WithName(name string) Option
- func WithRouter(router *mux.Router) Option
- func WithVersioning(version string, serverVersion ...string) Option
- type Parameters
- type RedirectError
- type Responder
- type Response
- func Error(e error) *Response
- func ErrorRedirect(r RedirectError) *Response
- func Errorf(f string, args ...interface{}) *Response
- func NewResponse(payload ...interface{}) *Response
- func Redirect(u *url.URL, args ...map[string]string) *Response
- func StatusError(status int, e error) *Response
- func StatusErrorf(status int, f string, args ...interface{}) *Response
- func (r *Response) Payload() interface{}
- func (r *Response) Status() int
- func (r *Response) WithHeader(key string, value string) *Response
- func (r *Response) WithHeaders(h map[string]interface{}) *Response
- func (r *Response) WithStatus(status int) *Response
- func (r *Response) Write(w http.ResponseWriter, req *http.Request) error
- type RouteOption
- type Server
- func (s *Server) AddRoute(path string, handler interface{}, opts ...RouteOption)
- func (s *Server) Handler() http.Handler
- func (s *Server) Log() log.Interface
- func (s *Server) LogMiddleware() func(http.Handler) http.Handler
- func (s *Server) RequestVersion(r *http.Request) string
- func (s *Server) Router() *mux.Router
- func (s *Server) Serve() error
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) Shutdown(ctx context.Context) error
- func (s *Server) WriteError(w http.ResponseWriter, status int, err error)
- func (s *Server) WriteJSON(w http.ResponseWriter, status int, v interface{}, pretty ...bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RequestBody ¶
RequestBody returns the raw request body
func RequestHost ¶
RequestHost returns the host from the current request
Types ¶
type Authorizer ¶
Authorizer performs an autorization and returns a context or error on failure
type ContextFunc ¶
ContextFunc adds context to a request
type Option ¶
type Option func(*Server)
Option provides the server options, these will override th defaults and any atomic instance values.
func WithBasepath ¶
WithBasepath sets the router basepath for the api
func WithListener ¶
WithListener sets the net listener for the server
func WithVersioning ¶
WithVersioning enables versioning that will enforce a versioned path and optionally set the Server header to the serverVersion
type Parameters ¶
type Parameters interface {
Validate() error
}
Parameters interface handles binding requests
type RedirectError ¶
RedirectError is used to redirect it the url is valid
type Responder ¶
type Responder interface { // Status returns the http status Status() int // Payload is the payload Payload() interface{} // Write writes ou the payload Write(w http.ResponseWriter, r *http.Request) error }
Responder is an api response interface
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response is the common response type
func ErrorRedirect ¶
func ErrorRedirect(r RedirectError) *Response
ErrorRedirect does a redirect if there is a url otherwise returns the error directly
func NewResponse ¶
func NewResponse(payload ...interface{}) *Response
NewResponse returns a response with defaults
func StatusError ¶
StatusError sets the status and error message in one go
func StatusErrorf ¶
StatusErrorf sets the status and error message in one go
func (*Response) WithHeader ¶
WithHeader adds headers to the request
func (*Response) WithHeaders ¶
WithHeaders adds a map of header values
func (*Response) WithStatus ¶
WithStatus sets the status
type RouteOption ¶
type RouteOption func(*routeOption)
RouteOption defines route options
func WithAuthorizers ¶
func WithAuthorizers(a ...Authorizer) RouteOption
WithAuthorizers sets the authorizers
func WithContextFunc ¶
func WithContextFunc(f ContextFunc) RouteOption
WithContextFunc sets the context handler for the route option
func WithMethod ¶
func WithMethod(m string) RouteOption
WithMethod sets the method for the route option
func WithParams ¶
func WithParams(p interface{}) RouteOption
WithParams sets the params for the route option
func WithValidation ¶
func WithValidation(v bool) RouteOption
WithValidation sets the parameter validation
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is an http server that provides basic REST funtionality
func (*Server) AddRoute ¶
func (s *Server) AddRoute(path string, handler interface{}, opts ...RouteOption)
AddRoute adds a route in the clear
func (*Server) LogMiddleware ¶
LogMiddleware is simple logging middleware handler
func (*Server) RequestVersion ¶
RequestVersion returns the request version or the server version is not found
func (*Server) WriteError ¶
func (s *Server) WriteError(w http.ResponseWriter, status int, err error)
WriteError writes an error object