context

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2021 License: MIT Imports: 11 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 data store and different render options.

Package context todos: TODO parse files, localizer TODO body/file size limit

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultLang string
)

Localizer of the request. Its added by the server.Translation. The controller can not call the server package (import cycle), thats why its here.

View Source
var (
	ErrParam = "context: the param %#v does not exist"
)

Error messages.

Functions

func RegisterRenderer

func RegisterRenderer(name string, renderer providerFn) error

RegisterRenderer provider.

func RenderTypes

func RenderTypes() (map[string]Renderer, error)

RenderTypes returns all registered render providers. Error will return if a renderer constructor returns one.

Types

type Context

type Context struct {
	Request  *Request
	Response *Response
}

Context of the controller.

func New

New returns a Context for the Request and Response.

type Renderer

type Renderer interface {
	Name() string
	Icon() string
	Write(response *Response) error
	Error(response *Response, code int, err error) error
}

Renderer interface for the render providers.

func RenderType

func RenderType(name string) (Renderer, error)

RenderType return a registered render provider by name. Error will return if it does not exist or the renderer constructor returns one.

type Request

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

Request struct.

func (*Request) Body

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

Body reads the raw body data.

func (*Request) Domain

func (r *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 (r *Request) File(k string) ([]*multipart.FileHeader, error)

File returns the file by key. 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 (r *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 (r *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) HTTPRequest

func (r *Request) HTTPRequest() *http.Request

HTTPRequest returns the original *http.Request.

func (*Request) Host

func (r *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 (r *Request) IP() string

IP of the request.

func (*Request) Is

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

Is compares the given method with the request HTTP method.

func (*Request) IsDelete

func (r *Request) IsDelete() bool

IsDelete checks if its a HTTP DELETE method.

func (*Request) IsGet

func (r *Request) IsGet() bool

IsGet checks if its a HTTP GET method.

func (*Request) IsPatch

func (r *Request) IsPatch() bool

IsPatch checks if its a HTTP PATCH method.

func (*Request) IsPost

func (r *Request) IsPost() bool

IsPost checks if it is a HTTP POST method.

func (*Request) IsPut

func (r *Request) IsPut() bool

IsPut checks if its a HTTP PUT method.

func (*Request) IsSecure

func (r *Request) IsSecure() bool

IsSecure checks if it is a HTTPS request.

func (*Request) JWTClaim

func (r *Request) JWTClaim() interface{}

JWTClaim is a helper to return the claim. The claim will be checked by the request context with the key "JWT". Nil will return if it was not set.

func (*Request) Locale

func (r *Request) Locale() translation.Locale

Locale is used to translate message ids in the controller.

func (*Request) Method

func (r *Request) Method() string

Method returns the HTTP method in uppercase.

func (*Request) Param

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

Param returns a parameter by key. 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 (r *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 (r *Request) Pattern() string

Pattern returns the router url pattern. The pattern will be checked by the request context with the key "router_pattern". If the pattern is not set, an empty string will return.

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

func (*Request) Port

func (r *Request) Port() int

Port will return. If empty, 80 will be set as default.

func (*Request) Protocol

func (r *Request) Protocol() string

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

func (*Request) Proxy

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

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

func (*Request) Referer

func (r *Request) Referer() string

Referer returns the Referer Header.

func (*Request) Scheme

func (r *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 (r *Request) SetBody(body []byte)

SetBody for manipulations.

func (*Request) Site

func (r *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) URI

func (r *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 (r *Request) URL() string

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

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

type Response

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

Response struct.

func (*Response) Error

func (w *Response) Error(code int, err error, renderType string) error

Error will render the error message by the given render type. An error will return if the render provider does not exist or the renders error function returns one.

func (*Response) Render

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

Render will render the content by the given render type. An error will return if the render provider does not exist or the renders write function returns one.

func (*Response) ResetValues

func (w *Response) ResetValues()

ResetValues will create a new data instance.

func (*Response) SetValue

func (w *Response) SetValue(key string, value interface{})

SetValue as key/value pair.

func (*Response) Value

func (w *Response) Value(key string) interface{}

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

func (*Response) Values

func (w *Response) Values() map[string]interface{}

Values return all defined values.

func (*Response) Writer

func (w *Response) Writer() http.ResponseWriter

Writer returns the *http.ResponseWriter.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳