Giter Club home page Giter Club logo

jsend's Introduction

JSend's implementation writen in Go(golang)

Build Status Coverage Status GoDoc Go Report Card Release

This package is an implementation of JSend specification written in Go(golang).

Installation

go get github.com/clevergo/jsend

Usage

Usage is pretty simple.

// success response
jsend.Success(w, "any type of data")
// fail response
jsend.Fail(w, "any type of data")
// error response
jsend.Error(w, "error message")
// error response with extra code
jsend.ErrorCode(w, "error message", errorCode)
// error response with extra code and data
jsend.ErrorCodeData(w, "error message", errorCode, "any type of data")

Error Handling

It is application responsibility to handle error, see Full Example for further more detail.

Status Code

By default status code http.StatusOK was used implicitly, it can also be specified by the last parameter if necessary.

jsend.Success(w, "any type of data", http.StatusOK)
jsend.Fail(w, "any type of data", http.StatusForbidden)
jsend.Error(w, "error message", http.StatusInternalServerError)

Full Example

package main

import (
	"log"
	"net/http"
	"sync"

	"github.com/clevergo/clevergo"
	"github.com/clevergo/jsend"
)

var users *Users

func init() {
	users = &Users{
		mutex: &sync.RWMutex{},
		entries: []User{
			User{"foo", "[email protected]"},
			User{"bar", "[email protected]"},
		},
	}
}

func handleError(w http.ResponseWriter, err error) {
	// for method chaining
	if err == nil {
		return
	}

	log.Println(err.Error())
	// convert error as jsend response
	if err = jsend.Error(w, err.Error(), http.StatusInternalServerError); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func getUsers(w http.ResponseWriter, r *http.Request) {
	handleError(w, jsend.Success(w, users.entries))
}

func getUser(w http.ResponseWriter, r *http.Request) {
	id := clevergo.GetParams(r).Get("id")
	user, found := users.find(id)
	if !found {
		handleError(w, jsend.Error(w, "User Not Found", http.StatusNotFound))
		return
	}

	handleError(w, jsend.Success(w, user))
}

func createUser(w http.ResponseWriter, r *http.Request) {
	if err := r.ParseForm(); err != nil {
		handleError(w, err)
		return
	}

	errs := map[string][]string{}
	id := r.FormValue("id")
	if id == "" {
		errs["id"] = append(errs["id"], "id can not be blank")
	}
	if _, found := users.find(id); found {
		errs["id"] = append(errs["id"], "id was taken")
	}
	email := r.FormValue("email")
	if email == "" {
		errs["email"] = append(errs["email"], "email can not be blank")
	}
	if len(errs) > 0 {
		handleError(w, jsend.Fail(w, errs))
		return
	}

	user := User{
		ID:    id,
		Email: email,
	}
	users.insert(user)

	handleError(w, jsend.Success(w, user))
}

func deleteUser(w http.ResponseWriter, r *http.Request) {
	id := clevergo.GetParams(r).Get("id")
	user, found := users.find(id)
	if !found {
		handleError(w, jsend.Error(w, "User Not Found", http.StatusNotFound))
		return
	}

	users.delete(user.ID)
	handleError(w, jsend.Success(w, nil))
}

func main() {
	app := clevergo.New(":1234")
	app.Get("/users", getUsers)
	app.Post("/users", createUser)
	app.Get("/users/:id", getUser)
	app.Delete("/users/:id", deleteUser)
	app.ListenAndServe()
}

type Users struct {
	entries []User
	mutex   *sync.RWMutex
}

func (us *Users) find(id string) (User, bool) {
	for _, user := range users.entries {
		if user.ID == id {
			return user, true
		}
	}

	return User{}, false
}

func (us *Users) insert(user User) {
	us.mutex.Lock()
	defer us.mutex.Unlock()

	us.entries = append(us.entries, user)
}

func (us *Users) delete(id string) {
	us.mutex.Lock()
	defer us.mutex.Unlock()

	for i, user := range us.entries {
		if user.ID == id {
			us.entries = append(us.entries[:i], us.entries[i+1:]...)
		}
	}
}

type User struct {
	ID    string `json:"id"`
	Email string `json:"email"`
}
$ go run main.go

# fetch users
$ curl http://localhost:1234/users
{"status":"success","data":[{"id":"foo","email":"[email protected]"},{"id":"bar","email":"[email protected]"}]}

# create user without required data
$ curl -XPOST  http://localhost:1234/users
{"status":"fail","data":{"email":["email can not be blank"],"id":["id can not be blank"]}}

# create test user
$ curl -XPOST -d "id=test&[email protected]" http://localhost:1234/users
{"status":"success","data":{"id":"test","email":"[email protected]"}}

# refetch users
$ curl http://localhost:1234/users
{"status":"success","data":[{"id":"foo","email":"[email protected]"},{"id":"bar","email":"[email protected]"},{"id":"test","email":"[email protected]"}]}

# fetch test user
$ curl http://localhost:1234/users/test
{"status":"success","data":{"id":"test","email":"[email protected]"}}

# delete test user
$ curl -XDELETE http://localhost:1234/users/test
{"status":"success","data":null}

# refetch test user
$ curl http://localhost:1234/users/test
{"status":"error","data":null,"message":"User Not Found"}

jsend's People

Contributors

razonyang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.