-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend-hunchentoot.lisp
111 lines (89 loc) · 3.59 KB
/
backend-hunchentoot.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
;;
;; RoL-server - Rails on Lisp application server
;;
;; Copyright 2012-2015 Thomas de Grivel <[email protected]>
;;
;; Permission to use, copy, modify, and distribute this software for any
;; purpose with or without fee is hereby granted, provided that the above
;; copyright notice and this permission notice appear in all copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(in-package :RoL-server)
;; Config
(setf hunchentoot:*hunchentoot-default-external-format* :utf8)
(setf hunchentoot:*log-lisp-errors-p* t)
(setf hunchentoot:*log-lisp-backtraces-p* t)
(setf hunchentoot:*log-lisp-warnings-p* t)
(setf hunchentoot:*show-lisp-errors-p* t)
(setf hunchentoot:*catch-errors-p* nil)
;; Request
(defun backend-request-env ()
(list :headers (hunchentoot:headers-in*)
:get-parameters (hunchentoot:get-parameters*)
:post-parameters (hunchentoot:post-parameters*)
:cookies (hunchentoot:cookies-in*)))
(defun backend-request-method ()
(hunchentoot:request-method*))
(defun backend-request-header (name)
(hunchentoot:header-in* name))
(defun backend-request-uri ()
(hunchentoot:request-uri*))
(defun backend-request-remote-addr ()
(hunchentoot:remote-addr*))
;; Forms
(defun backend-read-request-data ()
(hunchentoot:raw-post-data))
(defun backend-read-form-data ()
(hunchentoot:post-parameters*))
;; Reply
(defun backend-status (&rest parts)
(let ((status-string (apply #'str parts)))
(setf (hunchentoot:return-code*) (parse-integer status-string
:junk-allowed t))))
(defun backend-header (name &rest parts)
(setf (hunchentoot:header-out name)
(if (and parts (null (cdr parts)) (integerp (car parts)))
(car parts) ;; pass single integer as-is
(apply #'str parts))))
(defun backend-send-headers ()
(with-output-to-string (out)
(mapc (lambda (h)
(format out "~A: ~A~A" (car h) (cdr h) +crlf+))
(hunchentoot:headers-out*))))
(defun set-cookie (name value expires &optional (domain *host*) (path "/")
secure (http-only t))
(hunchentoot:set-cookie (string name)
:value value
:expires expires
:path path
:domain domain
:secure secure
:http-only http-only))
(defun backend-send (content)
(babel:octets-to-string content))
;; Running
(defclass rol-acceptor (hunchentoot:acceptor)
())
(defmethod hunchentoot:acceptor-dispatch-request ((acceptor rol-acceptor)
request)
(format t "acceptor dispatch request~%")
(let ((hunchentoot:*catch-errors-p* (not (debug-p :reply)))
(*req* request))
(route-request)))
(defun backend-run ()
(log-msg :info "starting hunchentoot at 127.0.0.1:~A" *port*)
(hunchentoot:start (make-instance 'rol-acceptor
:address "127.0.0.1"
:port *port*))
(labels ((sleepy ()
(sleep 1)
(sleepy)))
(sleepy))
#+nil(error "hunchentoot server exited"))