web

package
v0.0.0-...-fd23dd1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2019 License: OSL-3.0 Imports: 12 Imported by: 72

Documentation

Index

Examples

Constants

View Source
const (
	// INFO notice
	INFO = "info"
	// WARNING notice
	WARNING = "warning"
	// ERROR notice
	ERROR = "error"
)

todo: still necessary?

Variables

View Source
var (
	// ErrFormNotFound is triggered for missing form values
	ErrFormNotFound = errors.New("form value not found")
	// ErrParamNotFound for missing router params
	ErrParamNotFound = errors.New("param value not found")
	// ErrQueryNotFound for missing query params
	ErrQueryNotFound = errors.New("query value not found")
)

Functions

func Context_

func Context_(ctx context.Context, session *Request) context.Context

Context_ saves the Session in the context

func NoCache

func NoCache(c context.Context, rw http.ResponseWriter)

NoCache is a response hook to enforce no cache headers

func URLTitle

func URLTitle(title string) string

URLTitle normalizes a title for nice usage in URLs

Example

Example usage of the URLTitle helper function

fmt.Println(URLTitle("test/a 123 name % / _ - _ test"))
Output:

test_a-123-name-test

Types

type BasicResponse

type BasicResponse struct {
	Status int
	// contains filtered or unexported fields
}

BasicResponse defines a response with basic attributes

func (*BasicResponse) Apply

Apply sets status and content size of the response

func (*BasicResponse) GetContentLength

func (br *BasicResponse) GetContentLength() int

GetContentLength returns the content size of the response

func (*BasicResponse) GetStatus

func (br *BasicResponse) GetStatus() int

GetStatus returns the status of the response

func (*BasicResponse) Hook

func (br *BasicResponse) Hook(hooks ...ResponseHook) Response

Hook appends hooks to the response

func (*BasicResponse) OnResponse

func (br *BasicResponse) OnResponse(c context.Context, r *Request, rw http.ResponseWriter)

OnResponse callback to apply hooks

type ContentResponse

type ContentResponse struct {
	BasicResponse
	Body        io.Reader
	ContentType string
}

ContentResponse contains a response with body

func (*ContentResponse) Apply

Apply ContentResponse

func (*ContentResponse) Hook

func (cr *ContentResponse) Hook(hooks ...ResponseHook) Response

Hook appends hooks to the response

type DataResponse

type DataResponse struct {
	HTTPResponse
	Data interface{}
}

DataResponse returns a response containing data, e.g. as JSON

func (*DataResponse) Apply

Apply response

func (*DataResponse) Hook

func (r *DataResponse) Hook(hooks ...ResponseHook) Response

Hook helper deprecated: to be removed

type ErrorResponse

type ErrorResponse struct {
	Response
	Error error
}

ErrorResponse wraps a response with an error

type GetPartialDataFunc

type GetPartialDataFunc struct{}

func (*GetPartialDataFunc) Func

func (*GetPartialDataFunc) Func(c context.Context) interface{}

type HTTPResponse

type HTTPResponse struct {
	Status uint
	Body   io.Reader
	Header http.Header
	// contains filtered or unexported fields
}

HTTPResponse contains a status and a body

func (*HTTPResponse) Apply

Apply response

func (HTTPResponse) GetContentLength

func (HTTPResponse) GetContentLength() int

GetContentLength returns the bodies content length

func (*HTTPResponse) GetStatus

func (r *HTTPResponse) GetStatus() int

GetStatus returns the HTTP status

func (*HTTPResponse) Hook

func (r *HTTPResponse) Hook(hooks ...ResponseHook) Response

Hook helper deprecated: to be removed

type JSONResponse

type JSONResponse struct {
	BasicResponse
	Data interface{}
}

JSONResponse returns Data encoded as JSON todo: create a generic data response instead

func (*JSONResponse) Apply

Apply JSONResponse

func (*JSONResponse) Hook

func (jr *JSONResponse) Hook(hooks ...ResponseHook) Response

Hook appends hooks to the response

type OnResponse

type OnResponse interface {
	OnResponse(context.Context, *Request, http.ResponseWriter)
}

OnResponse hook

type Redirect

type Redirect interface {
	Response
	With(key string, data interface{}) Redirect
}

Redirect response with the ability to add data

type RedirectResponse

type RedirectResponse struct {
	BasicResponse
	Location string
	// contains filtered or unexported fields
}

RedirectResponse redirect

func (*RedirectResponse) Apply

Apply Response

func (*RedirectResponse) Hook

func (rr *RedirectResponse) Hook(hooks ...ResponseHook) Response

Hook appends hooks to the response

func (*RedirectResponse) OnResponse

func (rr *RedirectResponse) OnResponse(c context.Context, r *Request, rw http.ResponseWriter)

OnResponse Hook

func (*RedirectResponse) With

func (rr *RedirectResponse) With(key string, data interface{}) Redirect

With adds data to the web response

type RenderResponse

type RenderResponse struct {
	DataResponse
	Template string
	// contains filtered or unexported fields
}

RenderResponse renders data

func (*RenderResponse) Apply

Apply response

func (*RenderResponse) Hook

func (r *RenderResponse) Hook(hooks ...ResponseHook) Response

Hook helper deprecated: to be removed

type Request

type Request struct {
	Values *sync.Map
	// contains filtered or unexported fields
}

Request defines a web request

func FromContext

func FromContext(ctx context.Context) (*Request, bool)

FromContext retrieves the Request from the context

func RequestFromRequest

func RequestFromRequest(r *http.Request, session *Session) *Request

RequestFromRequest wraps a http Request

func (*Request) Form

func (r *Request) Form(n string) ([]string, bool)

Form get POST value

func (*Request) Form1

func (r *Request) Form1(n string) (string, bool)

Form1 get first POST value

func (*Request) FormAll

func (r *Request) FormAll() map[string][]string

FormAll get all POST values

func (*Request) LoadParams

func (r *Request) LoadParams(p map[string]string) *Request

LoadParams load request params

func (*Request) MustForm

func (r *Request) MustForm(n string) []string

MustForm panics if n is not found

func (*Request) MustForm1

func (r *Request) MustForm1(n string) string

MustForm1 panics if n is not found

func (*Request) MustParam1

func (r *Request) MustParam1(n string) string

MustParam1 panics if n is not found

func (*Request) MustQuery

func (r *Request) MustQuery(n string) []string

MustQuery panics if n is not found

func (*Request) MustQuery1

func (r *Request) MustQuery1(n string) string

MustQuery1 panics if n is not found

func (*Request) Param1

func (r *Request) Param1(n string) (string, bool)

Param1 get first querystring param

func (*Request) ParamAll

func (r *Request) ParamAll() map[string]string

ParamAll get all querystring params

func (*Request) Query

func (r *Request) Query(n string) ([]string, bool)

Query looks up Raw Query map for Param

func (*Request) Query1

func (r *Request) Query1(n string) (string, bool)

Query1 looks up Raw Query map for First Param

func (*Request) QueryAll

func (r *Request) QueryAll() map[string][]string

QueryAll returns a Map of the Raw Query

func (*Request) RemoteAddress

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

RemoteAddress get the requests real remote address

func (*Request) Request

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

Request get the requests request

func (*Request) Session

func (r *Request) Session() *Session

Session returns the ctx Session

func (*Request) WithVars

func (r *Request) WithVars(vars map[string]string) *Request

WithVars loads parameters

type Responder

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

Responder generates responses

func (*Responder) Data

func (r *Responder) Data(data interface{}) *DataResponse

Data returns a data response which can be serialized

func (*Responder) Forbidden

func (r *Responder) Forbidden(err error) *ServerErrorResponse

Forbidden creates a 403 error response

func (*Responder) HTTP

func (r *Responder) HTTP(status uint, body io.Reader) *HTTPResponse

HTTP Response generator

func (*Responder) Inject

func (r *Responder) Inject(engine template.Engine, router ReverseRouter, cfg *struct {
	TemplateForbidden     string `inject:"config:flamingo.template.err403"`
	TemplateNotFound      string `inject:"config:flamingo.template.err404"`
	TemplateUnavailable   string `inject:"config:flamingo.template.err503"`
	TemplateErrorWithCode string `inject:"config:flamingo.template.errWithCode"`
}) *Responder

Inject Responder dependencies

func (*Responder) NotFound

func (r *Responder) NotFound(err error) *ServerErrorResponse

NotFound creates a 404 error response

func (*Responder) Render

func (r *Responder) Render(tpl string, data interface{}) *RenderResponse

Render creates a render response, with the supplied template and data

func (*Responder) RouteRedirect

func (r *Responder) RouteRedirect(to string, data map[string]string) *RouteRedirectResponse

RouteRedirect generator

func (*Responder) ServerError

func (r *Responder) ServerError(err error) *ServerErrorResponse

ServerError creates a 500 error response

func (*Responder) ServerErrorWithCodeAndTemplate

func (r *Responder) ServerErrorWithCodeAndTemplate(err error, tpl string, status uint) *ServerErrorResponse

ServerErrorWithCodeAndTemplate error response with template and http status code

func (*Responder) TODO

func (r *Responder) TODO() *HTTPResponse

TODO creates a 501 Not Implemented response

func (*Responder) URLRedirect

func (r *Responder) URLRedirect(url *url.URL) *URLRedirectResponse

URLRedirect returns a 303 redirect to a given URL

func (*Responder) Unavailable

func (r *Responder) Unavailable(err error) *ServerErrorResponse

Unavailable creates a 503 error response

type Response

type Response interface {
	// Apply executes the response on the http.ResponseWriter
	Apply(context.Context, http.ResponseWriter) error
	GetStatus() int
	GetContentLength() int
	Hook(...ResponseHook) Response
}

Response defines the generic web response

type ResponseHook

type ResponseHook func(c context.Context, rw http.ResponseWriter)

func AddHeader

func AddHeader(k, v string) ResponseHook

AddHeader adds headers in a response hook

type ReverseRouter

type ReverseRouter interface {
	URL(name string, params map[string]string) *url.URL
}

ReverseRouter for RouteRedirect Responses

type RouteRedirectResponse

type RouteRedirectResponse struct {
	HTTPResponse
	To   string
	Data map[string]string
	// contains filtered or unexported fields
}

RouteRedirectResponse redirects to a certain route

func (*RouteRedirectResponse) Apply

Apply response

func (*RouteRedirectResponse) Hook

func (r *RouteRedirectResponse) Hook(hooks ...ResponseHook) Response

Hook helper deprecated: to be removed

type ServeHTTPResponse

type ServeHTTPResponse struct {
	*VerboseResponseWriter
	BasicResponse
}

ServeHTTPResponse wraps the original response with a VerboseResponseWriter

func (*ServeHTTPResponse) Apply

Apply Response (empty, it has already been applied)

func (*ServeHTTPResponse) Hook

func (shr *ServeHTTPResponse) Hook(hooks ...ResponseHook) Response

Hook appends hooks to the response

type ServerErrorResponse

type ServerErrorResponse struct {
	RenderResponse
	Error error
}

ServerErrorResponse returns a server error, by default http 500

func (*ServerErrorResponse) Apply

Apply response

func (*ServerErrorResponse) Hook

func (r *ServerErrorResponse) Hook(hooks ...ResponseHook) Response

Hook helper deprecated: to be removed

type Session

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

func NewSession

func NewSession(s *sessions.Session) *Session

NewSession wraps a gorilla session

func (*Session) AddFlash

func (s *Session) AddFlash(value interface{}, vars ...string)

AddFlash adds a flash message to the session.

A single variadic argument is accepted, and it is optional: it defines the flash key. If not defined "_flash" is used by default.

func (*Session) Delete

func (s *Session) Delete(key interface{})

Delete a given key from the session

func (*Session) Flashes

func (s *Session) Flashes(vars ...string) []interface{}

Flashes returns a slice of flash messages from the session.

A single variadic argument is accepted, and it is optional: it defines the flash key. If not defined "_flash" is used by default.

func (*Session) G

func (s *Session) G() *sessions.Session

G access gorilla subsession deprecated: kept for backwards compatibility

func (*Session) ID

func (s *Session) ID() (id string)

ID returns the Session id

func (*Session) Load

func (s *Session) Load(key interface{}) (data interface{}, ok bool)

Load data by a key

func (*Session) Store

func (s *Session) Store(key interface{}, data interface{})

Store data with a key in the Session

func (*Session) Try

func (s *Session) Try(key interface{}) (data interface{})

Load data by a key

type SetPartialDataFunc

type SetPartialDataFunc struct{}

func (*SetPartialDataFunc) Func

func (*SetPartialDataFunc) Func(c context.Context) interface{}

type URLRedirectResponse

type URLRedirectResponse struct {
	HTTPResponse
	URL *url.URL
}

URLRedirectResponse redirects to a certain URL

func (*URLRedirectResponse) Apply

Apply response

func (*URLRedirectResponse) Hook

func (r *URLRedirectResponse) Hook(hooks ...ResponseHook) Response

Hook helper deprecated: to be removed

type VerboseResponseWriter

type VerboseResponseWriter struct {
	http.ResponseWriter
	Status int
	Size   int
}

VerboseResponseWriter shadows http.ResponseWriter and tracks written bytes and result Status for logging.

func (*VerboseResponseWriter) Write

func (response *VerboseResponseWriter) Write(data []byte) (int, error)

Write calls http.ResponseWriter.Write and records the written bytes.

func (*VerboseResponseWriter) WriteHeader

func (response *VerboseResponseWriter) WriteHeader(h int)

WriteHeader calls http.ResponseWriter.WriteHeader and records the Status code.

Directories

Path Synopsis
Package responder are basically helper for generating responses.
Package responder are basically helper for generating responses.
mocks
Code generated by mockery v1.0.0 Code generated by mockery v1.0.0 Code generated by mockery v1.0.0 Code generated by mockery v1.0.0
Code generated by mockery v1.0.0 Code generated by mockery v1.0.0 Code generated by mockery v1.0.0 Code generated by mockery v1.0.0

Jump to

Keyboard shortcuts

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