rest

package module
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2024 License: MIT Imports: 18 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 (
	ErrUnmarshal    = errors.New("UNMARSHAL_ERROR")
	ErrMissingField = errors.New("MISSING_FIELD")
	ErrNotFound     = errors.New("NOT_FOUND")
	ErrValidate     = errors.New("VALIDATION_ERROR")
)
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)

ErrorResponse - write a HttpError structure as response

func GetAddr added in v0.0.21

func GetAddr(r *http.Request) string

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 error page for not found

func OkResponse added in v0.0.7

func OkResponse(w http.ResponseWriter)

OkResponse - write OK response with application/json Content-Type header

func ReadBody

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

ReadBody - read body from request and trying to unmarshal to provided struct

func Readiness added in v0.0.13

func Readiness(endpoint string, isReady *atomic.Value) func(http.Handler) http.Handler

Readiness - middleware for the readiness probe

func RenderJSON

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

RenderJSON sends data as json

func TextResponse added in v0.0.20

func TextResponse(w http.ResponseWriter, data string)

TextResponse - write a response with application/text Content-Type header

Types

type HttpError

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

HttpError - structure for http errors

func (HttpError) Error

func (e HttpError) Error() string

Just to confirm Error interface

type SSLConfig added in v0.0.16

type SSLConfig struct {
	Port     int    // https server port
	Redirect bool   // defines if http requests will be redirected to the https
	URL      string // url where http requests will be redirected

	CertPath string // path to the ssl certificate
	KeyPath  string // path to the ssl key
}

type Server

type Server struct {
	Address string
	Port    int
	IsReady *atomic.Value
	SSL     *SSLConfig

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

Server - rest server struct

func NewServer added in v0.0.18

func NewServer(port int) *Server

NewServer - will create new rest server on specified port

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() error

Shutdown - shutdown rest server

Jump to

Keyboard shortcuts

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