Power CouchDB - Basic HTTP Handlers
Friday, April 10, 2009 at 17:22 by
Jón Grétar Borgþórsson You may know CouchDB's mostly as a kick ass document store but you can use it's base for many great things. Thanks to the fact that CouchDB is an amazingly well written and very modular piece of technology. It is easy to extend it and add your custom code to all areas of CouchDB. If you want to use your own language for views you can add it without even touching the CouchDB code. Write your own authentication handlers connected to LDAP or even CouchDB itself in just a few lines of erlang.
One thing that may interest you are the httpd_global_handlers. They are the little handlers for example behind the /_all_dbs and /_utils paths and more. You can also use them to easily make our own resources and in fact it is by far the simplest way to create a REST resource I know of. So if you are working with json resources it me be worth simply adding a handler in CouchDB rather than making it seperatly using webmachine or other technology. In fact here is an example of how making a resource for the computers environment variables. (Hey it could happen you needed that.)
Well thats easy. We import a few helper functions from couch_httpd that will handle returning JSON data back to the client. We make a method that takes a #httpd record as a variable and if its path has one part we return all environment variables but if it has 2 parts we return the value of that specific one. In fact in creating this resource there are only a handful of lines that I would even call "my code".
Compile this and put in CouchDB's code path. To use it we just open up local.ini and define our new handler and map to '/_env'.
[httpd_global_handlers]
_env = {my_httpd_handlers, handle_env_req}
Now just restart and go to http://127.0.0.1/_env and http://127.0.0.1/_env/HOME to see the result. For more complete examples of using handlers just go the the couchdb source and view the couch_httpd_misc_handlers.erl file.




Reader Comments (4)
Great blog,
10 minutes gave me
{"PWD":"/home/david/bin/couchbeam",
"SUDO_USER":"david",
"SHELL":"/bin/bash",
"SUDO_COMMAND":"/usr/local/bin/couchdb",
"ROOTDIR":"/opt/erlang/lib/erlang",
"XAUTHORITY":"/home/david/.Xauthority",
"LS_COLORS":"no00:fi00:di01;34:ln01;36:pi40;33:so01;35:do01;35:bd40;33;01:cd40;33;01:or40;31;01:su37;41:sg30;43:tw30;42:ow34;42:st37;44:ex01;32:*.tar01;31:*.tgz01;31:*.svgz01;31:*.arj01;31:*.taz01;31:*.lzh01;31:*.lzma01;31:*.zip01;31:*.z01;31:*.Z01;31:*.dz01;31:*.gz01;31:*.bz201;31:*.bz01;31:*.tbz201;31:*.tz01;31:*.deb01;31:*.rpm01;31:*.jar01;31:*.rar01;31:*.ace01;31:*.zoo01;31:*.cpio01;31:*.7z01;31:*.rz01;31:*.jpg01;35:*.jpeg01;35:*.gif01;35:*.bmp01;35:*.pbm01;35:*.pgm01;35:*.ppm01;35:*.tga01;35:*.xbm01;35:*.xpm01;35:*.tif01;35:*.tiff01;35:*.png01;35:*.svg01;35:*.mng01;35:*.pcx01;35:*.mov01;35:*.mpg01;35:*.mpeg01;35:*.m2v01;35:*.mkv01;35:*.ogm01;35:*.mp401;35:*.m4v01;35:*.mp4v01;35:*.vob01;35:*.qt01;35:*.nuv01;35:*.wmv01;35:*.asf01;35:*.rm01;35:*.rmvb01;35:*.flc01;35:*.avi01;35:*.fli01;35:*.gl01;35:*.dl01;35:*.xcf01;35:*.xwd01;35:*.yuv01;35:*.aac00;36:*.au00;36:*.flac00;36:*.mid00;36:*.midi00;36:*.mka00;36:*.mp300;36:*.mpc00;36:*.ogg00;36:*.ra00;36:*.wav00;36:",
"LANG":"en_GB.UTF-8",
"DISPLAY":":0.0",
"PATH":"/opt/erlang/lib/erlang/erts-5.7.1/bin:/opt/erlang/lib/erlang/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin",
"USERNAME":"root",
"TERM":"xterm",
"LOGNAME":"root",
"SUDO_UID":"1000",
"BINDIR":"/opt/erlang/lib/erlang/erts-5.7.1/bin",
"COLORTERM":"gnome-terminal",
"EMU":"beam",
"PROGNAME":"erl",
"HOME":"/home/david",
"LD_LIBRARY_PATH":"/usr/lib:",
"USER":"root",
"SUDO_GID":"1000"}
Great!
Great!
Is it possible to spawn a external command via http handler?
I am trying to spawn python script but can't get it right.
Should not be a problem really. Just use os:cmd/1. This is just normal erlang code. The only problem I can think of is that you should avoid waiting for the return as the request may time out.
However there are other ways available that you may wish to check out first. Depending on what you are trying to accomplish. There is a notification system built into CouchDB. You can see it used by the Lucene-CouchDB full text search.