Skip to content

Commit

Permalink
Update README & Add Go Report Card
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklaw5 committed Mar 17, 2017
1 parent 0a7be6c commit afd3b79
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
37 changes: 22 additions & 15 deletions README.md
Expand Up @@ -2,7 +2,10 @@

A Go package for handling common HTTP JSON responses.

[![Build Status](https://travis-ci.org/nicklaw5/go-respond.svg?branch=master)](https://travis-ci.org/nicklaw5/go-respond) [![Coverage Status](https://coveralls.io/repos/github/nicklaw5/go-respond/badge.svg)](https://coveralls.io/github/nicklaw5/go-respond)
[![GoDoc](https://godoc.org/github.com/nicklaw5/go-respond?status.svg)](https://godoc.org/github.com/nicklaw5/go-respond)
[![Build Status](https://travis-ci.org/nicklaw5/go-respond.svg?branch=master)](https://travis-ci.org/nicklaw5/go-respond)
[![Coverage Status](https://coveralls.io/repos/github/nicklaw5/go-respond/badge.svg)](https://coveralls.io/github/nicklaw5/go-respond)
[![Go Report Card](https://goreportcard.com/badge/github.com/nicklaw5/go-respond)](https://goreportcard.com/report/github.com/nicklaw5/go-respond)

## Installation

Expand All @@ -23,14 +26,21 @@ import (
resp "github.com/nicklaw5/go-respond"
)

type response struct {
Success bool `json:"success"`
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc("/api/users", func(w http.ResponseWriter, r *http.Request) {
users := []User{
{1, "Billy", "billy@example.com"},
{2, "Joan", "joan@example.com"},
}

resp.NewResponse(w).
Ok(&response{true})
Ok(users)
})

http.ListenAndServe(":8080", nil)
Expand All @@ -57,6 +67,10 @@ func main() {
| 500 | InternalServerError() |
| 501 | NotImplemented() |

See [here](https://httpstatuses.com/) for a complete list of HTTP responses, along with an explanation of each.

Please submit a PR if you want to add to this list. Only the most common response types have been included.

## Handling Errors

The best option for handling errors that may occur while marshalling the JSON response, is to use [Negroni's Recovery middleware](https://github.com/urfave/negroni#recovery). Here's an example:
Expand All @@ -71,22 +85,15 @@ import (
resp "github.com/nicklaw5/go-respond"
)

type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
type Response struct {
Success bool `json:"success"`
}

func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
users := []User{
{1, "Billy", "billy@example.com"},
{2, "Joan", "joan@example.com"},
}

resp.NewResponse(w).
Created(users)
Ok(&Response{true})
})

n := negroni.New()
Expand Down
6 changes: 3 additions & 3 deletions response.go
Expand Up @@ -5,13 +5,13 @@ import (
"net/http"
)

// Response is our HTTP response
// Response is the HTTP response
type Response struct {
Writer http.ResponseWriter
Headers map[string]string
}

// NewResponse create and return an
// NewResponse creates and returns a new response
func NewResponse(w http.ResponseWriter) *Response {
return &Response{
Writer: w,
Expand All @@ -21,7 +21,7 @@ func NewResponse(w http.ResponseWriter) *Response {
}
}

// DeleteHeader deletes a single provided header
// DeleteHeader deletes a single header from the response
func (resp *Response) DeleteHeader(key string) *Response {
resp.Writer.Header().Del(key)
return resp
Expand Down

0 comments on commit afd3b79

Please sign in to comment.