Help us shape the future of Go! Take the 2026 Go Developer Survey

nap

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2014 License: Apache-2.0 Imports: 2 Imported by: 5

README

#Nap# Build Status Coverage Status

Simple, and I mean simple, REST framework for JSON-based responses.

##Usage##

###Simple###

Here is a dead-simple REST API.

package main

import (
	"net/http"

	"github.com/crawford/nap"
)

func Hello(req *http.Request) (interface{}, nap.Status) {
	return "Hello, World!", nap.OK{}
}

func main() {
	http.Handle("/hello", nap.HandlerFunc(Hello))
	http.Handle("/", nap.NotFoundHandler)
	http.ListenAndServe(":8080", nil)
}

And here it is in action!

curl localhost:8080/hello

{
	"Hello, World!"
}

curl localhost:8080/bye

null

###Custom Wrapper###

It's also possible to customize the response from Nap.

package main

import (
	"net/http"

	"github.com/crawford/nap"
)

type payloadWrapper struct{}

func (w payloadWrapper) Wrap(payload interface{}, status nap.Status) (interface{}, int) {
	return map[string]interface{}{
		"status": map[string]interface{}{
			"code":    status.Code(),
			"message": status.Message(),
		},
		"result": payload,
	}, status.Code()
}

func Hello(req *http.Request) (interface{}, nap.Status) {
	return "Hello, World!", nap.OK{}
}

func main() {
	http.Handle("/hello", nap.HandlerFunc(Hello))
	http.Handle("/", nap.NotFoundHandler)
	http.ListenAndServe(":8080", nil)
}

And here it is in action!

curl localhost:8080/hello

{
	"result": "Hello, World!",
	"status": {
		"code": 200,
		"message": ""
	}
}

curl localhost:8080/bye

{
	"result": null,
	"status": {
		"code": 404,
		"message": "resource not found"
	}
}

###Extra Parameters###

Here is a more interesting example. This pattern is useful for injecting extra parameters into your handlers.

package main

import (
	"net/http"
	"net/url"
	"time"

	"github.com/crawford/nap"
)

func Info(now time.Time, req *http.Request) (interface{}, nap.Status) {
	return struct {
		Url  *url.URL
		Host string
		Time time.Time
	}{
		Url:  req.URL,
		Host: req.Host,
		Time: now,
	}, nap.OK{}
}

func AddTimestamp(fn func(time.Time, *http.Request) (interface{}, nap.Status)) http.Handler {
	return nap.HandlerFunc(func(req *http.Request) (interface{}, nap.Status) {
		return fn(time.Now(), req)
	})
}

func main() {
	http.Handle("/info", AddTimestamp(Info))
	http.Handle("/", nap.NotFoundHandler)
	http.ListenAndServe(":8080", nil)
}

And the result!

{
	"Url": {
		"Scheme": "",
		"Opaque": "",
		"User": null,
		"Host": "",
		"Path": "/info",
		"RawQuery": "",
		"Fragment": ""
	},
	"Host": "localhost:8080",
	"Time": "2014-06-21T23:26:09.002024789-07:00"
}

###Panic###

Not a very good programmer?

package main

import (
	"net/http"

	"github.com/crawford/nap"
)

func Panic(req *http.Request) (interface{}, nap.Status) {
	panic("AHH")
}

func main() {
	http.Handle("/panic", nap.HandlerFunc(Panic))
	http.Handle("/", nap.NotFoundHandler)
	http.ListenAndServe(":8080", nil)
}

Nice save!

null

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MethodNotAllowedHandler HandlerFunc  = HandlerFunc(defaultMethodNotAllowed)
	NotFoundHandler         HandlerFunc  = HandlerFunc(defaultNotFound)
	PayloadWrapper          Wrapper      = DefaultWrapper{}
	PanicHandler            ErrorHandler = nil
	ResponseHeaders         []Header     = defaultHeaders
)

Functions

This section is empty.

Types

type BadRequest

type BadRequest struct {
	Msg string
}

func (BadRequest) Code

func (s BadRequest) Code() int

func (BadRequest) Message

func (s BadRequest) Message() string

type Created

type Created struct {
	Msg string
}

func (Created) Code

func (s Created) Code() int

func (Created) Message

func (s Created) Message() string

type DefaultWrapper

type DefaultWrapper struct{}

func (DefaultWrapper) Wrap

func (w DefaultWrapper) Wrap(payload interface{}, status Status) (interface{}, int)

type ErrorHandler

type ErrorHandler interface {
	Handle(e interface{})
}

type HandlerFunc

type HandlerFunc func(req *http.Request) (interface{}, Status)

func (HandlerFunc) ServeHTTP

func (f HandlerFunc) ServeHTTP(writer http.ResponseWriter, request *http.Request)
type Header struct {
	Name  string
	Value []string
}

type InternalError

type InternalError struct {
	Msg string
}

func (InternalError) Code

func (s InternalError) Code() int

func (InternalError) Message

func (s InternalError) Message() string

type MethodNotAllowed

type MethodNotAllowed struct {
	Msg string
}

func (MethodNotAllowed) Code

func (s MethodNotAllowed) Code() int

func (MethodNotAllowed) Message

func (s MethodNotAllowed) Message() string

type NotFound

type NotFound struct {
	Msg string
}

func (NotFound) Code

func (s NotFound) Code() int

func (NotFound) Message

func (s NotFound) Message() string

type OK

type OK struct {
	Msg string
}

func (OK) Code

func (s OK) Code() int

func (OK) Message

func (s OK) Message() string

type Status

type Status interface {
	Code() int
	Message() string
}

type Wrapper

type Wrapper interface {
	Wrap(payload interface{}, status Status) (interface{}, int)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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