-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpox-server.scm
220 lines (196 loc) · 8.97 KB
/
pox-server.scm
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
(module pox-server (make-pox-handler)
(import chicken scheme)
(use spiffy srfi-1 extras ports data-structures intarweb spiffy-request-vars
spiffy-uri-match pox-db/helpers pox-model downtime uri-common
irregex pox-auth spiffy-chain spiffy-auth spiffy-session pox-log pox-query)
(define-logger log server)
(define-log-category auth)
(define-log-category session)
(define-log-category request)
(define-log-category headers)
(include "web")
(http-status-codes
`((unprocessable-entity 422 . "Unprocessable Entity")
. ,(http-status-codes)))
(define (send-page id title content nav)
(send-sxml-response
`((head (title ,title))
(body
(div (@ (data-role "page") (id ,id))
(div (@ (data-role "header") (class "ui-bar"))
,nav
(h1 ,title))
(div (@ (data-role "content")) ,content))))))
(define (send-tasks-response select-tasks #!optional user)
(parameterize ((user-map (select-users)))
(with-request-vars* (request-vars source: 'query-string)
((group-by as-grouping)
(filter as-filter)
(ignore as-list-of-symbols)
(include-done as-boolean)
(skip as-list-of-symbols))
(let ((tasks (select-tasks group-by filter include-done)))
(if (not tasks)
(send-response status: 'not-found body: "Not Found")
(http-accept-case (current-request)
((application/json)
(send-json-response (task-list->json-serializable tasks)))
((text/x-downtime)
(send-response headers: '((content-type #(text/x-downtime ((charset . "utf-8")))))
body: (task-list->string
tasks
user
(request-uri (current-request))
ignore
skip)))
((text/html)
(send-page "tasks"
(if user
`("Tasks of " ,user)
"Tasks")
`(div (@ (id "tasks") (data-role "collapsible-set"))
,(map (lambda (t)
`(div (@ (data-role "collapsible"))
(h3 ,(alist-ref 'name t))
,(and-let* ((desc (alist-ref 'description t)))
(simple-format desc))
(div (@ (data-role "navbar"))
(ul (li (a (@ (href "/tasks/" ,(alist-ref 'id t) "/edit")
(data-iconpos "right")
(data-icon "gear"))
"Edit"))
(li (a (@ (href "/tasks" ,(alist-ref 'id t) "/delegate")
(data-iconpos "right")
(data-icon "forward"))
"Delegate"))
(li (a (@ (href "/tasks" ,(alist-ref 'id t) "/done")
(data-iconpos "right")
(data-icon "check"))
"Done"))))))
tasks))
`(a (@ (href "#login") (data-icon "back"))
"Change user")))))))))
(define (get-tasks continue)
(send-tasks-response select-tasks))
(define (get-user-tasks continue user)
(send-tasks-response
(lambda (group-by filter include-done)
(let* ((user (string->user-name user))
(tasks (select-user-tasks user group-by filter include-done)))
(if (or (not tasks) (null? tasks))
(and (db-select-one 'users 'name user 'id) '())
tasks)))
user))
(define (post-user-tasks continue user)
(parameterize ((user-map (select-users)))
(call/cc
(lambda (exit)
(or (and-let* ((headers (request-headers (current-request)))
(content-length (header-value 'content-length headers))
(body (read-string content-length (request-port (current-request))))
(tasks (http-accept-case (current-request)
((application/json)
;; the null list is the preamble; should add that to JSON as well, I guess
(cons '() (read-json-tasks body)))
((text/x-downtime)
(cdr (with-input-from-string body downtime-read)))))
(_ (unless (pair? (cdr tasks)) (exit #t))) ;; FIXME: not very pretty
(conflicts (persist-user-tasks (string->user-id user) tasks)))
(if (null? conflicts)
(send-response status: 'no-content)
(send-response status: 'conflict
body: (conflicts->string conflicts (string->user-name user)))))
(send-response status: 'internal-server-error
body: "Error handling input data"))))))
(session-cookie-name "pox-sid")
(get-session-id (lambda ()
(log (debug auth session) "checking cookie")
(or (get-session-id-from-cookie)
(let ((headers (request-headers (current-request))))
(and (eq? 'x-session (header-value 'authorization headers))
(header-param 'id 'authorization headers))))))
(define (post-session continue)
(with-request-vars* (request-vars source: 'request-body)
((user (nonempty as-string)) (password (nonempty as-string)))
(if (valid-credentials? user password)
(with-new-session
(lambda ()
(maybe-create-user user)
(session-set! 'user user)
(send-response status: 'created)))
(send-response status: 'unprocessable-entity body: "Invalid credentials"))))
(handle-exception
(lambda (exn chain)
(log-to (error-log) "~A" (build-error-message exn chain #t))
(let* ((message (get-condition-property exn 'exn 'message))
(args (get-condition-property exn 'exn 'arguments))
(message (cond ((null? args) message)
((null? (cdr args)) (sprintf "~A: ~S" message (car args)))
(else (sprintf "~A: ~S" message args)))))
(send-response status: 'internal-server-error body: message))))
(define (with-request-dump continue)
(log (debug request headers)
(cons 'headers (headers->list (request-headers (current-request)))))
(continue))
;; (define (wrap-handler . args)
;; (let ((args (reverse args))
;; (handler (car args)))
;; (log debug: foo: handler)
;; (lambda (continue)
;; (let loop ((wrappers (cdr args)))
;; (if (null? wrappers)
;; (handler continue)
;; ((car wrappers)
;; (lambda () (loop (cdr wrappers)))
;; continue))))))
;;; idea
;; (define handle-request
;; (wrap-handler
;; with-request-dump
;; (with-session)
;; (uri-match/spiffy
;; `(((/ "session")
;; (POST ,post-session))
;; (,with-authentication
;; ((/ "users")
;; ((/ (submatch (+ any)))
;; ((/ "tasks")
;; (GET ,get-user-tasks)
;; (POST ,post-user-tasks))))
;; ((/ "tasks")
;; (GET ,(lambda (continue)
;; (let ((user ((request-vars source: 'query-string) 'user)))
;; (if user
;; (get-user-tasks continue user)
;; (get-tasks continue)))))))))))
(define (with-authentication* handler)
(if (authentication-enabled?)
(chain with-authentication handler)
handler))
;; this a procedure to defer setting of parameters through user config
(define (make-request-handler)
(chain
with-request-dump
with-session
(uri-match/spiffy
`(((/ "session")
(POST ,post-session))
((/ "users")
((/ (submatch (+ any)))
((/ "tasks")
(GET ,(with-authentication* get-user-tasks))
(POST ,(with-authentication* post-user-tasks)))))
((/ "tasks")
(GET ,(with-authentication*
(lambda (continue)
(let ((user ((request-vars source: 'query-string) 'user)))
(if user
(get-user-tasks continue user)
(get-tasks continue)))))))))))
(define (make-pox-handler)
(let ((handle-request (make-request-handler)))
(lambda (continue)
(with-db-connection
(lambda ()
(handle-request continue))))))
)