Documentation
¶
Index ¶
- Constants
- func DeleteEmptyAndTrim(s []string) []string
- func GenUUID() string
- func GetAsJSON(input interface{}) (output string, err error)
- func GetGenericError(confKey string, confItemType reflect.Type) error
- func GetGenericParseError(confKey string, confValue interface{}, confItemType reflect.Type) error
- func LoadFromJSONFile(filename string, target interface{}) (err error)
- func MarshalSettings(settings map[string]string, config interface{}, strict bool) (err error)
- func NowDate() string
- func ProcessCORS(writer http.ResponseWriter, req *http.Request, fn HTTPFunc)
- func ValidUUID(u string) bool
- func WriteJSONHTTPResponse(w http.ResponseWriter, req *http.Request, statusCode int, content interface{}, ...)
- func WriteStringHTTPResponse(w http.ResponseWriter, req *http.Request, statusCode int, content interface{}, ...)
- func WriteToJSONFile(filename string, target interface{}, perm os.FileMode) (err error)
- type HTTPFunc
- type HTTPResponse
- func (response HTTPResponse) GetAsString() string
- func (response *HTTPResponse) SetHTTPResponseFields(statusCode int, content interface{}, contentType string, error bool)
- func (response *HTTPResponse) SetJSONHTTPResponseFields(statusCode int, content interface{}, error bool)
- func (response *HTTPResponse) SetStringHTTPResponseFields(statusCode int, content interface{}, error bool)
- func (response *HTTPResponse) WriteHTTPResponse(writer http.ResponseWriter, req *http.Request)
Constants ¶
const AllowedHeaders = "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization, X-CSRF-Token"
AllowedHeaders is the set of headers we will process in the ProcessCORS function.
const HTTPResponseTypeJSON = "json"
HTTPResponseTypeJSON is a const that allows for efficiently setting HTTPResponse structs' ContentType fields to json
const HTTPResponseTypeString = "string"
HTTPResponseTypeString is a const that allows for efficiently setting HTTPResponse structs' ContentType fields to string
Variables ¶
This section is empty.
Functions ¶
func DeleteEmptyAndTrim ¶
DeleteEmptyAndTrim takes an input string slice and trims whitespace surrounding each entry in the slice
func GetGenericError ¶
GetGenericError helps keep the ProcessApplicationSettings slim by enabling re-use of a common error format that should be returned to the user when the user fails to specify a configuration item.
func GetGenericParseError ¶
GetGenericParseError helps keep the ProcessApplicationSettings slim by enabling re-use of a common error format that should be returned to the user when the user fails to specify a parseable configuration item.
func LoadFromJSONFile ¶
LoadFromJSONFile takes an input filename and loads from JSON into any interface Make sure that the "target" parameter is used like this err := LoadFromJSONFile(filename, &target)
func MarshalSettings ¶
MarshalSettings converts a map[string]string into a properly marshaled config interface{}. This is useful for converting an EdgeX Application Settings or Device Service Driver config map into a proper struct. Since values in these maps are strings, some assumptions are made about these values when marshaling them into their proper values. For example, a config interface that contains a field with type []string will assume that the corresponding field name's key in the map is a CSV string. This function does not cover every possible type in Go, so there are bound to be some issues converting. It contains the most common types. If the strict boolean is set to true, this function will error out when a value in the input map is not specified. Note that the config parameter must be a pointer to an interface. All fields in the config interface should be exported. The reflect module does not have the ability to modify unexported fields. Any unexported fields will simply be ignored.
func ProcessCORS ¶
func ProcessCORS(writer http.ResponseWriter, req *http.Request, fn HTTPFunc)
ProcessCORS is a decorator function that enables CORS preflight responses and sets CORS headers. Usage:
func GetSomething(writer http.ResponseWriter, req *http.Request) { helpers.ProcessCORS(writer, req, func(writer http.ResponseWriter, req *http.Request) { // do some logic with writer and req }) }
func WriteJSONHTTPResponse ¶
func WriteJSONHTTPResponse(w http.ResponseWriter, req *http.Request, statusCode int, content interface{}, error bool)
WriteJSONHTTPResponse is a one-liner meant to quickly build out a JSON HTTPResponse, and then respond according to the given inputs
func WriteStringHTTPResponse ¶
func WriteStringHTTPResponse(w http.ResponseWriter, req *http.Request, statusCode int, content interface{}, error bool)
WriteStringHTTPResponse is a one-liner meant to quickly build out a string HTTPResponse, and then respond according to the given inputs
Types ¶
type HTTPFunc ¶
type HTTPFunc func(http.ResponseWriter, *http.Request)
HTTPFunc is a function type that matches what is typically passed into an HTTP API endpoint
type HTTPResponse ¶
type HTTPResponse struct { Content interface{} `json:"content"` ContentType string `json:"contentType"` StatusCode int `json:"statusCode"` Error bool `json:"error"` }
HTTPResponse All API HTTP responses should follow this format.
func GetHTTPResponseTemplate ¶
func GetHTTPResponseTemplate() HTTPResponse
GetHTTPResponseTemplate gets a template HTTPResponse. You can use this as a starting point for all HTTP responses
func ParseJSONHTTPResponseContent ¶
func ParseJSONHTTPResponseContent(respBody io.ReadCloser, outputJSONContent interface{}) (response HTTPResponse, err error)
ParseJSONHTTPResponseContent converts a raw http.Response (i.e. resp.Body) into a fully unmarshaled interface, including the content interface
func (HTTPResponse) GetAsString ¶
func (response HTTPResponse) GetAsString() string
GetAsString gets an HTTPResponse as a JSON string. If the JSON marshal fails, it will return a template error HTTPResponse
func (*HTTPResponse) SetHTTPResponseFields ¶
func (response *HTTPResponse) SetHTTPResponseFields(statusCode int, content interface{}, contentType string, error bool)
SetHTTPResponseFields sets the four HTTPResponse fields in a single line.
func (*HTTPResponse) SetJSONHTTPResponseFields ¶
func (response *HTTPResponse) SetJSONHTTPResponseFields(statusCode int, content interface{}, error bool)
SetJSONHTTPResponseFields sets the three common HTTPResponse fields, and the ContentType to JSON
func (*HTTPResponse) SetStringHTTPResponseFields ¶
func (response *HTTPResponse) SetStringHTTPResponseFields(statusCode int, content interface{}, error bool)
SetStringHTTPResponseFields sets the three common HTTPResponse fields, and the ContentType to string
func (*HTTPResponse) WriteHTTPResponse ¶
func (response *HTTPResponse) WriteHTTPResponse(writer http.ResponseWriter, req *http.Request)
WriteHTTPResponse is a helpful shorthand for writing out a prepared HTTPResponse