Documentation
¶
Overview ¶
Package webutils contains commonly used web utilities.
Index ¶
- func AccessLogHandler(h http.Handler, logger *logging.Logger) http.Handler
- func DumpDBHandler(w http.ResponseWriter, r *http.Request, db *bolt.DB)
- func GetHost(r *http.Request) string
- func GetIPs(r *http.Request) string
- func IPFromRemoteAddr(s string) string
- func ReadRequestBody(w http.ResponseWriter, r *http.Request, v interface{}) error
- type FormErrors
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AccessLogHandler ¶
AccessLogHandler logs HTTP requests that HTTP sever handles. It logs information about remote address, X-Forwarded-For or X-Real-Ip, HTTP method, request URI, HTTP protocol, HTTP response status, total bytes written to http.ResponseWriter, response duration, HTTP referrer and HTTP client user agent.
func DumpDBHandler ¶
DumpDBHandler writes all data from BoltDB database to a http.ResponseWriter.
func IPFromRemoteAddr ¶
IPFromRemoteAddr returns an IP without port from request's remote address.
func ReadRequestBody ¶
func ReadRequestBody(w http.ResponseWriter, r *http.Request, v interface{}) error
ReadRequestBody unmarshals JSON encoded HTTP request body into an arbitrary interface. In case of error, it writes appropriate JSON-encoded response to http.ResponseWriter, so the calling handler should not write new data if this function returns error.
Types ¶
type FormErrors ¶
type FormErrors struct {
Errors []string `json:"errors"`
FieldErrors map[string][]string `json:"field-errors"`
}
FormErrors represent structure errors returned by API server to request based on HTML form data.
func NewError ¶
func NewError(e string) FormErrors
NewError initializes FormErrors with one general error.
func NewFieldError ¶
func NewFieldError(field, e string) FormErrors
NewFieldError initializes FormErrors with one field error.
func (*FormErrors) AddError ¶
func (f *FormErrors) AddError(e string)
AddError appends an error to a list of general errors.
func (*FormErrors) AddFieldError ¶
func (f *FormErrors) AddFieldError(field, e string)
AddFieldError appends an error to a list of field specific errors.
func (FormErrors) HasErrors ¶
func (f FormErrors) HasErrors() bool
HasErrors returns weather FormErrors instance contains at leas one error.