-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
201 lines (186 loc) · 5.64 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//go:build go1.7
// +build go1.7
package main
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"time"
"github.com/clarify/rested/resource"
"github.com/clarify/rested/resource/testing/mem"
"github.com/clarify/rested/rest"
"github.com/clarify/rested/schema"
"github.com/justinas/alice"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
)
var (
user = schema.Schema{
Fields: schema.Fields{
"id": {
Required: true,
ReadOnly: true,
Filterable: true,
Sortable: true,
Validator: &schema.String{
Regexp: "^[0-9a-z_-]{2,150}$",
},
},
"created": schema.CreatedField,
"updated": schema.UpdatedField,
"name": {},
"ip": {Validator: &schema.IP{StoreBinary: true}},
"password": schema.PasswordField,
},
}
postFollower = schema.Schema{
Fields: schema.Fields{
"id": schema.IDField,
"post": {
Validator: &schema.Reference{Path: "posts"},
},
"user": {
Filterable: true,
Sortable: true,
Validator: &schema.Reference{Path: "users"},
},
},
}
post = schema.Schema{
Fields: schema.Fields{
"id": schema.IDField,
"created": schema.CreatedField,
"updated": schema.UpdatedField,
"user": {
Validator: &schema.Reference{Path: "users"},
},
"thumbnail_url": {
Params: schema.Params{
"width": {
Validator: schema.Integer{
Boundaries: &schema.Boundaries{Max: 1000},
},
},
"height": {
Validator: schema.Integer{
Boundaries: &schema.Boundaries{Max: 1000},
},
},
},
// Appends a "w" and/or "h" query string parameter(s) to the value (URL) if width or height params passed
Handler: func(ctx context.Context, value interface{}, params map[string]interface{}) (interface{}, error) {
str, ok := value.(string)
if !ok {
return nil, errors.New("not a string")
}
sep := "?"
if strings.IndexByte(str, '?') > 0 {
sep = "&"
}
if width, found := params["width"]; found {
str = fmt.Sprintf("%s%sw=%d", str, sep, width)
sep = "&"
}
if height, found := params["height"]; found {
str = fmt.Sprintf("%s%sy=%d", str, sep, height)
}
return str, nil
},
},
"meta": {
Schema: &schema.Schema{
Fields: schema.Fields{
"title": {},
"body": {},
},
},
},
},
}
)
func main() {
index := resource.NewIndex()
index.Bind("users", user, mem.NewHandler(), resource.Conf{
AllowedModes: resource.ReadWrite,
})
posts := index.Bind("posts", post, mem.NewHandler(), resource.Conf{
AllowedModes: resource.ReadWrite,
})
posts.Bind("followers", "post", postFollower, mem.NewHandler(), resource.Conf{
AllowedModes: resource.ReadWrite,
})
// Create API HTTP handler for the resource graph
api, err := rest.NewHandler(index)
if err != nil {
log.Fatal().Err(err).Msg("Invalid API configuration")
}
// Setup logger
c := alice.New()
c = c.Append(hlog.NewHandler(log.With().Logger()))
c = c.Append(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
hlog.FromRequest(r).Info().
Str("method", r.Method).
Str("url", r.URL.String()).
Int("status", status).
Int("size", size).
Dur("duration", duration).
Msg("")
}))
c = c.Append(hlog.RequestHandler("req"))
c = c.Append(hlog.RemoteAddrHandler("ip"))
c = c.Append(hlog.UserAgentHandler("ua"))
c = c.Append(hlog.RefererHandler("ref"))
c = c.Append(hlog.RequestIDHandler("req_id", "Request-Id"))
resource.LoggerLevel = resource.LogLevelDebug
resource.Logger = func(ctx context.Context, level resource.LogLevel, msg string, fields map[string]interface{}) {
zerolog.Ctx(ctx).WithLevel(zerolog.Level(level)).Fields(fields).Msg(msg)
}
// Bind the API under the root path
http.Handle("/", c.Then(api))
// Inject some fixtures
fixtures := [][]string{
{"PUT", "/users/johndoe", `{"name": "John Doe", "ip": "1.2.3.4", "password": "secret"}`},
{"PUT", "/users/fan1", `{"name": "Fan 1", "ip": "1.2.3.4", "password": "secret"}}`},
{"PUT", "/users/fan2", `{"name": "Fan 2", "ip": "1.2.3.4", "password": "secret"}}`},
{"PUT", "/users/fan3", `{"name": "Fan 3", "ip": "1.2.3.4", "password": "secret"}}`},
{"PUT", "/users/fan4", `{"name": "Fan 4", "ip": "1.2.3.4", "password": "secret"}}`},
{"PUT", "/posts/ar5qrgukj5l7a6eq2ps0",
`{
"user": "johndoe",
"thumbnail_url": "http://dom.com/image.png",
"meta": {
"title": "First Post",
"body": "This is my first post"
}
}`},
{"POST", "/posts/ar5qrgukj5l7a6eq2ps0/followers", `{"user": "fan1"}`},
{"POST", "/posts/ar5qrgukj5l7a6eq2ps0/followers", `{"user": "fan2"}`},
{"POST", "/posts/ar5qrgukj5l7a6eq2ps0/followers", `{"user": "fan3"}`},
}
for _, fixture := range fixtures {
req, err := http.NewRequest(fixture[0], fixture[1], strings.NewReader(fixture[2]))
if err != nil {
log.Fatal().Err(err).Msg("")
}
w := httptest.NewRecorder()
api.ServeHTTP(w, req)
if w.Code >= 400 {
log.Fatal().Msgf("Error returned for `%s %s`: %v", fixture[0], fixture[1], w)
}
}
// Serve it
println("Serving API on http://localhost:8080")
println("Play with (httpie):\n",
"- http :8080/posts fields=='id,thumb_s_url:thumbnail_url(height:80)'\n",
"- http :8080/posts fields=='i:id,m:meta{t:title,b:body},thumb_small_url:thumbnail_url(height:80)'\n",
"- http :8080/posts fields=='id,meta,user{id,name}'\n",
"- http :8080/posts/ar5qrgukj5l7a6eq2ps0/followers fields=='post{id,meta{title}},user{id,name}'\n",
"- http :8080/posts/ar5qrgukj5l7a6eq2ps0 fields=='id,meta{title},followers(limit:2){user{id,name}}'")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal().Err(err).Msg("")
}
}