forked from rs/rest-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
142 lines (130 loc) · 3.94 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package rest_layer
import (
"log"
"net/http"
"net/url"
"github.com/rs/cors"
"github.com/rs/rest-layer-mem"
"github.com/rs/rest-layer/resource"
"github.com/rs/rest-layer/rest"
"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
index := resource.NewIndex()
// Add a resource on /users[/:user_id]
users := index.Bind("users", resource.New(user, mem.NewHandler(), resource.Conf{
// We allow all REST methods
// (rest.ReadWrite is a shortcut for []rest.Mode{Create, Read, Update, Delete, List})
AllowedModes: resource.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", resource.New(post, mem.NewHandler(), resource.Conf{
// Posts can only be read, created and deleted, not updated
AllowedModes: []resource.Mode{resource.Read, resource.List, resource.Create, resource.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(index)
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)
}
}