Documentation ¶
Overview ¶
Package http contains the basic framework on top of which the Misirlou API is built.
Index ¶
- func GET(path string, handler func(c *Context))
- func Handler(o Options) fasthttp.RequestHandler
- func POST(path string, handler func(c *Context))
- func PUT(path string, handler func(c *Context))
- type Context
- func (c *Context) Cookie(s string) string
- func (c *Context) DeleteCookie(k string)
- func (c *Context) Error(err error)
- func (c *Context) Header(s string) []byte
- func (c *Context) IP() net.IP
- func (c *Context) JSON(v interface{}) error
- func (c *Context) ParamID(s string) (i models.ID)
- func (c *Context) ParamInt(s string) int
- func (c *Context) Query(s string) string
- func (c *Context) QueryID(s string) (i models.ID)
- func (c *Context) QueryInt(s string) int
- func (c *Context) Redirect(code int, location string)
- func (c *Context) Session() *models.Session
- func (c *Context) SetBody(b []byte)
- func (c *Context) SetCode(i int)
- func (c *Context) SetCookie(k, v string, expire time.Duration)
- func (c *Context) SetHeader(k, v string)
- func (c *Context) SetJSON(v interface{}, is404 bool)
- func (c *Context) SetJSONWithCode(v interface{}, code int)
- func (c *Context) WriteString(s string)
- type Options
- type ResponseError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Handler ¶
func Handler(o Options) fasthttp.RequestHandler
Handler creates an HTTP request handler using httprouter.
Types ¶
type Context ¶
type Context struct { Options // contains filtered or unexported fields }
Context is the information passed to each request about the requested method.
func (*Context) DeleteCookie ¶
DeleteCookie removes a cookie from the client.
func (*Context) IP ¶
IP retrieves the IP address of the request. If the remote addr is loopback or invalid (e.g. an unix socket), then it is allowed to change the IP address by using the request header X-Forwarded-For or X-Real-IP.
func (*Context) ParamID ¶
ParamID takes a named parameter set in the route of the request, and parses it as an ID.
func (*Context) ParamInt ¶
ParamInt takes a named parameter set in the route of the request, and parses it as an int.
func (*Context) QueryInt ¶
QueryInt retrieves a value from the querystring, and parses it as an int.
func (*Context) Redirect ¶
Redirect redirects the client to the given page, using the given response code.
func (*Context) SetCode ¶
SetCode sets the status code for the response, and sends all the header to the response.
func (*Context) SetJSON ¶
SetJSON sets the response body to the given JSON value. The second value defines whether the response code should be 404 of 200. It's useful to quickly set the code to 404 when the value is null: c.SetJSON(t, t == nil)
func (*Context) SetJSONWithCode ¶
SetJSONWithCode sets the response body to the given JSON value, as well as the HTTP response code.
func (*Context) WriteString ¶
WriteString writes s to the response. We provide WriteString and not Write because c.ctx.Write really makes a b2s conversion and then calls AppendBodyString. To avoid this indirection, we do not provide Write, and instead provide a WriteString with no return arguments - that is because even if there was (int, error), they would always be len(s) and nil.
type Options ¶
type Options struct { DB *models.DB Redis *redis.Client OAuth2ClientID string OAuth2ClientSecret string BaseURL string StoreTokensURL string HTTPS bool }
Options is a struct which is embedded in every context and contains information passed to the handlers directly by the main package.
type ResponseError ¶
ResponseError can be passed to Error, and instead of returning a 500, it will return a response with the code and the message specified.
func (*ResponseError) Error ¶
func (re *ResponseError) Error() string