sunneach / webcandy (http://onerlang.blogspot.com/)
The Hughes Waroquier's StickyNotes app works under control of the WebMachine 1.0+ (implemented on MochiWeb server). A bit deeper attempt to illustrate and use the key mechanisms and methods of this approach.
$ hg clone http://bitbucket.org/sunneach/webcandy/
History: Release 0.1: the initial merge of StickyNotes with WebMachine.
WebCandy goes RESTFul
Release 0.2 changes:
- static and DB requests are served by separate resources.
- The supervisor was rolled back to its original, for relative paths are supported, see below for the Dispatch.conf
- The StickyNotes application.js was updated to move from the generic POST to
- GET for action:read_all,
- PUT for action:update
- DELETE for action:delete
- POST just for action:create
- The StickyNotes notes.erl had the read method updated to support common message standard (conversion could be done in the resource as well though)
- The home page has admin functionality. It allows you to
- check the WebMachine TRACE status
- set TRACE ON / OFF (see below)
- go view the wmtrace page
- Play with HTTP methods and do much of Curl functionality.
- Go to the StickyNotes client
The Trace Admin Interface
On top of the home page there are two buttons in the trace group: Refresh and Turn ON (Turn OFF). The refresh will show you the current trace status in the left. How does it work?
The priv/dispatch.conf is:
Trace admin. Logic.
The first config line is used for view the dynamic trace list page or the individual trace results. The second line is administrative. The url is always /_trace
| HTTP Method | action |
|---|---|
| GET | ask for trace status |
| PUT | turn TRACE ON |
| DELETE | turn TRACE OFF |
Trace admin. Backend.
When the PUT request comes to /_trace, the webmstn_admin_resource virtually converts the Dispatch.conf to the following:
Of course, the file is read once and it is only the dispatch list in the RequestData, which is affected. The path to the trace folder, "priv/traces" is given in the first row of the Dispatch.conf, and it simply gets copied into all lines but webmstn_admin_service which does not need to be traced.
The resource code is already prepared to the tracing:
and the webmstn_static_resource as well:
CRUD Database operations. Server
The original version of StickyNotes has all operations communicating through the POST channel. The code was very short and elegant. The actual operation was parsed by the POST handler process_post and the notes was called for that action. The rest was encoding/decoding of the message body. Just several generic lines of code do all of it in the resource.
Not anymore. We have four different types of message (plus HEAD, which goes the same way as GET) and we need at least four handlers. Now webmstn_notes_resource accepts all methods:
That means you can only POST to /notes to create a new note and use GET/PUT/DELETE /notes/321 to read/update/delete a note 321. One special Id, all is used by GET /notes/all to initialize StickyNotes screen. It merely says "SELECT * FROM NOTES". DELETE/PUT with all are not implemented - just placeholders for now.
Go ahead and try
This revision is from 2009-10-06 13:58
