ghost

package module
v0.0.0-...-17382d8 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2016 License: MIT Imports: 4 Imported by: 0

README

Ghost: A Lightweight REST Framework

Ghost is a totally invisible framework for writing testable REST APIs. The idea is that each resource is broken up into several components:

  • An input model (which gets parsed from the request)
  • A validator which validates the input model
  • A processor which processes the input and possibly returns a different model, it may also write to the database / etc.
  • A writer which decides how to serialize the processed data for the user.

See the examples/ directory for a straightforward how-to.

By separating resources into components, each resource can be tested by component without needing a monolithic testing suite for every single resource.

Ghost is probably simple enough that you can write something like it yourself, but if you want to contribute back then please feel free to fork and submit a pull request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Extender

type Extender interface {
	// Extend the context by calling ctx.WithValue()
	Extend(ctx context.Context) context.Context
}

A context extender is an interface that adds values to a context that passes through it. For example, if you want database access inside a context you might build a context extender to pass along database connections.

type GhostError

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

An implementation of HttpError for simple things.

func (*GhostError) Error

func (e *GhostError) Error() string

func (*GhostError) StatusCode

func (e *GhostError) StatusCode() int

type HttpError

type HttpError interface {
	error
	StatusCode() int
}

A type for HTTP errors that extend the base error type. Provides a method for status codes to return to the HTTP client.

func NewHttpError

func NewHttpError(err error, code int) HttpError

type NullModel

type NullModel struct {
}

A null model doesn't do anything

func (*NullModel) FromRequest

func (m *NullModel) FromRequest(r *http.Request) HttpError

type NullProcessor

type NullProcessor struct {
}

A null processor doesn't do anything

func (*NullProcessor) Process

func (p *NullProcessor) Process(interface{}) (interface{}, HttpError)

type NullValidator

type NullValidator struct {
}

A null validator doesn't do anything

func (*NullValidator) Validate

func (v *NullValidator) Validate(interface{}) HttpError

type NullWriter

type NullWriter struct {
}

A null writer doesn't do anything

func (*NullWriter) Serialize

func (w *NullWriter) Serialize(interface{}) ([]byte, HttpError)

type Processor

type Processor interface {
	// Process the information or return an HTTP error if something is wrong,
	// or nil otherwise.
	Process(source interface{}) (interface{}, HttpError)
}

type RequestModel

type RequestModel interface {
	// Fill the model with the information from the request. One way to do this
	// is to just use json.Unmarshal(), but you might also want some other
	// information extracted.
	FromRequest(r *http.Request) HttpError
}

A request model is a type that wraps user input from a request body. Typically this will be a struct representation of some JSON object.

type RouteBuilder

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

func (*RouteBuilder) Extender

func (b *RouteBuilder) Extender(e Extender) *RouteBuilder

Add an Extender that extends the request context coming through to the HTTP handler.

func (*RouteBuilder) Methods

func (b *RouteBuilder) Methods(methods ...string) *RouteBuilder

Define the HTTP methods (verbs) that can be used on this route.

func (*RouteBuilder) Model

func (b *RouteBuilder) Model(m RequestModel) *RouteBuilder

Add a model which is simply a representation of the client input in a Go data structure.

func (*RouteBuilder) Processor

func (b *RouteBuilder) Processor(p Processor) *RouteBuilder

A processor operates on the input sent from the client, manipulates, extends it, whatever. Ultimately it returns something new.

func (*RouteBuilder) ServeHTTP

func (b *RouteBuilder) ServeHTTP(w http.ResponseWriter, r *http.Request)

Satisfies the http.Handler interface. The route builder will use the provided extensions to create a handler and serve HTTP through the given extensions.

func (*RouteBuilder) Validator

func (b *RouteBuilder) Validator(v Validator) *RouteBuilder

Add a validator, which validates input coming from the HTTP client before it gets operated on by a processor or writer.

func (*RouteBuilder) Writer

func (b *RouteBuilder) Writer(w Writer) *RouteBuilder

Add a writer which serializes the output from a processor.

type Router

type Router struct {
	Mux *mux.Router
}

A ghost router is actually backed by a gorilla mux, but we add some helpers to make building the API a little easier.

func NewRouter

func NewRouter() *Router

func (*Router) AddRoute

func (r *Router) AddRoute(path string) *RouteBuilder

Add the given path to the router, and return a route builder to begin constructing the additional properties of the router.

type Validator

type Validator interface {
	// Validate the input and return an HTTP error if something is wrong, or
	// nil otherwise.
	Validate(interface{}) HttpError
}

This is a type that validates an InputModel after the model has been parsed from the request body.

type Writer

type Writer interface {
	Serialize(interface{}) ([]byte, HttpError)
}

Write the information to the response writer to return the result to the HTTP client. Probably this will involve a call to json.Marshal(), unless you are using a different serialization method.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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