rest

package module
v0.0.0-...-552cc5b Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 18 Imported by: 0

README

rest

Documentation License Build Status Coverage Go Report Card

Built Restful Services

rest

Overview

rest is a package that facilitates developing Web API Services with chi. It provides set of utility functions for sending responses and handling errors.

Installation

$ go get -u github.com/phogolabs/rest

Contributing

We are welcome to any contributions. Just fork the project.

logo made by Free Pik

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoRouteContextFound = fmt.Errorf("no route context found")

ErrNoRouteContextFound returns no route context error

Functions

func Bind

func Bind(r *http.Request, v Binder) error

Bind decodes a request body and executes the Binder method of the payload structure.

func Data

func Data(w http.ResponseWriter, r *http.Request, v []byte)

Data writes raw bytes to the response, setting the Content-Type as application/octet-stream.

func Decode

func Decode(r *http.Request, v interface{}) error

Decode is a package-level variable set to our default Decoder. We do this because it allows you to set render.Decode to another function with the same function signature, while also utilizing the render.Decoder() function itself. Effectively, allowing you to easily add your own logic to the package defaults. For example, maybe you want to impose a limit on the number of bytes allowed to be read from the request body.

func DecodeForm

func DecodeForm(r *http.Request, v interface{}) (err error)

DecodeForm decodes an entity from form fields

func DecodeHeader

func DecodeHeader(r *http.Request, v interface{}) error

DecodeHeader decodes an entity from query

func DecodePath

func DecodePath(r *http.Request, v interface{}) error

DecodePath decodes an entity from path

func DecodePathContext

func DecodePathContext(ctx context.Context, v interface{}) error

DecodePath decodes an entity from path

func DecodeQuery

func DecodeQuery(r *http.Request, v interface{}) error

DecodeQuery decodes an entity from query

func EncodeHeader

func EncodeHeader(w http.ResponseWriter, v interface{}) error

EncodeHeader encodes a header

func Error

func Error(w http.ResponseWriter, r *http.Request, err error)

Error injects the error within the request Deprecated: Use Respond instead

func ErrorJSON

func ErrorJSON(w http.ResponseWriter, r *http.Request, err error)

ErrorJSON injects the error within the request Deprecated: Use JSON instead

func ErrorXML

func ErrorXML(w http.ResponseWriter, r *http.Request, err error)

ErrorXML injects the error within the request Deprecated: Use XML instead

func GetLogger

func GetLogger(r *http.Request) log.Logger

GetLogger returns the associated request logger

func HTML

func HTML(w http.ResponseWriter, r *http.Request, v string)

HTML writes a string to the response, setting the Content-Type as text/html.

func JSON

func JSON(w http.ResponseWriter, r *http.Request, v interface{})

JSON marshals 'v' to JSON, automatically escaping HTML and setting the Content-Type as application/json.

func NoContent

func NoContent(w http.ResponseWriter, r *http.Request)

NoContent returns a HTTP 204 "No Content" response.

func PlainText

func PlainText(w http.ResponseWriter, r *http.Request, v string)

PlainText writes a string to the response, setting the Content-Type as text/plain.

func Print

func Print(routes chi.Routes)

Print prints the routes

func RegisterValidation

func RegisterValidation(tag string, fn validator.Func)

RegisterValidation adds a validation with the given tag

func Render

func Render(w http.ResponseWriter, r *http.Request, v Renderer) error

Render renders a single payload and respond to the client request.

func Respond

func Respond(w http.ResponseWriter, r *http.Request, v interface{})

Respond handles streaming JSON and XML responses, automatically setting the Content-Type based on request headers. It will default to a JSON response.

func Status

func Status(r *http.Request, status int)

Status sets a HTTP response status code hint into request context at any point during the request life-cycle. Before the Responder sends its response header it will check the StatusCtxKey

func UnmarshalForm

func UnmarshalForm(data []byte, v interface{}) error

UnmarshalForm unmashals form

func Validate

func Validate(r *http.Request, data interface{}) error

Validate validates a data

func XML

func XML(w http.ResponseWriter, r *http.Request, v interface{})

XML marshals 'v' to JSON, setting the Content-Type as application/xml. It will automatically prepend a generic XML header (see encoding/xml.Header) if one is not found in the first 100 bytes of 'v'.

Types

type Binder

type Binder interface {
	Bind(r *http.Request) error
}

Binder interface for managing request payloads.

type Renderer

type Renderer interface {
	Render(w http.ResponseWriter, r *http.Request) error
}

Renderer interface for managing response payloads.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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