Skip to content

Gurpartap/jakiro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Facilitates use of single function for handling requests from http, websocket and other mediums

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages