app.arc
. The two main features of the app server are account management and improved forms handling.
arc>(asv 8080)
However, it is generally better to start the server in a separate thread, so the Arc REPL can be used. This allows the web server to be modified while it is running.
arc> (thread (asv 8080))
The user login uses a simple browser cookie to keep track of the login. Note that the user account management is entirely orthogonal to the fnid-based continuations of the Arc web server. Logins are maintained through a cookie; fnids are passed in the URL or a form field. The app server includes several mechanisms to ensure that a fnid callback is executed by the expected user.
The app server defines the following pages:
/whoami
: displays the logged-in userid and IP address, or redirects to login.
/login
: logs user in or creates new account.
/logout
: logs the current user out.
/admin
: displays the administrative page, if the user is logged into an admin account.
/mismatch
: displays an error "Dead link: users don't match." This page is used when a fnid is accessed by the wrong (or logged-out) user.
The following is an example page with user authentication; it will run at http://localhost:8080/example. First, the handler ensures the user is logged in, and displays the login page otherwise. The page displays a form saying "This is the example page". When submitted, the page will say, "Hello user". The uform
form ensures that the user is still logged in when the form is submitted; otherwise, the page will display the dead link error.
(defopl example req (let user (get-user req) (uform user req (prn "Hello " user) (prn "This is the example page.") (submit))))The following example illustrates
urform
. The page http://localhost:8080/urexample will accept a value in a form. When submitted, the continuation function will output a cookie header and redirect to the page "uexample", which will display the cookies.
(defopl urexample req (let user (get-user req) (urform user req (do (prn "Set-cookie: mycook=" (alref (req 'args) "foo")) "uexample") (prn "Enter value:") (input 'foo) (submit)))) (defopl uexample req (prn "User " (get-user req)) (br) (prn "Cookies " (req 'cooks)))
Markdown is a simple mechanism for adding some formatting to plain text. Text surrounded by asterisks is converted to italics. URLs are converted to links. Blank lines indicate paragraph breaks. Lines that are indented and separated from previous lines by a blank line are displayed as preformatted code. The Arc app server provides mechanisms to convert markdown text to HTML, and supports markdown input in forms.
The app server also provides a mechanism to create forms consisting of multiple typed fields in a table. For example, a form can have one string input and one integer input. The types are entirely separate from Arc's datatypes. The following table outlines the supported types:
Type | Form field | Result |
---|---|---|
string | text input of width formwid* | String |
string1 | text input of width formwid* | String, empty not allowed |
int | text input of width numwid* | Integer (rounded) |
num | text input of width numwid* | Number |
posint | text input of width numwid* | Integer > 0 (rounded) |
doc | textarea input of width bigformwid* | String |
text | textarea input of width formwid* | String |
mdtext | textarea input of width formwid* | Markdown text |
mdtext2 | textarea input of width formwid* | Markdown text, no links |
toks | text input of width formwid* | List of string tokens |
bigtoks | textarea input of width formwid* | List of tokens |
sexpr | text input of width formwid* | List of S-expressions. |
hexcol | text input | String if the string defines a valid hex color |
url | text input of width formwid* | URL (empty string allowed). |
users | text input of width formwid* | List of usernames with bad names filtered out |
choice | select dropdown menu. | Type from the choice list |
yesno | select dropdown with "yes" and "no" choices. | Boolean, true for input "yes" |
choice
type is specified as a list: choice
, the type of the choices, and the choices themselves, for instance '(choice int 1 2 3)
. The mdtext
and mdtext2
inputs include a help link to formatdoc-url*
.
A typed form is generated by vars-form
, which is a fairly complex procedure. It takes a list of field specifications, where each field specification is a list
of (type label value view modify question)
. The type
specifier is from the above table. The label
is the name assigned to the input field. The initial value of the field is value
. If view
is nil
, the field is skipped. If modify
is nil, the field is not modifiable; it is displayed as text rather than an input field. If question
is defined, it appears as a caption above the field; otherwise, the label is displayed before the field.
The following example shows a form created by vars-form
. When the form is submitted, each name and value is printed, followed by "Done!". The user must log in, if not already logged in.
The example runs at the URL http://localhost:8080/vars-form.
(defopl vars-form req (vars-form (get-user req) '((int field1 42 t t "Enter int:") (toks field2 (a b c) t t) (string nil "bar" t nil "Can't touch this.")) (fn (name val) (prn name " " val) (br)) (fn () (prn "Done!")) "Doit"))The generated form is:
asv [port]
Starts the application server.
|
>(asv 8080) |
vars-form user fields f done [button [lasts]]
Generates a form for
user . fields is a list of (type label value view modify question) lists specifying the form. When submitted, f is executed on each field, with the arguments label newval . Then continuation function done is executed. If there is a modifiable field, a submit button is generated with label specified by button . The lifetime of the associated fnid can be specified with lasts . |
|
md-from-form str [nolinks]
Converts
str to markdown after escaping it. URLs will be converted to links unless nolinks is set. Used to generate markdown from form input. |
>(md-from-form "Hello *world* &")
"Hello <i>world</i> &"
|
markdown s [maxurl [nolinks]]
Applies the markdown rules to
s to generate HTML. |
>(prn (markdown "Text\n\n Code\nhttp://arcfn.com, and *stuff*"))
Text<p><pre><code> Code</code></pre>
<a href="http://arcfn.com" rel="nofollow">http://arcfn.com
</a>, and <i>stuff</i>
Text
http://arcfn.com, and stuff
|
unmarkdown s
Inverse of
markdown to convert HTML to a marked-down string. |
>(unmarkdown "Text<p><pre><code> Code</code></pre>")
"Text\n\n Code"
|
paras s
Returns list of paragraph indices. New in arc3.
|
>(paras "ab\n\ncde\n\nfgh")
("ab" "cde" "fgh")
|
Copyright 2008 Ken Shirriff.