context

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package context provides a request and response struct which can be used in the controller. Request provides a lot of helper function and the Response offers a simple render function.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownProvider       = "cache: unknown response-provider %q"
	ErrNoProvider            = errors.New("cache: empty cache-name or cache-provider is nil")
	ErrProviderAlreadyExists = "cache: cache-provider %#v is already registered"
)

Error messages

View Source
var ErrParameter = errors.New("controller/request: the parameter %#v does not exist")

ErrParameter error message

Functions

func Providers

func Providers() map[string]provider

Providers return all registered providers

func Register

func Register(provider string, fn provider) error

Register the cache provider. This should be called in the init() of the providers. If the cache provider/name is empty or is already registered, an error will return.

Types

type Context

type Context struct {
	Request  *Request
	Response *Response
}

Context of the controller.

func New

func New(req *http.Request, res http.ResponseWriter) *Context

New returns a context for the request and response.

type Interface

type Interface interface {
	Write(response *Response) error
	Name() string
	Icon() string
}

func NewJson

func NewJson() Interface

New satisfies the config.provider interface.

type Request

type Request struct {
	// contains filtered or unexported fields
}

Request struct.

func (*Request) Body

func (req *Request) Body() []byte

func (*Request) Domain

func (req *Request) Domain() string

Domain is an alias of Host method.

Example: https://example.com:8080/user?id=12#test
example.com

func (*Request) File

func (req *Request) File(k string) ([]*multipart.FileHeader, error)

File returns the requested file of a multipart POST. It returns a []*FileHeader because the underlying input field could be an array. Error will return on parse error or if the key does not exist.

func (*Request) Files

func (req *Request) Files() (map[string][]*multipart.FileHeader, error)

Files returns all existing files. It returns a map[string][]*FileHeader because the underlying input field could be an array. Error will return on parse error.

func (*Request) FullURL

func (req *Request) FullURL() string

FullURL returns the schema,host,port,uri

Example: https://example.com:8080/user?id=12#test
https://example.com:8080/user?id=12#test

func (*Request) Host

func (req *Request) Host() string

Host returns the host name. Port number will be removed if existing. If no host info is available, localhost will return.

Example: https://example.com:8080/user?id=12#test
example.com

func (*Request) IP

func (req *Request) IP() string

IP of the request. First it checks the proxy X-Forwarded-For Header and takes the first entry - if exists. Otherwise the RemoteAddr will be returned without the Port. TODO Header RemoteAddr (is it official?) or X-Real-Ip

func (*Request) Is

func (req *Request) Is(m string) bool

Is checks the given method with the request HTTP Method. Both strings are getting set to uppercase.

func (*Request) IsDelete

func (req *Request) IsDelete() bool

IsDelete checks if its a HTTP DELETE Method.

func (*Request) IsGet

func (req *Request) IsGet() bool

IsGet checks if its a HTTP GET Method.

func (*Request) IsPatch

func (req *Request) IsPatch() bool

IsPatch checks if its a HTTP PATCH Method.

func (*Request) IsPost

func (req *Request) IsPost() bool

IsPost checks if its a HTTP POST Method.

func (*Request) IsPut

func (req *Request) IsPut() bool

IsPut checks if its a HTTP PUT Method.

func (*Request) IsSecure

func (req *Request) IsSecure() bool

IsSecure checks if the request is HTTPS.

func (*Request) Localizer

func (req *Request) Localizer() locale.LocalizerI

func (*Request) Method

func (req *Request) Method() string

Method returns the HTTP Method in uppercase.

GET

func (*Request) Param

func (req *Request) Param(k string) ([]string, error)

Param returns the requested parameter. It returns a []string because the underlying HTML input field could be an array. Error will return on parse error or if the key does not exist.

func (*Request) Params

func (req *Request) Params() (map[string][]string, error)

Params returns all existing parameters. It returns a map[string][]string because the underlying HTML input field could be an array. Error will return on parse error.

func (*Request) Pattern

func (req *Request) Pattern() string

Pattern returns the router url pattern. The pattern must be defined in the request context by the key router.PATTERN. This is usually done by the router.

Example: http://example.com/user/1
/user/:id

func (*Request) Port

func (req *Request) Port() int

Port returns request client port. On error or if its empty the standard port 80 will return.

func (*Request) Protocol

func (req *Request) Protocol() string

Protocol returns the protocol name, such as HTTP/1.1 .

func (*Request) Proxy

func (req *Request) Proxy() []string

Proxy return all IPs which are in the X-Forwarded-For header.

func (*Request) Raw

func (req *Request) Raw() *http.Request

Raw returns the original *http.Request

func (*Request) Referer

func (req *Request) Referer() string

Referer returns the Referer Header

func (*Request) Scheme

func (req *Request) Scheme() string

Scheme (http/https) checks the `X-Forwarded-Proto` header. If that one is empty the URL.Scheme gets checked. If that is also empty the request TLS will be checked.

func (*Request) SetBody

func (req *Request) SetBody(body []byte)

func (*Request) Site

func (req *Request) Site() string

Site returns base site url as scheme://domain type without the port.

Example: https://example.com:8080/user?id=12#test
https://example.com

func (*Request) Token

func (req *Request) Token() interface{}

Token returns the jwt token TODO check if needed?

func (*Request) URI

func (req *Request) URI() string

URI returns full request url with query string, fragment.

Example: https://example.com:8080/user?id=12#test
/user?id=12#test

func (*Request) URL

func (req *Request) URL() string

URL returns request url path without the query string and fragment.

Example: https://example.com:8080/user?id=12#test
/user

func (*Request) UserAgent

func (req *Request) UserAgent() *UserAgent

UserAgent parses the User-Agent header and return a UserAgent struct.

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response struct.

func (*Response) Data

func (o *Response) Data(key string) interface{}

Data returned by the key. If the key does not exist, nil will return.

func (*Response) Raw

func (o *Response) Raw() http.ResponseWriter

Raw returns the original *http.ResponseWriter

func (*Response) Render

func (o *Response) Render(renderType string) error

Render the response with the given renderType. Error will return if the render provider is not registered.

func (*Response) SetData

func (o *Response) SetData(key string, value interface{})

SetData by key and value.

type UserAgent

type UserAgent struct {
	// contains filtered or unexported fields
}

UserAgent struct.

func (*UserAgent) Browser

func (us *UserAgent) Browser() browserInfo

Browser returns the browser information which contain the Name and Version.

func (*UserAgent) Mobile

func (us *UserAgent) Mobile() bool

Mobile returns a boolean

func (*UserAgent) OS

func (us *UserAgent) OS() osInfo

OS returns the operation system information which contain the Name and Version.

Jump to

Keyboard shortcuts

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