Creating a REST API using Go (GoLang) and JAS (JSON API Server)

Post to Twitter

In a previous article I showed you how to get Go up and running on an Ubuntu 13.04 Desktop system. Today, I’ll show you how to build a simple REST API using Go. In particular, I’ll be using JAS (JSON API Server) which is a simple REST API framework.


Note: JAS requires Go version 1.1.x or higher.

The first thing you will want to do is add GOPATH to your ~/.bashrc file. My ~/.bashrc file looks like this (at the bottom of the file):

export PATH=$PATH:/usr/local/go/bin
export GOPATH=~/go-pkgs
export PATH=$PATH:$GOPATH

Create the following folders, one for the packages, one for our project, and one for the project itself as well as a file to put our Go code into:

$ cd
$ mkdir go-pkgs
$ mkdir go-jas-rest-proj && cd go-jas-rest-proj
$ touch rest-server.go

Note: I’m not claiming this to be the best way to setup your Go environment or packages, this is just what I’m using for this article’s examples.

Inside of the rest-server.go add the following code (borrowed from the JAS readme and modified by me to run easily):

package main

import (
    "fmt"
    "net/http"
    "github.com/coocood/jas"
)

type Hello struct {}

func (*Hello) Get (ctx *jas.Context) {
    ctx.Data = "Hello World"
}

func main () {
    router := jas.NewRouter(new(Hello))
    router.BasePath = "/v1/"
    fmt.Println(router.HandledPaths(true))
    http.Handle(router.BasePath, router)
    http.ListenAndServe(":8080", nil)
}

One more thing we need to do is add the JAS package:

$ go get github.com/coocood/jas

The JAS package will be installed at: ~/go-pkgs/

Run the project now and hit this address with a web browser: http://localhost:8080/v1/hello

$ go run rest-server.go

Results:

{"data":"Hello World","error":null}

So that takes care of a basic example. Let’s move onto something a little more in-depth. Using the JAS Docs as a starting place we can do the following:

package main

import (
    "fmt"
    "net/http"
    "github.com/coocood/jas"
)

type Users struct {}

func (*Users) Get (ctx *jas.Context) {
    ctx.Data = "Users GET"
}

func (*Users) Post (ctx *jas.Context) {
    ctx.Data = "Users POST"
}

func (*Users) Delete (ctx *jas.Context) {
    ctx.Data = "Users Delete"
}

func (*Users) Put (ctx *jas.Context) {
    ctx.Data = "Users PUT"
}

func main () {
    router := jas.NewRouter(new(Users))
    router.BasePath = "/v1/"
    fmt.Println(router.HandledPaths(true))
    http.Handle(router.BasePath, router)
    http.ListenAndServe(":8080", nil)
}

Run the server code:

$ go run rest-server.go

Using a tool like Poster for Firefox you can easily test the above code and the results should be similar to this:

{"data":"Users GET","error":null}
{"data":"Users POST","error":null}
{"data":"Users PUT","error":null}
{"data":"Users DELETE","error":null}

Just point Poster to the following URL: http://localhost:8080/v1/users

Poster with Firefox

This just touches on what JAS is capable of, there is plenty more it offers so feel free to read about it and investigate further.

Post to Twitter

This entry was posted in Go, Open Source, REST. Bookmark the permalink.

One Response to Creating a REST API using Go (GoLang) and JAS (JSON API Server)

  1. Pingback: Creating a REST API using Go (GoLang) and Goweb | Giant Flying Saucer

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>