-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (134 loc) · 3.02 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
package main
import (
"log"
"net/http"
"time"
"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"
"github.com/rs/xaccess"
"github.com/rs/xhandler"
"github.com/rs/xlog"
)
var (
user = schema.Schema{
Description: `The user object`,
Fields: schema.Fields{
"id": schema.IDField,
"created": schema.CreatedField,
"updated": schema.UpdatedField,
"name": {
Required: true,
Filterable: true,
Validator: &schema.String{
MaxLen: 100,
},
},
},
}
season = schema.Schema{
Description: `The season object`,
Fields: schema.Fields{
"id": schema.IDField,
"created": schema.CreatedField,
"updated": schema.UpdatedField,
"name": {
Required: true,
Filterable: true,
Validator: &schema.String{
MaxLen: 100,
},
},
},
}
song = schema.Schema{
Description: `The song object`,
Fields: schema.Fields{
"id": schema.IDField,
"created": schema.CreatedField,
"updated": schema.UpdatedField,
"user": {
Required: true,
Filterable: true,
Validator: &schema.Reference{
Path: "users",
},
},
"season": {
Required: true,
Filterable: true,
Validator: &schema.Reference{
Path: "seasons",
},
},
"name": {
Required: true,
Validator: &schema.String{
MaxLen: 100,
},
},
"url": {
Required: true,
Validator: &schema.String{
MaxLen: 4096,
},
},
},
}
vote = schema.Schema{
Description: `the vote object`,
Fields: schema.Fields{
"id": schema.IDField,
"created": schema.CreatedField,
"updated": schema.UpdatedField,
"auth": {
Required: true,
Filterable: true,
Validator: &schema.Reference{
Path: "users",
},
},
"song": {
Required: true,
Filterable: true,
Validator: &schema.Reference{
Path: "songs",
},
},
},
}
)
func main() {
// Create a REST API resource index
index := resource.NewIndex()
index.Bind("users", user, mem.NewHandler(), resource.Conf{
AllowedModes: resource.ReadWrite,
})
index.Bind("seasons", season, mem.NewHandler(), resource.Conf{
AllowedModes: resource.ReadWrite,
})
index.Bind("songs", song, mem.NewHandler(), resource.Conf{
AllowedModes: resource.ReadWrite,
})
// Create API HTTP handler for the resource graph
api, err := rest.NewHandler(index)
if err != nil {
log.Fatalf("Invalid API configuration: %s", err)
}
// Init a xhandler chain (see https://github.com/rs/xhandler)
c := xhandler.Chain{}
c.UseC(xhandler.CloseHandler)
c.UseC(xhandler.TimeoutHandler(2 * time.Second))
c.UseC(xlog.NewHandler(xlog.Config{}))
c.UseC(xaccess.NewHandler())
c.UseC(cors.New(cors.Options{OptionsPassthrough: true}).HandlerC)
// Bind the API under /api/ path
http.Handle("/api/", http.StripPrefix("/api/", c.Handler(api)))
// Serve it
log.Print("Serving API on http://localhost:8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}