rest

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2020 License: MIT Imports: 14 Imported by: 1

README

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyRequest = errors.New("empty request")
View Source
var ErrNotPointer = errors.New("not pointer provided")

Functions

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, r *http.Request, code int, error error, msg string)

JsonError - write a HttpError structure as response.

func JsonResponse

func JsonResponse(w http.ResponseWriter, data interface{})

JsonResponse - write a response with application/json Content-Type header.

func Logger

func Logger(next http.Handler) http.Handler

Logger - log all requests

func NotFound

func NotFound(w http.ResponseWriter, _ *http.Request)

NotFound - return a error page for not found

func OkResponse added in v0.0.7

func OkResponse(w http.ResponseWriter)

OKResponse - write a OK response with application/json Content-Type header.

func ReadBody

func ReadBody(w http.ResponseWriter, r *http.Request, str interface{}) error

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

func RenderJSON

func RenderJSON(w http.ResponseWriter, code int, data interface{})

RenderJSON sends data as json.

Types

type HttpError

type HttpError struct {
	Err     string `json:"error"`
	Message string `json:"message,omitempty"`
}

HttpError - structure for http errors.

func (HttpError) Error

func (e HttpError) Error() string

Just to confirm Error interface.

type Server

type Server struct {
	Port int

	ReadHeaderTimeout time.Duration
	WriteTimeout      time.Duration
	IdleTimeout       time.Duration
	// contains filtered or unexported fields
}

Server - http server struct.

func (*Server) Run

func (s *Server) Run(router http.Handler) error

Run - will initialize server and run it on provided port.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown - shutdown http server.

Jump to

Keyboard shortcuts

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