-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rkt
65 lines (57 loc) · 2.22 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
#lang racket/base
;===============================================================================
;
; Copyright (c) 2020 Jérôme Martin <rilouw.eu>
; The code for this project is released under the terms of the GNU AGPLv3.
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU Affero General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU Affero General Public License for more details.
;
; You should have received a copy of the GNU Affero General Public License
; along with this program. If not, see <https://www.gnu.org/licenses/>.
;
;===============================================================================
(provide response-poem)
(require
racket/path
racket/sandbox
racket/string
web-server/http
web-galaxy/response
racket-poetry/private/assembler)
(define (format-assembly ast)
(map
(lambda (line)
(map (lambda (word)
(if (symbol? word) (symbol->string word) word))
line))
ast))
(define ((error-handler message) e)
(hasheq 'error (string-append message ": " (exn-message e))))
(define (sandbox-eval text)
(with-handlers ([exn:fail:contract:arity? (error-handler "Incorrect number of arguments")]
[exn? (error-handler "An unknown exception occured")])
(define evaluator (make-module-evaluator (string-append "#lang racket-poetry\n" text "\n")))
(evaluator '(require 'poem-file))
(define ast (evaluator 'poem))
(hasheq
'binary (assemble ast)
'assembly (format-assembly ast))))
(define-response (poem)
(define raw (request-post-data/raw req))
(define text (bytes->string/utf-8 raw))
(define result (sandbox-eval text))
(response/json result))
(module+ main
(require web-galaxy/serve)
(define static-path (path->string (build-path (current-server-root-path) "static")))
(parameterize ([current-server-static-paths (list static-path)])
(serve/all
[POST ("") response-poem])))