rest

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2014 License: MIT Imports: 9 Imported by: 0

README

REST

GoDoc

Simple REST interface over MongoDB, as middlware for martini framework. Useful to create single page applications, REST style based.

Usage:
package main

import (
  "github.com/go-martini/martini"
  "labix.org/v2/mgo"
  "github.com/olebedev/rest"
)

func access(res http.ResponseWriter, req *http.Request){
  // Your data access logic here
}

func main() {
  session, err := mgo.Dial("localhost")
  if err != nil {
    panic(err)
  }
  defer session.Close()
  session.SetMode(mgo.Monotonic, true)
  db := session.DB("test")

  m := martini.Classic()
  
  m.Group("/api/v1", rest.Rest(rest.Config{
    Db:           db, 
    ResonseField: "data", // optional
    // Use integer autoincrement for _id instead of mongodb auto generated hash, default false. optional
    // Autoincrement: true, 
  }, access))

  m.Run()
}

Now you can send HTTP requests to http://localhost:3000/api/v1/example_collection.
Available GET parameters:

  • query - JSON mongodb query statement
  • limit - int
  • skip - int
  • sort - string, more detail
  • count - bool
  • select - JSON mongodb select statement
Examples:

Let's create something simple.

$ curl -X POST http://localhost:5000/api/v1/test -s \
  -H "Accept: application/json" \
  -H "Content-type: application/json" \
  -d '{"hello":"world"}'

{
  "data": { // ResonseField name
    "_id":"52d382ae367e0ee611626bf1"
    "hello":"world"
  }
}
$ // ... and so on

To get full collection use this command:

$ curl http://localhost:5000/api/v1/test
{
  "data": [
    {
      "hello": "world",
      "_id": "52d382ae367e0ee611626bf0"
    },
    {
      "hello": "world 2",
      "_id": "52d382b0367e0ee611626bf1"
    },
    {
      "hello": "world 3",
      "_id": "52d382b4367e0ee611626bf2"
    }
  ]
}

Also available PUT(by id) & DELETE(by id) methods.

Documentation

Overview

Package rest is a simple REST interface over MongoDB, as middlware for martini framework

package main

import (

"github.com/go-martini/martini"
"labix.org/v2/mgo"
"github.com/olebedev/rest"

)

func main() {
  session, err := mgo.Dial("localhost")
  if err != nil {
    panic(err)
  }
  defer session.Close()
  session.SetMode(mgo.Monotonic, true)
  db := session.DB("test")

  m := martini.Classic()

  m.Group("/api/v1", rest.Rest(rest.Config{
    Db            : db,
    ResonseField  : "data", // optional
    Autoincrement : true,   // optional
  }))

  m.Run()
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Rest

func Rest(c Config) func(r martini.Router)

Types

type Config

type Config struct {
	// Mongo instance pointer.
	Db *mgo.Database
	// Optional response field name. It is necessary to obtain the expected response.
	ResonseField string
	// Use integer autoincrement for _id instead of mongodb auto generated hash
	Autoincrement bool
}

Config is a struct for specifying configuration options for the rest.Rest middleware.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL