httphelpers

package module
v0.0.0-...-a253d83 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

README

http-helpers

Some small, but often used http functions

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleIDRequest

func HandleIDRequest(w http.ResponseWriter, r *http.Request, f func(id int, w http.ResponseWriter, r *http.Request))

HandleIDRequest extracts the ID from the request and passes it to the handler function `f`

func ServeCORS

func ServeCORS(w http.ResponseWriter, r *http.Request, m *mux.Router)

ServeCORS enables all verbs when an OPTIONS is received. In other cases dispatches the request to Multiplexer

func SetContentTypeForExtension

func SetContentTypeForExtension(w http.ResponseWriter, page string)

SetContentTypeForExtension sets the Content-Type header for the page based on the extension

func TimestampToString

func TimestampToString(ts int) string

TimestampToString takes the Unix timestamp integer and formats it into a filename friendly string

func WriteBytes

func WriteBytes(w http.ResponseWriter, b []byte) (int, error)

WriteBytes writes the slice `b` of bytes, handy for sending unmarshalled JSON. Returns the number of bytes written and an error if one happened.

func WriteCSVData

func WriteCSVData(w http.ResponseWriter, key []byte, d [][]string) error

WriteCSVData writes the `d` arrays in CSV form as an attachment file named `key`.csv

func WriteCloseResponse

func WriteCloseResponse(err error, w http.ResponseWriter)

WriteCloseResponse helper function as it was repeatedly being copy pasted.

func WriteErrorResponse

func WriteErrorResponse(w http.ResponseWriter, e error) (int, error)

WriteErrorResponse writes a simple JSON error, we do not return a server error because the Server is working correctly - some other issue has arrisen

func WriteHeader

func WriteHeader(w http.ResponseWriter, status ...int)

WriteHeader writes a defacto JSON CORs header, passing a `status value is optional`

func WriteJSONResponse

func WriteJSONResponse(o interface{}, w http.ResponseWriter) error

WriteJSONResponse attempts to Marshal and write the object `o`

func WriteOKResponse

func WriteOKResponse(w http.ResponseWriter)

WriteOKResponse for when everything went just fine - like a PUT.

func WriteRESTErrorResponse

func WriteRESTErrorResponse(w http.ResponseWriter, e error) (int, error)

WriteRESTErrorResponse provides a "proper" error status

func WriteZippedBytes

func WriteZippedBytes(w http.ResponseWriter, b []byte) (int, error)

WriteZippedBytes zips up the `b` array and writes it as a response Returns number of bytes written, and an error if any occured

Types

This section is empty.

Jump to

Keyboard shortcuts

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