Documentation ¶
Index ¶
- Constants
- func AddCORSMiddlewareAndEndpoint(router *mux.Router, requesterIdHeader string)
- func AddLoggingMiddleware(router *mux.Router, traceIdHeader string, debug bool)
- func AddRequesterIdHeaderMiddleware(router *mux.Router, requesterIdHeader string, debug bool)
- func CheckJSONMarshalAndWrite(data interface{}, status int, w http.ResponseWriter) (err error)
- func CreateAndRunServerFromRouter(router *mux.Router, port string, timeoutTime time.Duration, debug bool) http.Server
- func PreProcessInput(inputPtr InputObject, maxBytes int, w http.ResponseWriter, r *http.Request, ...) error
- func PreProcessInputFromHeaders(inputPtr InputObject, maxBytes int, w http.ResponseWriter, r *http.Request) error
- func PreProcessInputFromJSON(inputPtr InputObject, maxBytes int, w http.ResponseWriter, r *http.Request) error
- func PreProcessInputFromJSONAPI(inputPtr InputObject, maxBytes int, w http.ResponseWriter, r *http.Request) error
- func SendErrorOnError(err error, status int, w http.ResponseWriter, r *http.Request, ...)
- func SetupAndRunMultipleServers(routers []*mux.Router, ports []string, debug bool, shutdown func())
- func SetupAndRunServer(router *mux.Router, port string, debug bool, shutdown func())
- func StandardAgnosticRequestHandler(inputPtr InputObject, maxBytes int, ...) (err error)
- func StandardJSONRequestHandler(inputPtr InputObject, maxBytes int, ...) (err error)
- func StandardRequestHandler(inputPtr InputObject, maxBytes int, ...) (err error)
- func UnmarshalObjectFromHeaders(inputPtr interface{}, r *http.Request) error
- func UnmarshalObjectFromJSON(inputPtr interface{}, r *http.Request) error
- func UnmarshalObjectFromJSONStrict(inputPtr interface{}, r *http.Request) error
- func WalkRouter(router *mux.Router)
- func WriteModelToResponseFromHeaders(dataToSend interface{}, status int, w http.ResponseWriter, r *http.Request) error
- func WriteModelToResponseJSON(dataToSend interface{}, status int, w http.ResponseWriter) (err error)
- func WriteModelToResponseJSONAPI(dataToSend interface{}, status int, w http.ResponseWriter) (err error)
- func WriteNoContentToResponse(dataToSend interface{}, status int, w http.ResponseWriter, r *http.Request) error
- type InputObject
Constants ¶
const AcceptContentTypeHeader = "Accept"
const ContentTypeHeader = "Content-Type"
const JSONContentType = "application/json"
Variables ¶
This section is empty.
Functions ¶
func AddCORSMiddlewareAndEndpoint ¶
This adds CORS headers for all requests (to use if web server is running locally)
func AddLoggingMiddleware ¶
func AddRequesterIdHeaderMiddleware ¶
Check if the requesterIdHeader exists and is valid and return 400 if not
func CheckJSONMarshalAndWrite ¶ added in v0.2.2
func CheckJSONMarshalAndWrite(data interface{}, status int, w http.ResponseWriter) (err error)
This function manually marshals the json payload, and checks to see if it marshals to valid json before writing the header and response We use this function in order to write a proper error status if the passed-in data does not marshal properly
If we do this:
w.WriteHeader(status) return json.NewEncoder(w).Encode(payload)
Then, if the `Encode` function errors, the status will already have been set and will not be set by any error function down the line, thus masking the status output from the server (For example, we write 200 status, then the function errors, and an error bubbles-up, but a 500 error code cannot be written because the 200 was already written)
Likewise if we do this:
if err = json.NewEncoder(w).Encode(payload); err == nil { w.WriteHeader(status) }
Then, only a 200 code is ever written and the WriteHeader call is ignored because data has already been written to the ResponseWriter
Thus we can use this `CheckJSONMarshalAndWrite` function to capture the error appropriately ¶
See golang ResponseWriter for how information on the `WriteHeader` works https://pkg.go.dev/net/http@go1.17.1#ResponseWriter
NOTE: The `w.Write` call can also fail (due to network isues for example ) in which case the server will not respond with the correct status code regardless (because it cannot write ANY status code) But we still capture the error to bubble it up and do any logging/handling on the server side
Info comes from: https://stackoverflow.com/questions/49483111/http-override-http-header-code-in-golang-while-there-is-an-error-in-json-encodin
func CreateAndRunServerFromRouter ¶ added in v0.2.4
func PreProcessInput ¶
func PreProcessInput(inputPtr InputObject, maxBytes int, w http.ResponseWriter, r *http.Request, unmarshalFn func(interface{}, *http.Request) error) error
Be sure to pass in pointer to InputObject Usage: PreProcessInput(&model, 500, w, r, fn)
func PreProcessInputFromHeaders ¶
func PreProcessInputFromHeaders(inputPtr InputObject, maxBytes int, w http.ResponseWriter, r *http.Request) error
func PreProcessInputFromJSON ¶
func PreProcessInputFromJSON(inputPtr InputObject, maxBytes int, w http.ResponseWriter, r *http.Request) error
func PreProcessInputFromJSONAPI ¶
func PreProcessInputFromJSONAPI(inputPtr InputObject, maxBytes int, w http.ResponseWriter, r *http.Request) error
func SendErrorOnError ¶
func SetupAndRunMultipleServers ¶ added in v0.2.4
func SetupAndRunServer ¶
func StandardAgnosticRequestHandler ¶
func StandardAgnosticRequestHandler( inputPtr InputObject, maxBytes int, logicFunc func(InputObject, *http.Request) (interface{}, int, error), w http.ResponseWriter, r *http.Request, logError func(error, *http.Request), ) (err error)
See notes on logic func in StandardRequestHandler-- must return a pointer to a struct or jsonapi library will not work right (json library doesn't care)
func StandardJSONRequestHandler ¶
func StandardJSONRequestHandler( inputPtr InputObject, maxBytes int, logicFunc func(InputObject, *http.Request) (interface{}, int, error), w http.ResponseWriter, r *http.Request, logError func(error, *http.Request), ) (err error)
func StandardRequestHandler ¶
func StandardRequestHandler( inputPtr InputObject, maxBytes int, preprocessFunc func(InputObject, int, http.ResponseWriter, *http.Request) error, logicFunc func(InputObject, *http.Request) (interface{}, int, error), responseFunc func(interface{}, int, http.ResponseWriter, *http.Request) error, w http.ResponseWriter, r *http.Request, logError func(error, *http.Request), ) (err error)
Note that preprocessFunc returns a 400 Bad Request on ANY error So try to make sure that any errors the function returns are actual bad user input errors and not any other kind of logical error (ex, a failed http request in the func *should* probably return 500, but will not) If you REALLY want to control the error, use an empty `preprocessFunc` and put everything in the `logicFunc` NOTE: logicFunc should return a POINTER to your struct instead of the struct or else things might not work right-- see comment in code
func UnmarshalObjectFromJSON ¶
func WalkRouter ¶
Print out all route info Walk function taken from example https://github.com/gorilla/mux#walking-routes
func WriteModelToResponseFromHeaders ¶
func WriteModelToResponseFromHeaders(dataToSend interface{}, status int, w http.ResponseWriter, r *http.Request) error
See notes on jsonapi library-- must pass in in a POINTER to a struct or slice of pointers, or jsonapi library will not work right (json library doesn't care)
func WriteModelToResponseJSON ¶
func WriteModelToResponseJSON(dataToSend interface{}, status int, w http.ResponseWriter) (err error)
func WriteModelToResponseJSONAPI ¶
func WriteModelToResponseJSONAPI(dataToSend interface{}, status int, w http.ResponseWriter) (err error)
Must use a pointer
func WriteNoContentToResponse ¶
func WriteNoContentToResponse(dataToSend interface{}, status int, w http.ResponseWriter, r *http.Request) error
Convenience function so you don't have to write your own wrapper function if you don't want to return NoContent
Types ¶
type InputObject ¶
type InputObject interface {
Validate() error
}