/
tips-tricks
354 lines (252 loc) · 9.27 KB
/
tips-tricks
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
=== Tips and tricks
==== Reload applications' code on every request
You can use {{add-request-handler-hook!}} to reload the applications'
code every time a request is handled by awful. Here's a simple
example:
<enscript highlight=scheme>
(use awful)
(add-request-handler-hook!
'reload-on-request
(lambda (path handler)
(reload-apps (awful-apps))
(handler)))
(define-page (main-page-path)
(lambda ()
"1"))
</enscript>
==== Use {{link}} and {{form}} for links and forms
Instead of using {{<a>}} and {{<form>}} for creating links and forms,
respectively, consider using the {{link}} and {{form}} procedures
instead. They are specially useful when using sessions, since they
transparently handle the session identifier for you. Even if you
don't use sessions, they may save you some work if one day you decide
to use sessions (then you won't have do to anything regarding to links
and forms).
==== Use {{with-request-variables}} when referencing the same request variable multiple times
When you need to access the same request variable more than once,
consider using {{with-request-variables}}.
For example, instead of:
<enscript highlight=scheme>
(use awful)
(enable-sxml #t)
(define-session-page "save-and-show-user"
(lambda ()
($session-set! 'user ($ 'user))
`("Welcome " ,($ 'user "nobody") "!")))
</enscript>
consider using something like:
<enscript highlight=scheme>
(use awful)
(enable-sxml #t)
(define-session-page "save-and-show-user"
(lambda ()
(with-request-variables (user)
($session-set! 'user user)
`("Welcome " ,(or user "nobody") "!"))))
</enscript>
==== Use the web REPL and the session inspector for debugging
You can simply use the {{--development-mode}} option for the awful
application server to enable the web REPL and the session inspector
(when {{enable-session}} is {{#t}}). The development mode allows
access to them for the localhost. When in development mode, the web
REPL and the session inspector are available at the {{/web-repl}} and
{{/session-inspector}} paths.
If you want further flexibility, you can customize the web REPL and
the session inspector.
Here's a simple recipe to allow access for your local machine to the
web REPL ({{/repl}}) and to the session inspector
({{/session-inspector}}).
<enscript highlight=scheme>
(session-inspector-access-control
(lambda ()
(member (remote-address) '("127.0.0.1"))))
(enable-session-inspector "/session-inspector")
(web-repl-access-control
(lambda ()
(member (remote-address) '("127.0.0.1"))))
(enable-web-repl "/repl")
</enscript>
To access them, just point your browser to
{{http://localhost:<port>/repl}} and
{{http://localhost:<port>/session-inspector}}, respectively.
==== Create custom page definers when {{page-template}} and/or plain {{define-page}} are not enough
You can define your own page definers when {{page-template}} or the
plain {{define-page}} is not enough for what you need. Here's an
example:
<enscript highlight=scheme>
(use awful)
(enable-sxml #t)
(define (define-custom-page path content)
(define-page path
(lambda ()
`(html (body ,(content))))
no-template: #t))
(define-custom-page (main-page-path)
(lambda ()
"Hey!"))
</enscript>
If you access {{http://localhost:8080}} you'll get the following HTML code:
<html>
<body>Hey!</body></html>
==== Debugging: error messages on the browser window
Error messages right on the browser window can be quite handy for
debugging (although not for production environments). Here's a way to
accomplish that:
<enscript highlight=scheme>
(use awful)
(enable-sxml #t)
(page-exception-message
(lambda (exn)
`(pre ,(with-output-to-string
(lambda ()
(print-call-chain)
(print-error-message exn))))))
(define-page (main-page-path)
(lambda ()
`("Boom!" ,(+ 1 "foo"))))
</enscript>
This feature is automatically enabled when the awful application
server is used with the {{--development-mode}} option.
==== Run awful without arguments to quickly share a file
When invoked without arguments, awful (the application) starts the web
server using the current directory as root path and keeps listening on
port 8080. So, if you want to quickly share a file (or some files),
change to the directory containing the files and execute {{awful}}.
The access {{http://<host>:8080/<the-file-you-want>}}.
==== Awful & SXML
Since version 0.36, awful provides SXML support. To enable it, you
can either do it globally, by setting the {{enable-sxml}} parameter to
{{#t}}, or in a per-page basis, by binding {{define-page}}'s
{{use-sxml}} keyword parameter to {{#t}}.
Notice that supporting SXML by default is a goal. Strings support
will be deprecated and removed in future versions. So, if you are
about to start a project using awful, consider setting {{enable-sxml}}
to {{#t}}.
Here are some examples:
===== Globally using SXML
<enscript highlight=scheme>
(use awful)
(enable-sxml #t)
(define-page (main-page-path)
(lambda ()
`(div (@ (id "content"))
"Hi, this is awful " ,(awful-version))))
</enscript>
You can also use [[/egg/html-tags|html-tags]] and
[[/egg/html-utils|html-utils]] to generate SXML content:
<enscript highlight=scheme>
(use awful html-tags)
(enable-sxml #t)
(generate-sxml? #t) ;; from html-tags
(define-page (main-page-path)
(lambda ()
(<div> id: "content"
"Hi, this is awful " (awful-version))))
</enscript>
===== Using SXML in a per-page basis
<enscript highlight=scheme>
(use awful html-tags)
(define-page "/strings"
(lambda ()
(<div> id: "content"
"Hi, this is awful " (awful-version))))
(define-page "/sxml"
(lambda ()
`(div (@ (id "content"))
"Hi, this is awful " ,(awful-version)))
use-sxml: #t)
(define-page "/sxml-with-html-tags"
(lambda ()
(parameterize ((generate-sxml? #t))
(<div> id: "content"
"Hi, this is awful " (awful-version))))
use-sxml: #t)
</enscript>
==== Reloading awful from Emacs
Here's a quick hack to reload awful apps from Emacs. It can be handy
when you are developing using awful in development mode
({{--development-mode}}, or when you defined your own reload path).
<enscript highlight=elisp>
(defun awful-reload ()
(interactive)
(shell-command "lynx -dump http://localhost:8080/reload"))
(add-hook 'scheme-mode-hook
#'(lambda ()
(local-set-key "\C-cR" 'awful-reload)))
</enscript>
The code above defines an {{awful-reload}} procedure, which requests
the {{/reload}} path (automatically defined by awful when running in
development mode) using {{lynx}}. You can use whatever command line
HTTP client you want. {{lynx}} is handy because of the {{-dump}}
option, which can be readly displayed by Emacs in the shell command
output buffer.
Here's a screenshot:
[[image:http://parenteses.org/mario/img/awful/awful-reload-emacs.png|Reloading awful apps from Emacs]]
==== Performance tweaks
* if you don't intend to use static index files, you may configure the
[[/egg/spiffy|Spiffy]] {{index-files}} parameter to {{'()}}. That
will disable the lookup for static index files (or you can configure
it to contain only the static index files you use).
* awful usually performs better with a larger stack size (1MB is a
reasonable value). You can set the runtime stack size with the
{{-:s}} command line option (see
[[/manual/Using%20the%20compiler#runtime-options|the CHICKEN manual
page]] for other options):
$ awful -:s1m my-app.so
==== Binding URI paths as arguments to page handler procedures
<enscript highlight=scheme>
(use awful regex srfi-13)
(define (define-page* page-path content)
(define-page (regexp (string-append page-path "/*.*"))
(lambda (path)
(let ((url-args
(string-split
;; Removing `page-path' from the requested path
(string-drop path (string-length page-path))
"/")))
(condition-case
(apply content url-args)
((exn arity) "invalid arity")
(exn () (signal exn)))))))
;;;
;;; /page1 ignores all arguments
;;;
;; http://<server>:<port>/page1 => "no args"
;; http://<server>:<port>/page1/1 => "no args"
;; http://<server>:<port>/page1/1/2 => "no args"
(define-page* "/page1"
(lambda _
"no args"))
;;;
;;; /page2 requires exactly one argument
;;;
;; http://<server>:<port>/page2 => "invalid arity"
;; http://<server>:<port>/page2/1 => "foo=1"
;; http://<server>:<port>/page1/1/2 => "invalid arity"
(define-page* "/page2"
(lambda (foo)
(conc "foo=" foo)))
;;;
;;; /page3 requires one argument (the other is optional). Ignores the
;;; rest.
;;;
;; http://<server>:<port>/page3 => "invalid arity"
;; http://<server>:<port>/page3/1 => "foo=1, bar=#f"
;; http://<server>:<port>/page1/1/2 => "foo=1, bar=2"
;; http://<server>:<port>/page1/1/2/3 => "foo=1, bar=2"
(define-page* "/page3"
(lambda (foo #!optional bar)
(conc "foo=" foo ", bar=" bar)))
;;;
;;; /page4: all arguments are optional (get bound if given).
;;;
;; http://<server>:<port>/page4 => "foo=#f, bar=#f"
;; http://<server>:<port>/page4/1 => "foo=1, bar=#f"
;; http://<server>:<port>/page4/1/2 => "foo=1, bar=2"
;; http://<server>:<port>/page4/1/2/3 => "foo=1, bar=2"
(define-page* "/page4"
(lambda (#!optional foo bar)
(conc "foo=" foo ", bar=" bar)))
</enscript>
==== Awful badge
Here's a suggestion: [[image:http://parenteses.org/mario/img/thats-awful.png|That's awful!]]