util

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2020 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ResultsPage  = "Results-Page"
	ResultsLimit = "Results-Limit"
	ResultsTotal = "Results-Total"
)

Constants for HTTP headers to support pagination.

Variables

View Source
var Validate *validator.Validate

Validate exposes the validator in the util package.

Functions

func ClientIP

func ClientIP(r *http.Request) string

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

func HTTPGetSkipVerify(url string) (*http.Response, error)

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 InitConfig

func InitConfig()

InitConfig initializes the config system.

func JSONResponse

func JSONResponse(w http.ResponseWriter, d interface{}, statusCode int)

JSONResponse encodes a JSON Response object.

func RandInt64

func RandInt64(max int64) int64

RandInt64 returns a cryptographically secure random number within the bounds 0, max.

func RandUint32

func RandUint32() uint32

RandUint32 generates a cryptographically secure integer

func RandomAlphaString

func RandomAlphaString(length int) string

RandomAlphaString returns a random alphanumeric string consisting of `length` characters. Note the shared math/Rand source should be seeded.

func Retry

func Retry(attempts int, sleep time.Duration, f func() error) error

Retry attempts a function `attempts` number of times. It exponentially backs off the initial `sleep` upon failures. Credit to Nick Stogner from a May 2017 post.

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

func NewAPIParams(request *http.Request, allowed StringSet) (*APIParams, error)

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

func (params *APIParams) GetOffsetSQL() int64

GetOffsetSQL calculates the SQL offset from the page and limit parameters.

func (*APIParams) GetOrderBySQLStatement

func (params *APIParams) GetOrderBySQLStatement(fieldMap map[string]string) string

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 Config

type Config = *viper.Viper

Config is an alias for the config package.

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.

func (RetryStop) Error

func (stop RetryStop) Error() string

type StringSet

type StringSet map[string]struct{}

StringSet : a Set for strings

func NewStringSet

func NewStringSet(vals ...string) StringSet

NewStringSet creates a new StringSet from a number of passed string keys.

func (StringSet) Add

func (set StringSet) Add(key string) StringSet

Add puts a value into the set.

func (StringSet) Copy

func (set StringSet) Copy() StringSet

Copy returns a copy of a set.

func (StringSet) Delete

func (set StringSet) Delete(key string) StringSet

Delete removes a value from the set.

func (StringSet) Has

func (set StringSet) Has(key string) bool

Has tests the existence of `key` in the set.

func (StringSet) Values

func (set StringSet) Values() []string

Values returns the contents of the set.

Jump to

Keyboard shortcuts

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