Giter Site home page Giter Site logo

kolbasov / ember Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 1.34 MB

Simple back-end for http://emberjs.com applications

Home Page: http://kolbasov.github.io/ember

License: MIT License

Go 0.51% JavaScript 98.41% HTML 0.18% CSS 0.82% Handlebars 0.08%

ember's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.