Documentation ¶
Index ¶
- Constants
- Variables
- func ClientIP(r *http.Request) string
- func HTTPGetSkipVerify(url string) (*http.Response, error)
- func InitConfig()
- func JSONResponse(w http.ResponseWriter, d interface{}, statusCode int)
- func RandInt64(max int64) int64
- func RandUint32() uint32
- func RandomAlphaString(length int) string
- func Retry(attempts int, sleep time.Duration, f func() error) error
- type APIParams
- type Config
- type HTTPResponse
- type RetryStop
- type StringSet
Constants ¶
const ( ResultsPage = "Results-Page" ResultsLimit = "Results-Limit" ResultsTotal = "Results-Total" )
Constants for HTTP headers to support pagination.
Variables ¶
var Validate *validator.Validate
Validate exposes the validator in the util package.
Functions ¶
func ClientIP ¶
ClientIP implements a best effort algorithm to return the real client IP, it parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy. Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.
func HTTPGetSkipVerify ¶
HTTPGetSkipVerify performs an HTTP GET request, skipping the default client's verification of the server's certificate chain and host name. Safe only for localhost/testing as per (CWE-295): TLS InsecureSkipVerify set true.
#nosec
func JSONResponse ¶
func JSONResponse(w http.ResponseWriter, d interface{}, statusCode int)
JSONResponse encodes a JSON Response object.
func RandInt64 ¶
RandInt64 returns a cryptographically secure random number within the bounds 0, max.
func RandomAlphaString ¶
RandomAlphaString returns a random alphanumeric string consisting of `length` characters. Note the shared math/Rand source should be seeded.
Types ¶
type APIParams ¶
type APIParams struct { Ordering []string OrderDirection string AndFilters map[string]interface{} Limit int64 Page int64 CreatedBefore time.Time CreatedAfter time.Time Deep bool }
APIParams represents common API parameters.
func DefaultAPIParams ¶
func DefaultAPIParams() *APIParams
DefaultAPIParams returns default APIParams.
func NewAPIParams ¶
NewAPIParams constructs a params object from an `http.Request`. If the optional allowed params set is passed, arguments are checked against those.
func (*APIParams) GetOffsetSQL ¶
GetOffsetSQL calculates the SQL offset from the page and limit parameters.
func (*APIParams) GetOrderBySQLStatement ¶
GetOrderBySQLStatement returns an SQL statement for ordering.
func (*APIParams) WritePaginationHeaders ¶
func (params *APIParams) WritePaginationHeaders(w http.ResponseWriter, total int64)
WritePaginationHeaders adds pagination headers to a http response writer.
type HTTPResponse ¶
type HTTPResponse struct { Status string `json:"status"` Value interface{} `json:"value,omitempty"` Error string `json:"error,omitempty"` }
HTTPResponse defines a common JSON response.
type RetryStop ¶
type RetryStop struct {
Err error
}
RetryStop : return this error from your Retry-ing function to abort future attempts.
type StringSet ¶
type StringSet map[string]struct{}
StringSet : a Set for strings
func NewStringSet ¶
NewStringSet creates a new StringSet from a number of passed string keys.