Skip to content

Spokepoint/go-json-rest

 
 

Repository files navigation

Go-Json-Rest

A quick and easy way to setup a RESTful JSON API

Build Status

Go-Json-Rest is a thin layer on top of net/http that helps building RESTful JSON APIs easily. It provides fast URL routing using a Trie based implementation, and helpers to deal with JSON requests and responses. It is not a high-level REST framework that transparently maps HTTP requests to procedure calls, on the opposite, you constantly have access to the underlying net/http objects.

Features

  • Implemented as a net/http Handler. This standard interface allows combinations with other Handlers.
  • Fast URL routing. It implements the classic route description syntax using a fast and scalable trie data structure.
  • Test package to help writing tests for the API.
  • Optional /.status endpoint for easy monitoring.
  • Examples

Install

This package is "go-gettable", just do:

go get github.com/ant0ine/go-json-rest

Vendoring

The recommended way of using this library in your project is to use the "vendoring" method, where this library code is copied in your repository at a specific revision. This page is a good summary of package management in Go.

Example

package main
import (
        "github.com/ant0ine/go-json-rest"
        "net/http"
)
type User struct {
        Id   string
        Name string
}
func GetUser(w *rest.ResponseWriter, req *rest.Request) {
        user := User{
                Id:   req.PathParam("id"),
                Name: "Antoine",
        }
        w.WriteJson(&user)
}
func main() {
        handler := rest.ResourceHandler{}
        handler.SetRoutes(
                rest.Route{"GET", "/users/:id", GetUser},
        )
        http.ListenAndServe(":8080", &handler)
}

More Examples

(See the dedicated examples repository: https://github.com/ant0ine/go-json-rest-examples)

  • Countries Demo very simple GET, POST, DELETE operations
  • Users Demo the mapping to object methods
  • SPDY Demo SPDY using github.com/shykes/spdy-go
  • GAE Demo go-json-rest on Google App Engine
  • GORM Demo basic CRUD operations using MySQL and GORM
  • Streaming Demo Line Delimited JSON stream
  • CORS Demo CORS support for all endpoints
  • Basic Auth Demo an Authentication Basic impl for all endpoints

Documentation

Options

Things to enable in production:

  • Gzip compression (default: disabled)
  • Custom Logger (default: Go default)

Things to enable in development:

  • Json indentation (default: enabled)
  • Relaxed ContentType (default: disabled)
  • Error stack trace in the response body (default: disabled)

The Status Endpoint

Inspired by memcached "stats", this optional feature can be enabled to help monitoring the service.

GET /.status returns something like:

{
  "Pid": 21732,
  "UpTime": "1m15.926272s",
  "UpTimeSec": 75.926272,
  "Time": "2013-03-04 08:00:27.152986 +0000 UTC",
  "TimeUnix": 1362384027,
  "StatusCodeCount": {
    "200": 53,
    "404": 11
  },
  "TotalCount": 64,
  "TotalResponseTime": "16.777ms",
  "TotalResponseTimeSec": 0.016777,
  "AverageResponseTime": "262.14us",
  "AverageResponseTimeSec": 0.00026214
}

Thanks

Copyright (c) 2013-2014 Antoine Imbert

MIT License

Analytics

Packages

No packages published

Languages

  • Go 100.0%