binding

package module
v0.0.0-...-f16a0c2 Latest Latest
Warning

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

Go to latest
Published: May 2, 2015 License: MIT Imports: 6 Imported by: 0

README

JSON bind for gocraft/web package#

  • decode json body to your object
  • encode your object to json and send it as a response

Installation

$ go get github.com/corneldamian/json-binding

Usage

package main

import (
	"net/http"

	"github.com/corneldamian/json-binding"
	"github.com/gocraft/web"
)

type Context struct {
	RequestJSON interface{}
	ResponseJSON interface{}
	ResponseStatus int
}

type Authenticate struct {
	Username string
	Password string
}

func Login(ctx *Context, rw web.ResponseWriter, req *web.Request) {
	a := ctx.RequestJSON.(*Authenticate)
	ctx.ResponseJSON = binding.SuccessResponse("User " + a.Username + " Pass: " +  a.Password)
	ctx.ResponseStatus = http.StatusUnauthorized
}

func main() {
	web := web.New(Context{}).
		Middleware(binding.Response(nil)).		
		Middleware(binding.Request(Authenticate{}, nil)).
		Post("/auth/login", Login)

	http.ListenAndServe("localhost:8080", web)
}

Inspiration from: http://github.com/opennota/json-binding

Jump to

Keyboard shortcuts

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