ember

package module
v0.0.0-...-293f1cd Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2015 License: MIT Imports: 3 Imported by: 1 module, 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

Build Status GoDoc

Ember

Go package ember is a simple back-end for http://emberjs.com applications which use the RESTAdapter.

Using Ember

Getting Ember

go get github.com/kolbasov/ember

Running Tests

go test

TodoMVC Demo Application

The example folder contains TodoMVC demo application.

cd example
go run main.go

Defining routes

Let's assume that our applications shows an editable list of people. Here is the project's structure.

app
  public
    assets
      app.cs
      app.js
      ...
  index.html
main.go

We need to define routes for the index.html, assets and REST API. Also, let's prefix the API with api/v1.

ActionHTTP VerbURL
index.htmlGET/
assetsGET/assets
REST API
FindGETapi/v1/people/1
Find AllGETapi/v1/people
UpdatePUTapi/v1/people/1
CreatePOSTapi/v1/people
DeleteDELETEapi/v1/people/1

First, we need to create an Ember instance.

// Create an ember instance.
e := ember.New()

Then we can define routes for the index.html and assets.

// Register a route for public/index.html
e.Index("public/index.html")

// Register a route for public/assets
e.Assets("/assets", "public/assets")

We use Ember.Namespace method to create a namespace.

// Register a namespace.
api := e.Namespace("/api/v1")

Now we can add a model to the namespace and define its routes.

// Create routes for a model.
people := api.Model("people")

// GET /api/v1/people
people.FindAll(getPeople)
...

Call the Ember.Run method to start the application.

// Serve requests.
e.Run(":8080")

Here is a part of the main.go file:

	import (
		"net/http"
		"github.com/kolbasov/ember"
	)

	func main() {
		// Create an ember instance.
		e := ember.New()

		// Register a route for public/index.html
		e.Index("public/index.html")

		// Register a route for public/assets
		e.Assets("/assets", "public/assets")

		// Register a namespace.
		api := e.Namespace("/api/v1")

		// Create routes for a model.
		people := api.Model("people")

		// GET /api/v1/people
		people.FindAll(getPeople)

		// GET /api/v1/people/{id}
		people.Find(getPerson)

		// POST /api/v1/people
		people.Create(addPerson)

		// PUT /api/v1/people/{id}
		people.Update(updatePerson)

		// DELETE /api/v1/people/{id}
		people.Delete(deletePerson)

		// Serve requests.
		e.Run(":8080")
	}

// Functions getPeople, getPerson, updatePerson and deletePerson.
...

The application serves the following routes:

HTTP VerbURL
GET http://localhost:8080
GET/POST http://localhost:8080/api/v1/people
PUT/DELETE http://localhost:8080/api/v1/people/1

Also, it's possible to define a route for a model without a namespace.

	import (
		"net/http"
		"github.com/kolbasov/ember"
	)

	func main() {
		// Create an ember instance.
		e := ember.New()

		// Create routes for a model without a namespace and register a route.
		// GET /stats
		e.Model("stats").FindAll(getStats)

		// Serve requests.
		e.Run(":8080")
	}

The application will serve the following route:

HTTP VerbURL
GET http://localhost:8080/stats

Documentation

Overview

Package ember is a simple back-end for http://emberjs.com applications which use the RESTAdapter.

Using Ember

Getting Ember

go get github.com/kolbasov/ember

Running Tests

go test

TodoMVC Demo Application

The example folder contains TodoMVC demo application.

cd example
go run main.go

Defining routes

Let's assume that our applications shows an editable list of people. Here is the project's structure.

Project structure:

app
  public
    assets
      app.cs
      app.js
      ...
  index.html
main.go

We need to define routes for the index.html, assets and REST API. Also, let's prefix the API with "api/v1".

Static resources:

index.html: GET /
assets:     GET /assets

API:

Find:    GET    api/v1/people/1
FindAll: GET    api/v1/people
Update:  PUT    api/v1/people/1
Create:  POST   api/v1/people
Delete:  DELETE api/v1/people/1

First, we need to create an Ember instance.

// Create an ember instance.
e := ember.New()

Then we can define routes for the index.html and assets.

// Register a route for public/index.html
e.Index("public/index.html")

// Register a route for public/assets
e.Assets("/assets", "public/assets")

We use Ember.Namespace method to create a namespace.

// Register a namespace.
api := e.Namespace("/api/v1")

Now we can add a model to the namespace and define its routes.

// Create routes for a model.
people := api.Model("people")

// GET /api/v1/people
people.FindAll(getPeople)
...

Call the Ember.Run method to start the application.

// Serve requests.
e.Run(":8080")

Here is a part of the main.go file:

import (
	"net/http"
	"github.com/kolbasov/ember"
)

func main() {
	// Create an ember instance.
	e := ember.New()

	// Register a route for public/index.html
	e.Index("public/index.html")

	// Register a route for public/assets
	e.Assets("/assets", "public/assets")

	// Register a namespace.
	api := e.Namespace("/api/v1")

	// Create routes for a model.
	people := api.Model("people")

	// GET /api/v1/people
	people.FindAll(getPeople)

	// GET /api/v1/people/{id}
	people.Find(getPerson)

	// POST /api/v1/people
	people.Create(addPerson)

	// PUT /api/v1/people/{id}
	people.Update(updatePerson)

	// DELETE /api/v1/people/{id}
	people.Delete(deletePerson)

	// Serve requests.
	e.Run(":8080")
}

// Functions getPeople, getPerson, updatePerson and deletePerson.
...

The application serves the following routes:

index.html and assets:

GET: http://localhost:8080

API:

GET/POST   http://localhost:8080/api/v1/people
PUT/DELETE http://localhost:8080/api/v1/people/1

It's possible to create a route without a namespace.

import (
	"net/http"
	"github.com/kolbasov/ember"
)

func main() {
	// Create an ember instance.
	e := ember.New()

	// Create routes for a model without a namespace and register a route.
	// GET /stats
	e.Model("stats").FindAll(getStats)

	// Serve requests.
	e.Run(":8080")
}

The application will serve the following route:

GET http://localhost:8080/stats

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Vars

func Vars(req *http.Request) map[string]string

Types

type Ember

type Ember struct {
	// Router for extenting default functionality.
	Router *mux.Router
}

Ember provides simple routing for http://emberjs.com applications.

func New

func New() *Ember

New returns a new ember instance.

func (*Ember) Assets

func (e *Ember) Assets(route string, path string) *Ember

Assets registers a handler for assets directory.

func (*Ember) Index

func (e *Ember) Index(path string) *Ember

Index registers a handler for the index.html file.

func (*Ember) Model

func (e *Ember) Model(name string) *Model

Model creates a new model instance.

func (*Ember) Namespace

func (e *Ember) Namespace(path string) *Namespace

Namespace creates a new namespace instance.

func (*Ember) Run

func (e *Ember) Run(addr string)

Run listens on the TCP network address addr and then handles requests on incoming connections.

type Model

type Model struct {

	// Router for extenting default functionality.
	Router *mux.Router
	// contains filtered or unexported fields
}

func (*Model) Create

func (m *Model) Create(h http.HandlerFunc) *Model

Create registers a handler for POST /model route.

func (*Model) Delete

func (m *Model) Delete(h http.HandlerFunc) *Model

Create registers a handler for DELETE /model/{id} route.

func (*Model) Find

func (m *Model) Find(h http.HandlerFunc) *Model

Find registers a handler for GET /model/{id} route.

func (*Model) FindAll

func (m *Model) FindAll(h http.HandlerFunc) *Model

Find registers a handler for GET /model route.

func (*Model) Update

func (m *Model) Update(h http.HandlerFunc) *Model

Update registers a handler for PUT /model/{id} route.

type Namespace

type Namespace struct {

	// Router for extenting default functionality.
	Router *mux.Router
	// contains filtered or unexported fields
}

func (*Namespace) Model

func (ns *Namespace) Model(name string) *Model

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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