jsonhttp

package
v0.0.2-dev Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2016 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package jsonhttp defines a simple HTTP server that renders JSON.

Routes can be added by passing a handle that should return JSON serializable data or an error.

Index

Examples

Constants

View Source
const (
	// DefaultAddress is the default address of the server.
	DefaultAddress = ":5000"
)

Variables

This section is empty.

Functions

func NotFound

func NotFound(w http.ResponseWriter, r *http.Request, _ httprouter.Params, _ *Config) (interface{}, error)

NotFound is a handle for a route that is not found.

Types

type Config

type Config struct {
	// The address of the server.
	Address string

	// Optionally, the path to a TLS certificate.
	CertFile string

	// Optionally, the path to a TLS private key.
	KeyFile string
}

Config contains configuration options for the server.

type ErrHTTP

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

ErrHTTP is an error with an HTTP status code.

func NewErrBadRequest

func NewErrBadRequest(msg string) ErrHTTP

NewErrBadRequest creates an error with a bad request HTTP status code. If the message is empty, the default is "bad request".

func NewErrHTTP

func NewErrHTTP(msg string, status int) ErrHTTP

NewErrHTTP creates a, error with a message and HTTP status code.

func NewErrInternalServer

func NewErrInternalServer(msg string) ErrHTTP

NewErrInternalServer creates an error with an internal server error HTTP status code. If the message is empty, the default is "internal server error".

func NewErrNotFound

func NewErrNotFound(msg string) ErrHTTP

NewErrNotFound creates an error with a not found HTTP status code. If the message is empty, the default is "not found".

func NewErrUnauthorized

func NewErrUnauthorized(msg string) ErrHTTP

NewErrUnauthorized creates an error with an unauthorized HTTP status code. If the message is empty, the default is "unauthorized".

func (ErrHTTP) Error

func (e ErrHTTP) Error() string

Error implements error.Error.

func (ErrHTTP) JSONMarshal

func (e ErrHTTP) JSONMarshal() []byte

JSONMarshal marshals an error to JSON.

func (ErrHTTP) Status

func (e ErrHTTP) Status() int

Status returns the HTTP status code of the error.

type Handle

type Handle func(http.ResponseWriter, *http.Request, httprouter.Params, *Config) (interface{}, error)

Handle is the function type for a route handle.

type Server

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

Server is the type that implements net/http.Handler.

Example

This example shows how to create a server and add a route with a named param. It also tests the route using net/http/httptest.

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"

	"github.com/julienschmidt/httprouter"

	"github.com/stratumn/go/jsonhttp"
)

func main() {
	// Create the server.
	s := jsonhttp.New(&jsonhttp.Config{Address: ":3333"})

	// Add a route with a named param.
	s.Get("/items/:id", func(r http.ResponseWriter, _ *http.Request, p httprouter.Params, _ *jsonhttp.Config) (interface{}, error) {
		// Return a map containing the ID.
		result := map[string]string{
			"id": p.ByName("id"),
		}

		return result, nil
	})

	// Create a test server.
	ts := httptest.NewServer(s)
	defer ts.Close()

	// Test our route.
	res, err := http.Get(ts.URL + "/items/one")
	if err != nil {
		log.Fatal(err)
	}

	item, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s", item)
}
Output:

{"id":"one"}

func New

func New(config *Config) *Server

New creates an instance of Server.

func (*Server) Delete

func (s *Server) Delete(path string, handle Handle)

Delete adds a DELETE route.

func (*Server) Get

func (s *Server) Get(path string, handle Handle)

Get adds a GET route.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe starts the server.

func (*Server) Options

func (s *Server) Options(path string, handle Handle)

Options adds an OPTIONS route.

func (*Server) Patch

func (s *Server) Patch(path string, handle Handle)

Patch adds a PATCH route.

func (*Server) Post

func (s *Server) Post(path string, handle Handle)

Post adds a POST route.

func (*Server) Put

func (s *Server) Put(path string, handle Handle)

Put adds a PUT route.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements net/http.Handler.ServeHTTP.

Jump to

Keyboard shortcuts

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