Atlas
Minimalistic Go Library for Creating JSON API Servers. (Deprecated: labstack/echo is much better)
import "github.com/azer/atlas"
var api = atlas.New(atlas.Map{ "/": Hello })
func Hello(request *atlas.Request) *atlas.Response {
return atlas.Success("Hello World")
}
api.Start(":8080")
It'll output the JSON encoding of whatever is returned from request handlers:
$ curl localhost:8080
{ ok: true, result: "Hello World" }
JSONP is enabled by default:
$ curl localhost:8080?callback=foobar
foobar({ ok: true, result: "Hello World" })
Install
$ go get github.com/azer/atlas
Manual
Create a new API by defining Sinatra-like routes:
import "github.com/azer/atlas"
var api = atlas.New(atlas.Map{
"/user/:name/:surname": User,
"/company/:id": Company,
"/hello": Hello,
})
Every route points to a function (atlas.Handler) that takes *atlas.Request and returns a *Response
. Atlas comes with Success
and Error
functions that converts anything to a response:
func Hello(request *atlas.Request) *atlas.Response {
return atlas.Success("Hello World")
}
You can start the API by calling Start
method:
api.Start(":8080")
Atlas will output JSON for you:
$ curl http://localhost:8080/hello
{ ok: true, result: "Hello World!" }
If you leave "/" without a handler, Atlas will show a simple API index by default:
$ curl http://localhost:8080/
{
"welcome": true,
"endpoints": [
"/company/:id",
"/hello",
"/user/:name/:surname"
]
}
You can return structs, maps, etc... Atlas will output them as JSON for you:
type Person struct { Name, Surname string }
func User(request *atlas.Request) *atlas.Response {
return atlas.Success(&Person{request.Params["name"], request.Params["surname"]})
}
Requests to /user/:name/:surname
will get:
$ curl http://localhost:8080/user/john/smith
{
ok: true,
result: {
Name: "john",
Surname: "smith"
}
}
JSON Form Posts
To read JSON form posts easily;
func HelloWorld (request *atlas.Request) *atlas.Response {
var data map[string]string
err := request.JSONPost(&data)
if err != nil {
return atlas.Error(500, err)
}
return atlas.Success(data)
}
Using JSON Tags
If you'd like to modify struct keys for API, here is an example of how to do it:
type Person struct {
Name string `json:"name"`
Surname string `json:"surname"`
}
Error Handling
Atlas also has a handy method to produce errors:
func Company(request *atlas.Request) *atlas.Response {
return atlas.Error(500, "Not Implemented Yet")
}
Will output:
$ curl localhost:8080/company/foobar
{
error: "Not implemented yet"
}
Checkout examples/
for more info.
Reference
atlas.Request
Atlas will pass you its modified version of requests:
type Request struct {
Header map[string][]string
Params urlrouter.Params
Method string
Host string
URL *url.URL
GET bool
POST bool
Body io.ReadCloser
Form url.Values
PostForm url.Values
Query url.Values
}
atlas.Manual
If you'd like to structure the entire response, including the wrapper, use Manual
instead of Success
or Error
.
Here is an example of how to do that:
func Hello(request *atlas.Request) *atlas.Response {
return atlas.Manual(200, "Hello World")
}
atlas.API.Listen
To provide your own net.Listener, call Listen
:
listener, _ := net.Listen("tcp", "0.0.0.0:6666")
var api = atlas.New(atlas.Map{ "/": Hello })
api.Listen(listener)
Debugging
Atlas uses debug for logging. Enable verbose mode by:
DEBUG=* go run my server