rest

package module
v0.0.0-...-16e4cac Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2014 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

REST

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/codegangsta/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.Use(rest.Rest(rest.Config{
    Prefix:       "/api/v1",
    Db:           db, 
    ResonseField: "data", // optional
    // Use integer autoincrement for _id instead of mongodb auto generated hash, default false. optional
    Autoincrement: false, 
  }, 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 & DELETE method.

Documentation

Overview

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

package main

import (
  "github.com/codegangsta/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.Use(rest.Serve(&rest.Config{
    Prefix:       "/api/v1",
    Db:           db,
    ResonseField: "data", // optional
  }))

  m.Run()
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Rest

func Rest(c Config, h ...martini.Handler) martini.Handler

Types

type Config

type Config struct {
	// API prefix for routing it right.
	Prefix string
	// 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
⌂ 换网址