http

package
v0.0.0-...-24a4aaa Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2018 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package http contains the basic framework on top of which the Misirlou API is built.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GET

func GET(path string, handler func(c *Context))

GET registers a handler for a GET request.

func Handler

func Handler(o Options) fasthttp.RequestHandler

Handler creates an HTTP request handler using httprouter.

func POST

func POST(path string, handler func(c *Context))

POST registers a handler for a POST request.

func PUT

func PUT(path string, handler func(c *Context))

PUT registers a handler for a PUT request.

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) Cookie

func (c *Context) Cookie(s string) string

Cookie returns the requested cookie.

func (*Context) DeleteCookie

func (c *Context) DeleteCookie(k string)

DeleteCookie removes a cookie from the client.

func (*Context) Error

func (c *Context) Error(err error)

Error closes the request with a 500 code and prints the error to stderr.

func (*Context) Header

func (c *Context) Header(s string) []byte

Header retrieves a header from the request.

func (*Context) IP

func (c *Context) IP() net.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) JSON

func (c *Context) JSON(v interface{}) error

JSON unmarshals the request's body into v, and returns any error.

func (*Context) ParamID

func (c *Context) ParamID(s string) (i models.ID)

ParamID takes a named parameter set in the route of the request, and parses it as an ID.

func (*Context) ParamInt

func (c *Context) ParamInt(s string) int

ParamInt takes a named parameter set in the route of the request, and parses it as an int.

func (*Context) Query

func (c *Context) Query(s string) string

Query retrieves a value from the query string.

func (*Context) QueryID

func (c *Context) QueryID(s string) (i models.ID)

QueryID retrieves a value from the querystring, and parses it as an ID.

func (*Context) QueryInt

func (c *Context) QueryInt(s string) int

QueryInt retrieves a value from the querystring, and parses it as an int.

func (*Context) Redirect

func (c *Context) Redirect(code int, location string)

Redirect redirects the client to the given page, using the given response code.

func (*Context) Session

func (c *Context) Session() *models.Session

Session retrieves the Session related to this context.

func (*Context) SetBody

func (c *Context) SetBody(b []byte)

SetBody changes the existing response body with the passed body.

func (*Context) SetCode

func (c *Context) SetCode(i int)

SetCode sets the status code for the response, and sends all the header to the response.

func (*Context) SetCookie

func (c *Context) SetCookie(k, v string, expire time.Duration)

SetCookie sets a cookie in the user's browser.

func (*Context) SetHeader

func (c *Context) SetHeader(k, v string)

SetHeader sets an header in the response.

func (*Context) SetJSON

func (c *Context) SetJSON(v interface{}, is404 bool)

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

func (c *Context) SetJSONWithCode(v interface{}, code int)

SetJSONWithCode sets the response body to the given JSON value, as well as the HTTP response code.

func (*Context) WriteString

func (c *Context) WriteString(s string)

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

type ResponseError struct {
	Code    int
	Message string
}

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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