Documentation ¶
Index ¶
- Constants
- Variables
- type BaseController
- func (c *BaseController) Err(err error, code int) *Result
- func (c *BaseController) ErrBadGateway(err error) *Result
- func (c *BaseController) ErrBadRequest(err error) *Result
- func (c *BaseController) ErrForbidden(err error) *Result
- func (c *BaseController) ErrGatewayTimeout(err error) *Result
- func (c *BaseController) ErrInternalServerError(err error) *Result
- func (c *BaseController) ErrMethodNotAllowed(err error) *Result
- func (c *BaseController) ErrNotFound(err error) *Result
- func (c *BaseController) ErrServiceUnavailable(err error) *Result
- func (c *BaseController) ErrUnauthorized(err error) *Result
- func (c *BaseController) OK(v json.Marshaler) *Result
- func (c *BaseController) OKBytes(v []byte) *Result
- func (c *BaseController) OKList(v []json.Marshaler) *Result
- func (c *BaseController) OKString(v string) *Result
- type BytesResult
- type CRUDClient
- type CRUDController
- type CRUDModel
- type Client
- func (c *Client) Delete(uri string) (*Response, error)
- func (c *Client) Get(uri string) (*Response, error)
- func (c *Client) Post(uri string, body json.Marshaler) (*Response, error)
- func (c *Client) Put(uri string, body json.Marshaler) (*Response, error)
- func (c *Client) Request() *Request
- func (c *Client) SetAuthFunc(authFunc ClientAuthFunc) *Client
- func (c *Client) UseSSL() *Client
- type ClientAuthFunc
- type Controller
- type ControllerHandler
- type ControllerMethods
- type ControllerPaths
- type Ctx
- func (c *Ctx) Err(err error, code int)
- func (c *Ctx) ErrBadGateway(err error)
- func (c *Ctx) ErrBadRequest(err error)
- func (c *Ctx) ErrForbidden(err error)
- func (c *Ctx) ErrGatewayTimeout(err error)
- func (c *Ctx) ErrInternalServerError(err error)
- func (c *Ctx) ErrMethodNotAllowed(err error)
- func (c *Ctx) ErrNotFound(err error)
- func (c *Ctx) ErrServiceUnavailable(err error)
- func (c *Ctx) ErrUnauthorized(err error)
- func (c *Ctx) GetHeader(k string) string
- func (c *Ctx) GetParamFloat64(k string) (float64, error)
- func (c *Ctx) GetParamInt(k string) (int, error)
- func (c *Ctx) GetParamInt64(k string) (int64, error)
- func (c *Ctx) GetParamString(k string) string
- func (c *Ctx) GetParamUint64(k string) (uint64, error)
- func (c *Ctx) OK(v json.Marshaler)
- func (c *Ctx) ReadJSON(v json.Unmarshaler) error
- func (c *Ctx) SetHeader(k string, v string)
- func (c *Ctx) WriteJSON(v json.Marshaler)
- type Error
- type Handler
- type ID
- type ListResult
- type Request
- func (r *Request) Do() (*Response, error)
- func (r *Request) SetBody(body []byte) *Request
- func (r *Request) SetHeader(k string, v string) *Request
- func (r *Request) SetHeaders(headers map[string]string) *Request
- func (r *Request) SetMethod(method string) *Request
- func (r *Request) SetURI(uri string) *Request
- type Response
- type Result
- type Server
- func (s *Server) CRUDController(path string, ctrl CRUDController) *Server
- func (s *Server) Connect(p string, handler Handler) *Server
- func (s *Server) Controller(basePath string, ctrl Controller) *Server
- func (s *Server) ControllerMethod(method string, p string, handler ControllerHandler) *Server
- func (s *Server) Delete(p string, handler Handler) *Server
- func (s *Server) Get(p string, handler Handler) *Server
- func (s *Server) GetAddr() string
- func (s *Server) Head(p string, handler Handler) *Server
- func (s *Server) Listen() error
- func (s *Server) ListenTLS(certFile string, keyFile string) error
- func (s *Server) ListenTLSEmbed(cert []byte, key []byte) error
- func (s *Server) ListenUNIX(mode os.FileMode) error
- func (s *Server) Options(p string, handler Handler) *Server
- func (s *Server) Patch(p string, handler Handler) *Server
- func (s *Server) Post(p string, handler Handler) *Server
- func (s *Server) Put(p string, handler Handler) *Server
- func (s *Server) Route(method string, p string, handler Handler) *Server
- func (s *Server) SetAddr(addr string) *Server
- func (s *Server) SetAuthFunc(authFunc ServerAuthFunc)
- func (s *Server) SetListener(ln net.Listener) *Server
- func (s *Server) Trace(p string, handler Handler) *Server
- type ServerAuthFunc
- type StringResult
Constants ¶
const ( DefaultAddr = ":80" MethodGet = "GET" MethodHead = "HEAD" MethodPost = "POST" MethodPut = "PUT" MethodPatch = "PATCH" MethodDelete = "DELETE" MethodConnect = "CONNECT" MethodOptions = "OPTIONS" MethodTrace = "TRACE" StatusOK = 200 StatusBadRequest = 400 StatusForbidden = 403 StatusNotFound = 404 StatusMethodNotAllowed = 405 StatusInternalServerError = 500 StatusBadGateway = 502 StatusGatewayTimeout = 504 )
Variables ¶
var (
)Functions ¶
This section is empty.
Types ¶
type BaseController ¶
type BaseController struct { }
BaseController is a helper struct that implements basic response methods that can be used by inheriting controller
func (*BaseController) Err ¶
func (c *BaseController) Err(err error, code int) *Result
Err returns an error response with http code
func (*BaseController) ErrBadGateway ¶
func (c *BaseController) ErrBadGateway(err error) *Result
ErrBadGateway return http error BadGateway
func (*BaseController) ErrBadRequest ¶
func (c *BaseController) ErrBadRequest(err error) *Result
ErrBadRequest return http error BadRequest
func (*BaseController) ErrForbidden ¶
func (c *BaseController) ErrForbidden(err error) *Result
ErrForbidden return http error Forbidden
func (*BaseController) ErrGatewayTimeout ¶
func (c *BaseController) ErrGatewayTimeout(err error) *Result
ErrGatewayTimeout return http error GatewayTimeout
func (*BaseController) ErrInternalServerError ¶
func (c *BaseController) ErrInternalServerError(err error) *Result
ErrInternalServerError return http error InternalServerError
func (*BaseController) ErrMethodNotAllowed ¶
func (c *BaseController) ErrMethodNotAllowed(err error) *Result
ErrMethodNotAllowed return http error MethodNotAllowed
func (*BaseController) ErrNotFound ¶
func (c *BaseController) ErrNotFound(err error) *Result
ErrNotFound returns http error NotFound
func (*BaseController) ErrServiceUnavailable ¶
func (c *BaseController) ErrServiceUnavailable(err error) *Result
ErrServiceUnavailable return http error ServiceUnavailable
func (*BaseController) ErrUnauthorized ¶
func (c *BaseController) ErrUnauthorized(err error) *Result
ErrUnauthorized return http error Unauthorized
func (*BaseController) OK ¶
func (c *BaseController) OK(v json.Marshaler) *Result
OK returns 200 OK response
func (*BaseController) OKBytes ¶
func (c *BaseController) OKBytes(v []byte) *Result
Bytes is a method for returning []byte result (for example: raw json as []byte)
func (*BaseController) OKList ¶
func (c *BaseController) OKList(v []json.Marshaler) *Result
List is a method for returning list of []json.Marshaler without the need to create a custom struct wrapper
func (*BaseController) OKString ¶
func (c *BaseController) OKString(v string) *Result
String is a method for returning string result (for example: raw json as string)
type BytesResult ¶
type BytesResult []byte
BytesResult is a wrapper for bytes to return it as json.Marshaler
func (BytesResult) MarshalJSON ¶
func (b BytesResult) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler
type CRUDClient ¶
type CRUDClient struct {
// contains filtered or unexported fields
}
func NewCRUDClient ¶
func NewCRUDClient(addr string, path string) *CRUDClient
func (*CRUDClient) Create ¶
func (c *CRUDClient) Create(v CRUDModel) error
func (*CRUDClient) Delete ¶
func (c *CRUDClient) Delete(id ID)
func (*CRUDClient) Get ¶
func (c *CRUDClient) Get(v CRUDModel) error
func (*CRUDClient) GetByID ¶
func (c *CRUDClient) GetByID(id ID, v CRUDModel)
func (*CRUDClient) Update ¶
func (c *CRUDClient) Update(id ID, v CRUDModel)
type CRUDController ¶
type CRUDController interface { Create(*Ctx) *Result // POST /path Get(*Ctx) *Result // GET /path GetByID(*Ctx) *Result // GET /path/:id Update(*Ctx) *Result // PUT /path/:id Delete(*Ctx) *Result // DELETE /path/:id }
CRUDController interface defines a basic CRUDController controller
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client describes jsonapi client
func (*Client) SetAuthFunc ¶
func (c *Client) SetAuthFunc(authFunc ClientAuthFunc) *Client
SetAuthFunc sets authentication modifier function
type ClientAuthFunc ¶
type ClientAuthFunc func(*Request)
ClientAuthFunc is modifying the client request to implement authentication in client
type Controller ¶
type Controller interface {
Methods() ControllerMethods // should return controller methods
}
Controller interface
type ControllerHandler ¶
ControllerHandler defines controller handler func
type ControllerMethods ¶
type ControllerMethods map[string]ControllerPaths
ControllerMethods is a map linking http method to controller paths
type ControllerPaths ¶
type ControllerPaths map[string]ControllerHandler
ControllerPaths is a map linking path to handler
type Ctx ¶
type Ctx struct {
*fasthttp.RequestCtx
}
Ctx is a wrapper for fasthttp.RequestCtx
func (*Ctx) ErrBadGateway ¶
ErrBadGateway writes http error BadGateway to response body
func (*Ctx) ErrBadRequest ¶
ErrBadRequest writes http error BadRequest to response body
func (*Ctx) ErrForbidden ¶
ErrForbidden writes http error Forbidden to response body
func (*Ctx) ErrGatewayTimeout ¶
ErrGatewayTimeout writes http error GatewayTimeout to response body
func (*Ctx) ErrInternalServerError ¶
ErrInternalServerError writes http error InternalServerError to response body
func (*Ctx) ErrMethodNotAllowed ¶
ErrMethodNotAllowed writes http error MethodNotAllowed to response body
func (*Ctx) ErrNotFound ¶
ErrNotFound writes http error NotFound to response body
func (*Ctx) ErrServiceUnavailable ¶
ErrServiceUnavailable writes http error ServiceUnavailable to response body
func (*Ctx) ErrUnauthorized ¶
ErrUnauthorized writes http error Unauthorized to response body
func (*Ctx) GetParamFloat64 ¶
GetParamFloat64 returns path parameter k as float64 or error
func (*Ctx) GetParamInt ¶
GetParamInt returns path parameter k as int or error
func (*Ctx) GetParamInt64 ¶
GetParamInt64 returns path parameter k as int64 or error
func (*Ctx) GetParamString ¶
GetParamString returns path parameter k as string
func (*Ctx) GetParamUint64 ¶
GetParamUint64 returns path parameter k as uint64 or error
func (*Ctx) ReadJSON ¶
func (c *Ctx) ReadJSON(v json.Unmarshaler) error
ReadJSON will try to read request body into v
type Error ¶
Error is a custom error object
func NewErrorString ¶
func (Error) MarshalEasyJSON ¶
MarshalEasyJSON supports easyjson.Marshaler interface
func (Error) MarshalJSON ¶
MarshalJSON supports json.Marshaler interface
func (*Error) UnmarshalEasyJSON ¶
UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (*Error) UnmarshalJSON ¶
UnmarshalJSON supports json.Unmarshaler interface
type ListResult ¶
ListResult is a wrapper for []json.Marshaler to return a list without declaring a custom struct for the list
func (ListResult) MarshalJSON ¶
func (l ListResult) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request is the request object
func (*Request) Do ¶
Do executes the http request and returns *Response or error if the request failed
func (*Request) SetHeaders ¶
SetHeaders is setting(replacing) request headers map
type Response ¶
Response is a wrapper for *fasthttp.Response it adds some useful methods for working with json
type Result ¶
Result is an object returned from controller method If Err is not nil, an error will be returned to the client
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is an http server wrapper
func (*Server) CRUDController ¶
func (s *Server) CRUDController(path string, ctrl CRUDController) *Server
CRUDController assigns a crud controller to a path
func (*Server) Controller ¶
func (s *Server) Controller(basePath string, ctrl Controller) *Server
Controller registers a controller
func (*Server) ControllerMethod ¶
func (s *Server) ControllerMethod(method string, p string, handler ControllerHandler) *Server
ControllerMethod registers a controller method handler by http method and path This method can be called directly without a controller.
func (*Server) ListenTLS ¶
ListenTLS starts http server and listens on defined addr with TLS Reads TLS certificate from certFile and key from keyFile
func (*Server) ListenTLSEmbed ¶
ListenTLSEmbed starts http server and listens on defined addr with TLS Accepts TLS certificate in cert and key in key
func (*Server) ListenUNIX ¶
ListenUNIX starts http server and listens on UNIX socket Accepts mode as file mode
func (*Server) SetAuthFunc ¶
func (s *Server) SetAuthFunc(authFunc ServerAuthFunc)
SetAuthFunc sets authentication func that will be triggered on every request to the server
func (*Server) SetListener ¶
SetListener sets net.Listener that will be used
type ServerAuthFunc ¶
ServerAuthFunc is checking the server request for authentication and returns a bool
type StringResult ¶
type StringResult string
StringResult is a wrapper for string to return it json.Marshaler
func (StringResult) MarshalJSON ¶
func (s StringResult) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler