-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rkt
236 lines (209 loc) · 8.25 KB
/
server.rkt
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
#lang racket
(require
css-expr
file/unzip
json
net/url
racket/match
racket/set
racket/string
web-server/servlet
web-server/servlet-env
"util.rkt"
"database.rkt"
"github.rkt"
"srfi-files.rkt")
;;;
(define web-port
(string->number (must-env "PORT")))
;;;
(define (web-text-bytes-response body)
(response/full 200 #"OK"
(current-seconds)
(string->bytes/utf-8 "text/plain; charset=utf-8")
empty
(list body)))
(define (web-html-bytes-response body)
(response/full 200 #"OK"
(current-seconds)
(string->bytes/utf-8 "text/html; charset=utf-8")
empty
(list body)))
;;
(define (web-error-response status-code status-text)
(response/full status-code
(string->bytes/utf-8 status-text)
(current-seconds)
(string->bytes/utf-8 "text/plain; charset=utf-8")
empty
(list (string->bytes/utf-8 status-text))))
(define (web-not-found req)
(web-error-response 404 "Not Found"))
(define (web-method-not-allowed req)
(web-error-response 405 "Method Not Allowed"))
(define (web-unauthorized req)
(web-error-response 403 "Forbidden"))
(define (repo-name->srfi-number repo-name)
(match (regexp-match #rx"^srfi-([0-9]+)$" repo-name)
((list _ srfi-number) (string->number srfi-number))
(else #f)))
(define (web-admin-github req)
(let* ((req-bytes (request-post-data/raw req)))
(cond ((not (github-sha1-match? req req-bytes))
(web-unauthorized req))
(else
(let ((j (bytes->jsexpr req-bytes)))
(display j (current-error-port))
(display #\newline (current-error-port))
(let* ((repository (hash-ref j 'repository))
(repo-name (hash-ref repository 'name))
(srfi-number (repo-name->srfi-number repo-name)))
(fprintf (current-error-port)
"The repo name is <~a>~n" repo-name)
(cond ((not srfi-number)
(fprintf (current-error-port)
"This is not a SRFI repo~n"))
(else
(fprintf (current-error-port)
"The repo SRFI number is <~a>~n" srfi-number)
(let* ((commit-hash (hash-ref j 'after))
(zip-url (github-zip-url repository commit-hash)))
(fprintf (current-error-port)
"The zip URL is <~a>~n" zip-url)
(fprintf (current-error-port)
"Reading zip...~n")
(call/input-url
(string->url zip-url)
(lambda (url) (get-pure-port url '() #:redirections 1))
(compose database-set-srfi-files!
derive-srfi-files
gather-srfi-files-from-zip-port)))))
(response/xexpr '(html (body (h1 "OK"))))))))))
(define (cleanup-srfi-title title)
(match (regexp-match #rx"^SRFI[ -][0-9]+: (.*)$" title)
((list _ real-title) real-title)
(else title)))
(define (parse-srfi-info info-sexp-bytes)
(let* ((raw (call-with-input-string (bytes->string/utf-8 info-sexp-bytes) read))
(alist (if (pair? raw) (cdr raw) '()))
(table (make-hash)))
(hash-set! table 'title
(cleanup-srfi-title (cadr (or (assoc 'title alist) '(title "")))))
(hash-set! table 'status "final")
table))
(define (srfi-html-url srfi-number)
(format "/file/srfi-~a.html" srfi-number))
(define (srfi-info-url srfi-number)
(format "/file/srfi-~a-info.scm" srfi-number))
(define (srfi-args-url srfi-number)
(format "/file/srfi-~a-args.scm" srfi-number))
(define (web-main-page req)
(let ((srfi-table (database-get-srfi-table)))
(response/xexpr
`(html
(head
(title "SRFI Status Dashboard")
(style
,(css-expr->css
(css-expr
[html #:font-family sans-serif]
[table #:border-collapse collapse]
[table td th
#:border (1px solid black)
#:padding 5px]
[td.srfi-number #:text-align center]
[td.srfi-status
#:text-align center
#:font-variant small-caps]
[td.srfi-status.draft
#:background-color lightyellow]
[td.srfi-status.draft::after
#:content "draft"]
[td.srfi-status.final
#:background-color lightgreen]
[td.srfi-status.final::after
#:content "final"]
[td.srfi-status.withdrawn
#:background-color lightblue]
[td.srfi-status.withdrawn::after
#:content "withdrawn"]
[td.volunteer-status
#:text-align center]
[td.volunteer-status.ok
#:background-color lightgreen]
;;[td.volunteer-status.ok::after
;; #:content "\u2714"]
[td.volunteer-status.pending
#:background-color lightyellow]
[td.volunteer-status.pending::after
#:content "\u2715"])))
(body
(h1 "SRFI Status Dashboard")
(table
(tr
(th ((colspan "2")) "SRFI")
(th "Status")
(th "Markup")
(th "Info")
(th "Args")
(th "Types"))
,@(map (lambda (srfi-number)
(let* ((srfi-files-1 (hash-ref srfi-table srfi-number))
(info (parse-srfi-info
(hash-ref srfi-files-1 "-info.scm" #""))))
`(tr
(td ((class "srfi-number"))
,(number->string srfi-number))
(td ,(hash-ref info 'title))
(td ((class ,(string-join
(list "srfi-status"
(hash-ref info 'status))))))
(td ((class ,(string-join '("volunteer-status" "ok"))))
(a ((href ,(srfi-html-url srfi-number))) "html"))
(td ((class ,(string-join '("volunteer-status" "ok"))))
(a ((href ,(srfi-info-url srfi-number))) "scm"))
(td ((class ,(string-join '("volunteer-status" "ok"))))
(a ((href ,(srfi-args-url srfi-number))) "scm"))
(td ((class ,(string-join '("volunteer-status"
"pending"))))))))
(sort (hash-keys srfi-table) <)))))))))
(define (web-api-srfi-file send-response srfi-number srfi-suffix)
(let ((contents (database-get-srfi-file srfi-number srfi-suffix)))
(if (not contents)
(web-error-response 404 "We don't have that SRFI or that metadata file")
(send-response contents))))
(define (web-file req filename)
(let ((we-dont "We don't have any filenames of that form"))
(match (regexp-match #rx"^srfi-(0|[1-9][0-9]*)([.-].*)$" filename)
((list _ srfi-number srfi-suffix)
(let ((srfi-number (string->number srfi-number)))
(case srfi-suffix
((".html")
(web-api-srfi-file web-html-bytes-response srfi-number ".html"))
(("-args.scm")
(web-api-srfi-file web-text-bytes-response srfi-number "-args.scm"))
(("-info.scm")
(web-api-srfi-file web-text-bytes-response srfi-number "-info.scm"))
(else
(web-error-response 404 we-dont)))))
(else
(web-error-response 404 we-dont)))))
(define-values (web-dispatch web-url)
(dispatch-rules
[("")
web-main-page]
[("file" (string-arg)) web-file]
[("admin" "github")
#:method "post"
web-admin-github]
[else
web-not-found]))
(define (web-serve)
(serve/servlet web-dispatch
#:port web-port
#:listen-ip #f
#:command-line? #t
#:servlet-path "/"
#:servlet-regexp #rx""))
(database-initialize)
(web-serve)