Skip to content

pkgz/rest

Repository files navigation

rest

GoDoc Tests codecov

http/rest server, middleware and helpers.

Installation

go get github.com/pkgz/rest

Server

Create a simple http server with timeouts.

Middleware

Logger

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

Helpers

ReadBody

Read the body from request and trying to unmarshal to the provided struct.

JsonResponse

Write a response with application/json Content-Type header.
Except only bytes or struct.

ErrorResponse

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"`
}

NotFound

Handler for not found endpoint.
Return next response:

Content-Type: text/plain
Status Code: 404 (Not Found)
Body: Not found.

Licence

MIT License