Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 128 lines (118 sloc) 4.889 kB
6f920f9 @nicferrier tutorial files
authored
1
2 * suggestions
3 ** How about going from "hello world" on?
4 *** Start with that,
5 *** and then, move on to publishing a static file,
6 *** then a buffer,
7 **** with calling functions to manipulate the buffer and re-present it?
8 ** That'd save me a fair bit of tinkering :)
9
10 * installing
11 ** use elpa/marmalade
12 * what elnode gives you by default
13 ** require elnode
14 *** elnode-init
15 **** starts a server
16 ***** on port 8000
17
18
19 * hello world
20 ** install elnode with marmalade
21 ** open a new emacs buffer C-x C-f my-elnode-hello-world.el
22 ** make a handler
23 (defun my-elnode-hello-world-handler (httpcon)
24 (elnode-http-start httpcon 200 '("Content-Type" . "text/html"))
25 (elnode-http-return
26 httpcon
27 "<html><body><h1>Hello World</h1></body></html>"))
28 (elnode-start my-elnode-hello-world-handler 8028 "localhost")
29 ** now evaluate that with M-x eval-buffer
30 ** now open localhost:8028 in your browser
31
32 * publish some files
33 ** elnode provides a webserver, more accurately a fileserver
34 ** the webserver is turned on by default
35 ** open localhost:8000 and you should see ~/public_html
36 *** if you don't have ~/public_html then make one?
37 *** or configure elnode-webserver-docroot
38 ** make a new webserver
39 *** make a new docroot
40 **** mkdir ~/myspecialdocroot
41 *** put an html file in there
42 cat <<EOF > ~/myspecialdocroot/saybum.html
43 <html>
44 <h1>BUM!</h1>
45 </html>
46 *** open a new emacs buffer
47 *** put the following lisp in
48 (defvar my-elnode-webserver-handler
49 (elnode-webserver-handler-maker "~/myspecialdocroot"))
50 (elnode-start my-elnode-webserver-handler 8001 "localhost")
51 *** now evaluate that with M-x eval-buffer
52 *** now open localhost:8001/saybum.html
53 *** now open localhost:8001
54 **** you should see an automatic index
55
56 * stopping a server
57 ** stop 8028
58 ** stop 8001
59
60 * add a binding to the standard server
61 ** we can add bindings to the standard elnode server
62 ** go back to hello world - C-x b my-elnode-hello-world.el
63 ** remove the server-start and add this:
64 (add-to-list 'elnode-hostpath-default-table '("/helloworld/" . my-elnode-hello-world-handler))
65 ** so now it should be:
66 (defun my-elnode-hello-world-handler (httpcon)
67 (elnode-http-start httpcon 200 '("Content-Type" . "text/html"))
68 (elnode-http-return
69 httpcon
70 "<html><body><h1>Hello World</h1></body></html>"))
71 (add-to-list 'elnode-hostpath-default-table '("/helloworld/" . my-elnode-hello-world-handler))
72 ** now eval the buffer with M-x eval-buffer
73 ** now open localhost:8000/helloworld/ in your browser
74 ** just to prove the webserver is still there, open localhost:8000/
75 *** check it's still the directory ~/public_html
76 ** check the variable elnode-hostpath-default-table with C-h v elnode-hostpath-default-table
77 Its value is (("/helloworld/" . my-elnode-hello-world-handler)
78 ("[^/]+/.*" . elnode-webserver))
79 ** elnode-hostpath-default-table can also be customized
80 *** but any handler will have to be loaded so you probably need to package and load your elnode module
81
82 * publishing something else?
83 ** let's try and make an online editor
84 ** make a new file my-elnode-editor.el
85 (defvar my-elnode-editor-buffer (get-buffer-create "*my-elnode-editor-buffer*"))
86
87 (defun my-elnode-editor-handler (httpcon)
88 (elnode-http-start httpcon 200 '("Content-Type" . "text/plain"))
89 (elnode-http-return
90 httpcon
91 (with-current-buffer my-elnode-editor-buffer
92 (buffer-substring-no-properties (point-min) (point-max)))))
93 ** eval that
94 ** go type some data in *my-elnode-editor-buffer*
95 ** then M-x elnode-start my-elnode-editor-handler 8002 localhost
96 ** try and hit localhost:8002
97 ** go update the buffer
98 ** refresh the webpage
99 ** but what about someone else updating the buffer?
100 ** make another handler to handle updates
101 (defun my-elnode-editor-update-handler (httpcon)
102 (let ((change-text (elnode-http-param httpcon "change")))
103 (with-current-buffer my-elnode-editor-buffer
104 (goto-char (point-max))
105 (insert (if (stringp change-text)
106 change-text
107 ""))))
108 (elnode-http-start httpcon 302 '("Location" . "/"))
109 (elnode-http-return httpcon))
110 ** now we need to map these two handlers
111 *** one to / and the other to /update/
112 ** make a new variable
113 (defvar my-elnode-editor-urls
114 `(
115 ("$" . my-elnode-editor-handler)
116 ("update/.*$" . my-elnode-editor-update-handler)))
117 ** and make a dispatcher handler for the urls
118 (defun my-elnode-editor-dispatcher-handler (httpcon)
119 (elnode-dispatcher httpcon my-elnode-editor-urls))
120 *** a dispatcher handler is a handler that accepts requests and dispatches them to further handlers.
121 *** moar about dispatcher handlers.
122 ** now stop the old server
123 ** M-x elnode-stop 8002
124 ** Now start the new server with the dispatcher handler
125 ** then M-x elnode-start my-elnode-editor-dispatcher-handler 8002 localhost
126 ** now visit localhost:8002 and see the buffer
127 ** now visit localhost:8002/update/?change=lah+dee+dah%0d and see the updated buffer
Something went wrong with that request. Please try again.