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 ¶
- func ResourceId(resourceName string, r *http.Request) string
- type HandlerFuncs
- func (f HandlerFuncs) Create(w http.ResponseWriter, r *http.Request)
- func (f HandlerFuncs) Destroy(w http.ResponseWriter, r *http.Request)
- func (f HandlerFuncs) Index(w http.ResponseWriter, r *http.Request)
- func (f HandlerFuncs) Show(w http.ResponseWriter, r *http.Request)
- func (f HandlerFuncs) Update(w http.ResponseWriter, r *http.Request)
- type ResourceHandlers
- type ResourceRouter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 (f HandlerFuncs) Show(w http.ResponseWriter, r *http.Request)
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 (*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)