-
Notifications
You must be signed in to change notification settings - Fork 29
/
rest.lisp
409 lines (370 loc) · 16.4 KB
/
rest.lisp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
(in-package :graph-db)
(defvar *rest-port* 8080)
(defvar *rest-app* nil)
(defvar *clack-app* nil)
(defvar *rest-procedures*
#+sbcl (make-hash-table :synchronized t :test 'equalp)
#+lispworks (make-hash-table :single-thread nil :test 'equalp)
#+ccl (make-hash-table :shared t :test 'equalp))
(defvar *rest-passwd-file* "rpasswd")
(defvar *htpasswd-bin* "/usr/bin/htpasswd")
(defun add-rest-user (username password)
(trivial-shell:shell-command
(format nil "~A -b ~A ~A ~A ~A"
*htpasswd-bin*
(if (probe-file *rest-passwd-file*) "" "-c")
*rest-passwd-file*
username
password)))
(defun delete-rest-user (username)
(trivial-shell:shell-command
(format nil "~A -D ~A ~A"
*htpasswd-bin*
*rest-passwd-file*
username)))
(defun auth-rest-user (username password)
(with-open-file (stream *rest-passwd-file*)
(do ((line (read-line stream nil :eof)
(read-line stream nil :eof)))
((eql line :eof))
(let ((pair (cl-ppcre:split "\\:" line)))
(when (equalp (first pair) username)
(destructuring-bind (_ hash salt enc)
(cl-ppcre:split "\\$" (second pair))
(declare (ignore _ enc))
(multiple-value-bind (out err exit-code)
(trivial-shell:shell-command
(format nil "openssl passwd -~A -salt ~A ~A"
hash salt password))
(declare (ignore exit-code err))
(setq out (cl-ppcre:regex-replace-all "\\s+$" out ""))
(log:warn "GOT ~A FOR ~A" out (second pair))
(return-from auth-rest-user
(equalp out (second pair))))))))))
#|
;; This only works with newer versions of htpasswd; see openssl version above
(multiple-value-bind (out err exit-code)
(trivial-shell:shell-command
(format nil "~A -vb ~A ~A ~A"
*htpasswd-bin*
*rest-passwd-file*
username
password))
(declare (ignore out err))
(eq 0 exit-code)))
|#
(defmacro with-rest-auth ((username password) &body body)
`(if (auth-rest-user ,username ,password)
(progn
,@body)
(progn
(setf (lack.response:response-status ningle:*response*) 401)
(json:encode-json-to-string
(list
(cons :error "Invalid credentials"))))))
(defmacro with-rest-graph ((graph-name) &body body)
`(let ((*graph* (lookup-graph (intern (json:camel-case-to-lisp ,graph-name) :keyword))))
(if (graph-p *graph*)
(progn
,@body)
(progn
(setf (lack.response:response-status ningle:*response*) 404)
(json:encode-json-to-string
(list
(cons :error
(format nil "Unknown graph ~A" ,graph-name))))))))
(defmacro with-rest-vertex ((id) &body body)
`(let ((vertex (lookup-vertex ,id)))
(if vertex
(progn
,@body)
(progn
(setf (lack.response:response-status ningle:*response*) 404)
(json:encode-json-to-string
(list
(cons :error
(format nil "Unknown vertex ~A" ,id))))))))
(defmacro with-rest-edge ((id) &body body)
`(let ((edge (lookup-edge ,id)))
(if edge
(progn
,@body)
(progn
(setf (lack.response:response-status ningle:*response*) 404)
(json:encode-json-to-string
(list
(cons :error
(format nil "Unknown edge ~A" ,id))))))))
(defmethod json-encode ((edge edge))
(let* ((class (class-of edge))
(class-name (class-name class)))
(json-rpc::encode-json-to-string
(nconc
(list (cons :id (string-id edge))
(cons :type (json:lisp-to-camel-case
(symbol-name class-name)))
(cons :from (string-id (from edge)))
(cons :to (string-id (to edge))))
(mapcar (lambda (slot-name)
(cons slot-name (slot-value edge slot-name)))
(data-slots (find-class class-name)))))))
(defmethod json-encode-edge-list (seq)
(json-rpc::encode-json-to-string
(map 'list
(lambda (edge)
(let* ((class (class-of edge))
(class-name (class-name class)))
(nconc
(list (cons :id (string-id edge))
(cons :type (json:lisp-to-camel-case
(symbol-name class-name)))
(cons :from (string-id (from edge)))
(cons :to (string-id (to edge))))
(mapcar (lambda (slot-name)
(cons slot-name (slot-value edge slot-name)))
(data-slots (find-class class-name))))))
seq)))
(defmethod json-encode ((vertex vertex))
(let* ((class (class-of vertex))
(class-name (class-name class)))
(json-rpc::encode-json-to-string
(nconc
(list (cons :id (string-id vertex))
(cons :type (json:lisp-to-camel-case
(symbol-name class-name))))
(mapcar (lambda (slot-name)
(cons slot-name (slot-value vertex slot-name)))
(data-slots (find-class class-name)))))))
(defmethod json-encode ((graph graph))
(json-rpc::encode-json-to-string
(list
(cons :name (graph-name graph))
(cons :type :graph)
(cons :mode (if (typep graph 'slave-graph)
:read-only
:read-write))
(cons :vertex-types
(loop
for key being the hash-keys
in (gethash :vertex (schema-type-table (schema graph)))
using (hash-value type-definition)
if (numberp key)
collecting
(list (cons :name (node-type-name type-definition))
(cons :slots
(mapcar
(lambda (slot-def)
(list (cons :name (first slot-def))
(cons :type
(let ((type-pos (position :type slot-def)))
(if type-pos
(nth (1+ type-pos) slot-def)
:any)))))
(node-type-slots type-definition))))))
(cons :edge-types
(loop
for key being the hash-keys
in (gethash :edge (schema-type-table (schema graph)))
using (hash-value type-definition)
if (numberp key)
collecting
(list (cons :name (node-type-name type-definition))
(cons :slots
(mapcar
(lambda (slot-def)
(list (cons :name (first slot-def))
(cons :type
(let ((type-pos (position :type slot-def)))
(if type-pos
(nth (1+ type-pos) slot-def)
:any)))))
(node-type-slots type-definition)))))))))
(defun get-param (params name)
(cdr (assoc name params :test 'equalp)))
(defmacro def-rest-procedure (name lambda-list &body body)
(with-gensyms (params)
`(let ((fn (lambda (,params)
(let (,@(mapcar (lambda (var)
(list var
`(get-param ,params
,(json:lisp-to-camel-case
(symbol-name var)))))
lambda-list))
(progn
,@body)))))
(setf (gethash ,name *rest-procedures*) fn)
(setf (gethash ,(json:lisp-to-camel-case (symbol-name name)) *rest-procedures*) fn))))
(defun call-rest-procedure (name params)
(with-rest-auth ((get-param params "username") (get-param params "password"))
(let ((fn (gethash name *rest-procedures*)))
(if (functionp fn)
(with-rest-graph ((get-param params :graph-name))
(funcall fn params))
(progn
(setf (lack.response:response-status ningle:*response*) 404)
(json:encode-json-to-string
(list
(cons :error
(format nil "Unknown procedure '~A'" name)))))))))
(defun rest-get-graph (params)
(with-rest-auth ((get-param params "username") (get-param params "password"))
(with-rest-graph ((get-param params :graph-name))
(json-encode *graph*))))
(defun rest-get-vertex (params)
(with-rest-auth ((get-param params "username") (get-param params "password"))
(with-rest-graph ((get-param params :graph-name))
(with-rest-vertex ((get-param params :node-id))
(json-encode vertex)))))
(defun rest-post-vertex (params)
(with-rest-auth ((get-param params "username") (get-param params "password"))
(with-rest-graph ((get-param params :graph-name))
(let* ((type-name (get-param params :type))
(type (lookup-node-type-by-name
(intern (json:camel-case-to-lisp type-name) :keyword)
:vertex
:graph *graph*)))
(if type
(let* ((class (find-class (node-type-name type)))
(slots (mapcar (lambda (slot)
(intern (symbol-name slot) :keyword))
(data-slots class)))
(constructor (node-type-constructor type))
(slot-args (flatten
(mapcar (lambda (slot)
(list slot
(cdr (assoc (json:lisp-to-camel-case
(symbol-name slot))
params
:test 'string-equal))))
slots)))
(node (apply constructor slot-args)))
(json-encode node))
(json:encode-json-to-string
(list
(cons :error
(format nil "Unknown vertex type ~A" type-name)))))))))
(defun rest-put-vertex (params)
(with-rest-auth ((get-param params "username") (get-param params "password"))
(with-rest-graph ((get-param params :graph-name))
(with-rest-vertex ((get-param params :node-id))
(with-transaction ()
(let ((slots (data-slots (class-of vertex)))
(new-vertex (copy vertex)))
(dolist (slot slots)
(let ((kv (assoc (json:lisp-to-camel-case
(symbol-name slot))
params
:test 'string-equal)))
(when kv
(setf (slot-value new-vertex slot)
(cdr kv)))))
(json-encode (save new-vertex))))))))
(defun rest-delete-vertex (params)
(with-rest-auth ((get-param params "username") (get-param params "password"))
(with-rest-graph ((get-param params :graph-name))
(with-rest-vertex ((get-param params :node-id))
(mark-deleted vertex)))))
(defun rest-get-edge (params)
(with-rest-auth ((get-param params "username") (get-param params "password"))
(with-rest-graph ((get-param params :graph-name))
(with-rest-edge ((get-param params :node-id))
(json-encode edge)))))
(defun rest-post-edge (params)
(with-rest-auth ((get-param params "username") (get-param params "password"))
(with-rest-graph ((get-param params :graph-name))
(let* ((type-name (get-param params :type))
(type (lookup-node-type-by-name
(intern (json:camel-case-to-lisp type-name) :keyword)
:edge
:graph *graph*)))
(if type
(let* ((class (find-class (node-type-name type)))
(slots (mapcar (lambda (slot)
(intern (symbol-name slot) :keyword))
(data-slots class)))
(constructor (node-type-constructor type))
(from (lookup-vertex (cdr (assoc "from" params :test 'string-equal))))
(to (lookup-vertex (cdr (assoc "to" params :test 'string-equal))))
(slot-args (flatten
(mapcar (lambda (slot)
(list slot
(cdr (assoc (json:lisp-to-camel-case
(symbol-name slot))
params
:test 'string-equal))))
slots))))
(if (and from to)
(let ((node (apply constructor :from from :to to slot-args)))
(json-encode node))
(json:encode-json-to-string
(list
(cons :error
"You must provide both FROM and TO vertices")))))
(json:encode-json-to-string
(list
(cons :error
(format nil "Unknown edge type ~A" type-name)))))))))
(defun rest-put-edge (params)
(with-rest-auth ((get-param params "username") (get-param params "password"))
(with-rest-graph ((get-param params :graph-name))
(with-rest-edge ((get-param params :node-id))
(with-transaction ()
(let ((slots (data-slots (class-of edge)))
(new-edge (copy edge)))
(dolist (slot slots)
(let ((kv (assoc (json:lisp-to-camel-case
(symbol-name slot))
params
:test 'string-equal)))
(when kv
(setf (slot-value new-edge slot)
(cdr kv)))))
(json-encode (save new-edge))))))))
(defun rest-delete-edge (params)
(with-rest-auth ((get-param params "username") (get-param params "password"))
(with-rest-graph ((get-param params :graph-name))
(with-rest-edge ((get-param params :node-id))
(mark-deleted edge)))))
(defun rest-list-edges (params)
(with-rest-auth ((get-param params "username") (get-param params "password"))
(with-rest-graph ((get-param params :graph-name))
(with-rest-vertex ((get-param params :node-id))
(json-encode-edge-list
(nconc (map-edges 'identity *graph*
:direction :out
:collect-p t
:vertex vertex)
(map-edges 'identity *graph*
:direction :in
:collect-p t
:vertex vertex)))))))
(defun start-rest (&optional (port *rest-port*))
(prog1
(setq *rest-app* (make-instance 'ningle:<app>))
(setf (ningle:route *rest-app* "/graph/:graph-name" :method :get)
'rest-get-graph
(ningle:route *rest-app* "/graph/:graph-name/vertex/:node-id" :method :get)
'rest-get-vertex
(ningle:route *rest-app* "/graph/:graph-name/vertex/:node-id/edges" :method :get)
'rest-list-edges
(ningle:route *rest-app* "/graph/:graph-name/vertex/:node-id" :method :delete)
'rest-delete-vertex
(ningle:route *rest-app* "/graph/:graph-name/vertex/:type" :method :post)
'rest-post-vertex
(ningle:route *rest-app* "/graph/:graph-name/vertex/:node-id" :method :put)
'rest-put-vertex
(ningle:route *rest-app* "/graph/:graph-name/edge/:node-id" :method :get)
'rest-get-edge
(ningle:route *rest-app* "/graph/:graph-name/edge/:node-id" :method :delete)
'rest-delete-edge
(ningle:route *rest-app* "/graph/:graph-name/edge/:type" :method :post)
'rest-post-edge
(ningle:route *rest-app* "/graph/:graph-name/edge/:node-id" :method :put)
'rest-put-edge
(ningle:route *rest-app* "/graph/:graph-name/procedure/:procedure-name" :method :post)
'(lambda (params)
(call-rest-procedure procedure-name params)))
(setq *clack-app* (clack:clackup *rest-app* :port port))))
(defun stop-rest (&optional (app *clack-app*))
(clack:stop app)
(setq *rest-app* nil))