-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update documentation, add some examples and unexport some types
- Loading branch information
Olivier Poitrey
committed
Aug 7, 2015
1 parent
2c1abb4
commit c899247
Showing
20 changed files
with
358 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
Package rest layer is a REST API framework heavily inspired by the excellent | ||
Python Eve (http://python-eve.org). | ||
It lets you automatically generate a comprehensive, customizable, and secure | ||
REST API on top of any backend storage with no boiler plate code. You can focus | ||
on your business logic now. | ||
Implemented as a `net/http` middleware, it plays well with other middlewares like | ||
CORS (http://github.com/rs/cors). | ||
REST Layer is an opinionated framework. Unlike many web frameworks, you don't | ||
directly control the routing. You just expose resources and sub-resources, the | ||
framework automatically figures what routes to generate behind the scene. | ||
You don't have to take care of the HTTP headers and response, JSON encoding, etc. | ||
either. rest handles HTTP conditional requests, caching, integrity checking for | ||
you. A powerful and extensible validation engine make sure that data comes | ||
pre-validated to you resource handlers. Generic resource handlers for MongoDB and | ||
other databases are also available so you have few to no code to write to make | ||
the whole system work. | ||
*/ | ||
package rest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package rest_test | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/rs/cors" | ||
"github.com/rs/rest-layer" | ||
"github.com/rs/rest-layer-mem" | ||
"github.com/rs/rest-layer/schema" | ||
) | ||
|
||
func Example() { | ||
var ( | ||
// Define a user resource schema | ||
user = schema.Schema{ | ||
"id": schema.Field{ | ||
Required: true, | ||
// When a field is read-only, on default values or hooks can | ||
// set their value. The client can't change it. | ||
ReadOnly: true, | ||
// This is a field hook called when a new user is created. | ||
// The schema.NewID hook is a provided hook to generate a | ||
// unique id when no value is provided. | ||
OnInit: &schema.NewID, | ||
// The Filterable and Sortable allows usage of filter and sort | ||
// on this field in requests. | ||
Filterable: true, | ||
Sortable: true, | ||
Validator: &schema.String{ | ||
Regexp: "^[0-9a-f]{32}$", | ||
}, | ||
}, | ||
"created": schema.Field{ | ||
Required: true, | ||
ReadOnly: true, | ||
Filterable: true, | ||
Sortable: true, | ||
OnInit: &schema.Now, | ||
Validator: &schema.Time{}, | ||
}, | ||
"updated": schema.Field{ | ||
Required: true, | ||
ReadOnly: true, | ||
Filterable: true, | ||
Sortable: true, | ||
OnInit: &schema.Now, | ||
// The OnUpdate hook is called when the item is edited. Here we use | ||
// provided Now hook which just return the current time. | ||
OnUpdate: &schema.Now, | ||
Validator: &schema.Time{}, | ||
}, | ||
// Define a name field as required with a string validator | ||
"name": schema.Field{ | ||
Required: true, | ||
Filterable: true, | ||
Validator: &schema.String{ | ||
MaxLen: 150, | ||
}, | ||
}, | ||
} | ||
|
||
// Define a post resource schema | ||
post = schema.Schema{ | ||
// schema.*Field are shortcuts for common fields (identical to users' same fields) | ||
"id": schema.IDField, | ||
"created": schema.CreatedField, | ||
"updated": schema.UpdatedField, | ||
// Define a user field which references the user owning the post. | ||
// See bellow, the content of this field is enforced by the fact | ||
// that posts is a sub-resource of users. | ||
"user": schema.Field{ | ||
Required: true, | ||
Filterable: true, | ||
Validator: &schema.Reference{ | ||
Path: "users", | ||
}, | ||
}, | ||
"public": schema.Field{ | ||
Filterable: true, | ||
Validator: &schema.Bool{}, | ||
}, | ||
// Sub-documents are handled via a sub-schema | ||
"meta": schema.Field{ | ||
Schema: &schema.Schema{ | ||
"title": schema.Field{ | ||
Required: true, | ||
Validator: &schema.String{ | ||
MaxLen: 150, | ||
}, | ||
}, | ||
"body": schema.Field{ | ||
Validator: &schema.String{ | ||
MaxLen: 100000, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
) | ||
|
||
// Create a REST API root resource | ||
root := rest.New() | ||
|
||
// Add a resource on /users[/:user_id] | ||
users := root.Bind("users", rest.NewResource(user, mem.NewHandler(), rest.Conf{ | ||
// We allow all REST methods | ||
// (rest.ReadWrite is a shortcut for []rest.Mode{Create, Read, Update, Delete, List}) | ||
AllowedModes: rest.ReadWrite, | ||
})) | ||
|
||
// Bind a sub resource on /users/:user_id/posts[/:post_id] | ||
// and reference the user on each post using the "user" field of the posts resource. | ||
posts := users.Bind("posts", "user", rest.NewResource(post, mem.NewHandler(), rest.Conf{ | ||
// Posts can only be read, created and deleted, not updated | ||
AllowedModes: []rest.Mode{rest.Read, rest.List, rest.Create, rest.Delete}, | ||
})) | ||
|
||
// Add a friendly alias to public posts | ||
// (equivalent to /users/:user_id/posts?filter={"public":true}) | ||
posts.Alias("public", url.Values{"filter": []string{"{\"public\"=true}"}}) | ||
|
||
// Create API HTTP handler for the resource graph | ||
api, err := rest.NewHandler(root) | ||
if err != nil { | ||
log.Fatalf("Invalid API configuration: %s", err) | ||
} | ||
|
||
// Add cors support | ||
h := cors.New(cors.Options{OptionsPassthrough: true}).Handler(api) | ||
|
||
// Bind the API under /api/ path | ||
http.Handle("/api/", http.StripPrefix("/api/", h)) | ||
|
||
// Serve it | ||
log.Print("Serving API on http://localhost:8080") | ||
if err := http.ListenAndServe(":8080", nil); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.