Documentation ¶
Index ¶
- Constants
- func AllowedMethods(handler http.HandlerFunc, methods string) http.HandlerFunc
- func ContentResponse(w http.ResponseWriter, contentType string, response []byte)
- func DoJSONRequest(method, url string, requestBody, responseBody interface{}) (*http.Response, error)
- func DownloadFile(url, filename string) (ok bool, err error)
- func EnvironMap() map[string]string
- func FileExists(filename string) bool
- func FloatSearch(target float64, array []float64) bool
- func GenerateRandInt(max int) (returnValue int, returnError error)
- func GetDefaultFromEnv(key, fallback string) string
- func GetEnv(key, fallback string) string
- func GetEnvSecret(secretName string) (secret string)
- func HashFile(algorithm string, filepath string) (value string, err error)
- func IntSearch(target int, array []int) bool
- func JSONMarshalResponse(w http.ResponseWriter, body interface{})
- func JSONResponse(w http.ResponseWriter, response []byte)
- func ParseYamlOrJSON(fileName string, outputInterface interface{}) error
- func SkipRoot(jsonBlob []byte) (values json.RawMessage)
- func SkipRootWithError(jsonBlob []byte) (json.RawMessage, error)
- func StringResponse(w http.ResponseWriter, response string)
- func StringSearch(target string, array []string) bool
Constants ¶
const JSONApplicationType = "application/json; charset=utf-8"
JSONApplicationType is MIME type for JSON data.
Variables ¶
This section is empty.
Functions ¶
func AllowedMethods ¶
func AllowedMethods(handler http.HandlerFunc, methods string) http.HandlerFunc
AllowedMethods is a decorator to only allow comma separated methods.
func ContentResponse ¶
func ContentResponse(w http.ResponseWriter, contentType string, response []byte)
ContentResponse writes a http response with a given content type.
func DoJSONRequest ¶
func DoJSONRequest(method, url string, requestBody, responseBody interface{}) (*http.Response, error)
DoJSONRequest sends a client JSON request. The responseBody should be a pointer to the address of a struct. If a blank string is passed then it will default to a POST request. Example:
var response exampleStruct
resp, err := DoJSONRequest("POST", "http://example.com", nil, &response)
or
response := new(exampleStruct)
resp, err := DoJSONRequest("POST", "http://example.com", nil, response).
func DownloadFile ¶
DownloadFile downloads a file and writes it to the file path. Overwrites any file at the path.
func EnvironMap ¶
EnvironMap returns a string map of environment variables.
func FileExists ¶
FileExists is a function to check if the file exists at the path.
func FloatSearch ¶
FloatSearch checks an array of float64 to see if the target float is in it.
func GenerateRandInt ¶
GenerateRandInt securely generate a random int64. The input is the maximum value that the random int can be.
func GetDefaultFromEnv ¶ added in v5.2.0
GetDefaultFromEnv checks if the key exists in the environment variables. If yes then returns that value and if not returns default value.
func GetEnv ¶
GetEnv checks if the key exists in the environment variables. If yes then returns that value and if not returns default value.
func GetEnvSecret ¶
GetEnvSecret will get either an OS environment variable. If there is no environment variable set it will check to see if a variable with _FILE is set. If so then it will read the secret name as a filepath and return the content.
func HashFile ¶
HashFile generates a string hash of the given file path. Supported hashing algorithm: sha256, sha384, and sha512.
func JSONMarshalResponse ¶
func JSONMarshalResponse(w http.ResponseWriter, body interface{})
JSONMarshalResponse writes a http response as JSON. Takes an interface.
func JSONResponse ¶
func JSONResponse(w http.ResponseWriter, response []byte)
JSONResponse writes a http response as JSON. Takes a byte array as input.
func ParseYamlOrJSON ¶
ParseYamlOrJSON will detect if a file is either a JSON or YAML file and marshal it to the provided interface. Yaml files can end in .yml or .yaml
Example:
var response exampleStruct
if err := ParseYamlOrJSON("helloworld.json", &response); err != nil { log.Fatal(err)}
OR ¶
response := new(exampleStruct)
err := ParseYamlOrJSON("helloworld.yml" response); err != nil { log.Fatal(err)}.
func SkipRoot ¶
func SkipRoot(jsonBlob []byte) (values json.RawMessage)
SkipRoot skips the root struct of a JSON message but will return nil if an error happens. Taken from https://stackoverflow.com/a/20873511.
func SkipRootWithError ¶
func SkipRootWithError(jsonBlob []byte) (json.RawMessage, error)
SkipRootWithError skips the root struct of a JSON message but will return an error. Taken from https://stackoverflow.com/a/20873511.
func StringResponse ¶
func StringResponse(w http.ResponseWriter, response string)
StringResponse writes a http response as a string.
func StringSearch ¶
StringSearch checks an array of strings to see if the target string is in it.
Types ¶
This section is empty.