restify

package module
v0.0.0-...-e6ffaab Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: MIT Imports: 19 Imported by: 0

README

restify

Documentation License Build Status Coverage Go Report Card

An Opinionated package for building HTTP RESTFul API

Installation

Make sure you have a working Go environment. Go version 1.13.x is supported.

See the install instructions for Go.

To install restify, simply run:

$ go get github.com/phogolabs/restify

Getting Started

type CreateUserInput struct {
	FirstName string `json:"first_name" header:"-"            validate:"required"`
	LastName  string `json:"last_name"  header:"-"            validate:"required"`
	CreatedBy string `json:"-"          header:"X-Created-By" validate:"-"`
}

type CreateUserOutput struct {
	UserID string `json:"user_id"`
}

func (output *CreateUserOutput) Status() int {
	return http.StatusCreated
}

func create(w http.ResponseWriter, r *http.Request) {
	reactor := restify.NewReactor(w, r)

	var (
		input  = &CreateUserInput{}
		output = &CreateUserOutput{}
	)

	if err := reactor.Bind(input); err != nil {
		reactor.Render(err)
		return
	}

	// TODO: implement your logic here

	if err := reactor.Render(output); err != nil {
		reactor.Render(err)
		return
	}
}

Contributing

We are open for any contributions. Just fork the project.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeForm

func DecodeForm(r io.ReadCloser, v interface{}) error

DecodeForm decodes a object from Form reader

func DecodeJSON

func DecodeJSON(r io.Reader, v interface{}) error

DecodeJSON decodes a object from JSON reader

func DecodeXML

func DecodeXML(r io.Reader, v interface{}) error

DecodeXML decodes a object from XML reader

func EncodeForm

func EncodeForm(w http.ResponseWriter, obj interface{}) error

EncodeForm marshals 'obj' to URL encoded data, and setting the Content-Type as application/x-www-form-urlencoded.

func EncodeJSON

func EncodeJSON(w http.ResponseWriter, obj interface{}) error

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

func EncodeXML

func EncodeXML(w http.ResponseWriter, obj interface{}) error

EncodeXML marshals 'obj' 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'.

func GetLogger

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

GetLogger returns the associated request logger

Types

type Binder

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

Binder interface for managing request payloads.

type Content

type Content struct {
	Type   ContentType
	Params map[string]string
}

Content represents the content type

func ParseContent

func ParseContent(header string) (*Content, error)

ParseContent parse the content type

type ContentType

type ContentType int

ContentType is an enumeration of common HTTP content types.

const (
	ContentTypeUnknown ContentType = iota
	ContentTypePlainText
	ContentTypeHTML
	ContentTypeJSON
	ContentTypeXML
	ContentTypeForm
	ContentTypeEventStream
)

ContentTypes handled by this package.

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder decodes a request

func NewDecoder

func NewDecoder(r *http.Request) *Decoder

NewDecoder creates a new decoder

func (*Decoder) Decode

func (d *Decoder) Decode(obj interface{}) (err error)

Decode decodes the request

func (*Decoder) SetContentType

func (d *Decoder) SetContentType(kind ContentType)

SetContentType overrides the Content-Type attribute

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder encodes the request

func NewEncoder

func NewEncoder(w http.ResponseWriter, r *http.Request) *Encoder

NewEncoder creates a new encoder

func (*Encoder) Encode

func (e *Encoder) Encode(obj interface{}) (err error)

Encode encodes the request

func (*Encoder) SetContentType

func (e *Encoder) SetContentType(kind ContentType)

SetContentType overrides the Content-Type attribute

func (*Encoder) Status

func (e *Encoder) Status(code int)

Status sets the response status code

type Reactor

type Reactor struct {
	// contains filtered or unexported fields
}

Reactor works with http response and request

func NewReactor

func NewReactor(w http.ResponseWriter, r *http.Request) *Reactor

NewReactor creates a new reactor

func (*Reactor) Bind

func (r *Reactor) Bind(obj interface{}) (err error)

Bind binds the object

func (*Reactor) Render

func (r *Reactor) Render(obj interface{}) (err error)

Render renders the response

func (*Reactor) RenderWith

func (r *Reactor) RenderWith(code int, obj interface{}) (err error)

RenderWith renders the response with code

func (*Reactor) Status

func (r *Reactor) Status(code int)

Status sets the response status code

type Renderer

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

Renderer interface for managing response payloads.

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator validates an object

func NewValidator

func NewValidator(r *http.Request) *Validator

NewValidator creates a new validator

func (*Validator) Validate

func (v *Validator) Validate(obj interface{}) (err error)

Validate validates the object

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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