jakiro

package module
v0.0.0-...-5afe843 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2015 License: MIT Imports: 1 Imported by: 3

README

jakiro

GoDoc

Image from dotafire.com. Go there to learn about Jakiro, the dual breath dragon!

jakiro provides a lightweight interface for handling resource actions and JSON encoding/decoding. It also ships with an HTTP and WebSocket implementation of this interface.

In other words, you can use the same handler function to serve http as well as websocket requests. You can also roll your own jakiro.Context implementation to serve through another medium.

type Context interface {
	Params() map[string]string
	Body() []byte

	Write(code int, response []byte)
	JSON(code int, object interface{})
	Error(code int, err error)
}
Implementations

jakiro ships with two implementations of jakiro.Context: HTTPContext and WebSocketContext.

HTTPContext's implementation of Params() assumes that you're using gorilla/mux.

Similarly, WebSocketContext assumes gorilla/websocket.

Fork away for your choice of socket, RPC or HTTP request multiplexer! Implementations are portable anyway!

Get
go get github.com/Gurpartap/jakiro
Usage
package main

import (
	"net/http"

	"github.com/Gurpartap/jakiro"
	"github.com/codegangsta/negroni"
	"github.com/gorilla/mux"
)

var DBUsersTable = make(map[int64]*User, 0)

func withJakiro(handlerFunc func(jakiro.Context)) func(http.ResponseWriter, *http.Request) {
	return func(rw http.ResponseWriter, req *http.Request) {
		handlerFunc(jakiro.NewHTTPContext(rw, req))
	}
}

func main() {
	r := mux.NewRouter()
	n := negroni.Classic()

	r.Methods("GET").Path("/api/users").HandlerFunc(withJakiro(IndexUserHandler))
	r.Methods("POST").Path("/api/users").HandlerFunc(withJakiro(CreateUserHandler))
	r.Methods("GET").Path("/api/users/{id:[0-9]+}").HandlerFunc(withJakiro(ReadUserHandler))
	r.Methods("DELETE").Path("/api/users/{id:[0-9]+}").HandlerFunc(withJakiro(DestroyUserHandler))

	n.UseHandler(r)
	n.Run(":3000")
}

See rest of the example code with CRUD handlers and JSON interfaces usage.

Questions? Bugs?

Create a new issue

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context interface {
	Params() map[string]string
	Body() []byte

	Write(code int, response []byte)
	JSON(code int, object interface{})
	Error(code int, err error)
}

type ContextError

type ContextError struct {
	Code    int      `json:"code,omitempty"`
	Message string   `json:"message,omitempty"`
	Params  []string `json:"params,omitempty"`
}

type JSONDecodable

type JSONDecodable interface {
	FromJSON([]byte) error
}

type JSONDecoderDecodable

type JSONDecoderDecodable interface {
	FromJSONDecoder(*json.Decoder) error
}

type JSONEncodable

type JSONEncodable interface {
	ToJSON() ([]byte, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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