resourceful

package module
v0.0.0-...-c05f7e9 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2014 License: MIT Imports: 3 Imported by: 0

README

resourceful

Resourceful routing for Go.

Package resourceful provides an implementation of resourceful routing. It simplifies setting up RESTful endpoints and providing handlers.

For full documentation see the godoc.

Example

func NoId(w http.ResponseWriter, r *http.Request) {
  fmt.Fprint(w, "Handler with no resource id")
}

func WithId(w http.ResponseWriter, r *http.Request) {
  fmt.Fprint(w, "Handler with resource id", ResourceId("resource_name", r))
}

func main() {
  r := resourceful.NewResourceRouter()
  h := resourceful.HandlerFuncs{
    IndexFunc: NoId,
    CreateFunc: NoId,
    ShowFunc: WithId,
  }
  r.AddResource(“resource_name”, h)
  http.Handle(“/”, r)

  log.Fatal(http.ListenAndServe(":8080", nil))
}

Background

Resourceful routes are a way to represent CRUD-manageable resources in a web application. They provide a conventional set of endpoints to manage application models. They are a standard interface for the Ruby on Rails framework.

Endpoints

The package implements a simple API, ignoring the HTML form endpoints from Rails-style resourceful routes.

HTTP Method URI Description
GET /resource Index: list all instances.
POST /resource Create: make a new instance.
GET /resource/{id} Show: get a specific instance.
PUT /resource/{id} Update: change a specific instance.
DELETE /resource/{id} Destroy: delete a specific instance.

Documentation

Overview

Package resourceful provides an implementation of resourceful routing. It simplifies setting up RESTful endpoints and providing handlers.

Background

Resourceful routes are a way to represent CRUD-manageable resources in a web application. They provide a conventional set of endpoints to manage application models. They are a standard interface for the Ruby on Rails framework.

Endpoints

The package implements a simple API, ignoring the HTML form endpoints from Rails-style resourceful routes.

HTTP Method  URI             Description
GET          /resource       Index: list all instances.
POST         /resource       Create: make a new instance.
GET          /resource/{id}  Show: get a specific instance.
PUT          /resource/{id}  Update: change a specific instance.
DELETE       /resource/{id}  Destroy: delete a specific instance.

Handlers

The package provides a ResourceHandlers implementation in a struct HandlerFuncs which responds with a 404 on all endpoints. The endpoints can be overridden individually.

If the application implements ResourceHandlers, only a single-process architecture can hold complex state in memory between calls. Since scalable web applications will have to shard work across multiple processes and implement cheap recovery when a process goes down, this is discouraged. Instead, maintain only basic state like a database connection and load per-request data separately on each request from a memcache or persistent store.

Routers

A router is created by NewResourceRouter, and uses gorilla/mux to do URI and HTTP method matching. Resources are added with AddResource, which takes a resource name and ResourceHandlers to build the routes. For endpoints that use a specific instance, the resource ids for a request can be extracted within a handler with ResourceId(resource_name, request).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResourceId

func ResourceId(resourceName string, r *http.Request) string

Get the id of the named resource for the given request. If the resource was not present on the requested route, the empty string is returned.

Types

type HandlerFuncs

type HandlerFuncs struct {
	IndexFunc   func(http.ResponseWriter, *http.Request)
	CreateFunc  func(http.ResponseWriter, *http.Request)
	ShowFunc    func(http.ResponseWriter, *http.Request)
	UpdateFunc  func(http.ResponseWriter, *http.Request)
	DestroyFunc func(http.ResponseWriter, *http.Request)
}

A basic implementation of ResourceHandlers which responds with a not found error on all endpoints. Each action can be overridden independently.

func (HandlerFuncs) Create

func (f HandlerFuncs) Create(w http.ResponseWriter, r *http.Request)

func (HandlerFuncs) Destroy

func (f HandlerFuncs) Destroy(w http.ResponseWriter, r *http.Request)

func (HandlerFuncs) Index

func (f HandlerFuncs) Index(w http.ResponseWriter, r *http.Request)

func (HandlerFuncs) Show

func (HandlerFuncs) Update

func (f HandlerFuncs) Update(w http.ResponseWriter, r *http.Request)

type ResourceHandlers

type ResourceHandlers interface {
	// List all instances.
	Index(http.ResponseWriter, *http.Request)
	// Create a new instance.
	Create(http.ResponseWriter, *http.Request)
	// Show a specific instance.
	Show(http.ResponseWriter, *http.Request)
	// Update a specific instance.
	Update(http.ResponseWriter, *http.Request)
	// Delete an instance.
	Destroy(http.ResponseWriter, *http.Request)
}

ResourceHandlers define the methods to call for each action on a resource.

type ResourceRouter

type ResourceRouter struct {
	// contains filtered or unexported fields
}

An http.Handler which serves resourceful routes, created with NewResourceRouter.

Example
router := NewResourceRouter()
router.AddResource("prefix", HandlerFuncs{
	ShowFunc: func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Show function for %v", ResourceId("prefix", r))
	},
})
router.AddResource("resource_2", HandlerFuncs{
	IndexFunc: func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Index for resource_2")
	},
})

w := httptest.NewRecorder()
// Show
r, err := http.NewRequest("GET", "/prefix/42a", nil)
if err != nil {
	log.Fatal(err)
}

router.ServeHTTP(w, r)
fmt.Println(w.Body.String())

w = httptest.NewRecorder()
// Index
r, err = http.NewRequest("GET", "/resource_2", nil)
if err != nil {
	log.Fatal(err)
}

router.ServeHTTP(w, r)
fmt.Println(w.Body.String())
Output:

Show function for 42a
Index for resource_2

func NewResourceRouter

func NewResourceRouter() *ResourceRouter

Create a new ResourceRouter.

func (*ResourceRouter) AddResource

func (router *ResourceRouter) AddResource(resourceName string, handlers ResourceHandlers) *ResourceRouter

Add a resource to a router. The router directs RESTful resource actions to the handlers defined by the provided interface. This returns the modified router to allow chained calls.

func (*ResourceRouter) ServeHTTP

func (router *ResourceRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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