http/rest server, middleware and helpers.
go get github.com/pkgz/rest
Create a simple http server with timeouts.
Log all requests with level DEBUG.
Log contains next parameters:
- Method
- requested url
- ip address (please use hide real user ip on prod)
- request duration
- response code
[DEBUG] GET - /test - 127.0.0.1 - 10.423µs - 200
Read the body from request and trying to unmarshal to the provided struct.
Write a response with application/json Content-Type header.
Except only bytes or struct.
Makes error response easiest.
JsonError(w, http.StatusBadRequest, err, "Missed value in request")
Error in response has the next structure:
type HttpError struct {
Err string `json:"error"`
Message string `json:"message,omitempty"`
}
Handler for not found endpoint.
Return next response:
Content-Type: text/plain
Status Code: 404 (Not Found)
Body: Not found.