umleitung
an intro to mochiweb and CouchDB
mochiweb

• get mochiweb
•   svn checkout http://mochiweb.googlecode.com/svn/
    trunk/ mochiweb


• create new applicati...
run mochiweb

• make the project and run it
•   cd umleitung

•   make

•   ./start-dev.sh


• go to http://localhost:8000
directory structure

• we have our sources in /src
• we have our compiled binaries in /ebin
• the only file we need in the ...
act on GET/HEAD
Method when Method =:= 'GET'; Method =:= 'HEAD' ->

 case lookup(Path) of

    {ok, Dest} ->

        Req:...
the lookup/1
%% Internal API



lookup(Path) ->

      io:format(quot;Path is: ~s~nquot;, [Path]),

      {ok, Path}.

...
testing

• recompile and see that the running project
  is reloaded
  • to recompile just run make
• request http://localh...
CouchDB
• get CouchDB from http://
  couchdb.apache.org
• best compile from source
• http://wiki.apache.org/couchdb/
  Ins...
create DB
• click on “create database”
• name it “umleitung”
• create a document:
      type: redir
      path: test
     ...
create view
• select the view: Custom query...
• in the map function add:
  function(doc) {

      if(doc.type == 'redir')...
create view 2

• choose save as
• name the design document: _design/redir
• and the view: match
what we have

• we have a mochiweb server interpreting
  our path
• we have a couchdb that can save
  path:destination tup...
erlang_couchdb
• based on mochiweb
• slim library, small foot print
•   git clone git://github.com/ngerakines/
    erlang_...
the view request

• erlang_couchdb:invoke_view({quot;HOSTquot;,
  PORT}, quot;DBquot;, quot;DESIGN DOCquot;, quot;VIEWquot...
major tuple hacking
• the erlang_couchdb lib is small, that means
  less comfort
• the data structure is deep nested json ...
fiddle it together
lookup(Path) ->
    io:format(quot;PATH: ~s~nquot;, [Path]),
   {json,{struct, Props}} =
      erlang_co...
testing
• recompile the project again
• go to http://localhost:8000/test
• then go to http://localhost:8000/bla
• we have ...
do the redirect

• the final step is doing the redirect now
• change the response line to:
  Req:respond({302, [{quot;Locat...
cosmetics
• getting rid of that ugly compile warning and
  serving a static file
  case lookup(Path) of
       {ok, Dest} -...
and now?

• sources are on github
• git clone git://github.com/norbu09/
  umleitung.git
• go out and play, extend, rewrite...
hope you enjoyed it
          lenz@ideegeo.com

http://github.com/norbu09/umleitung/tree
Upcoming SlideShare
Loading in...5
×

Umleitung: a tiny mochiweb/CouchDB app

2,689

Published on

The slides from erlounge Wellington 02-2009. Umleintung is a tiny HTTP redirect daemon based on mochiweb and CouchDB.
It describes how to bootstrap a mochiweb application and integrate CouchDB.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,689
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
65
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Umleitung: a tiny mochiweb/CouchDB app

  1. 1. umleitung an intro to mochiweb and CouchDB
  2. 2. mochiweb • get mochiweb • svn checkout http://mochiweb.googlecode.com/svn/ trunk/ mochiweb • create new application • mochiweb/scripts/new_mochiweb.erl umleitung
  3. 3. run mochiweb • make the project and run it • cd umleitung • make • ./start-dev.sh • go to http://localhost:8000
  4. 4. directory structure • we have our sources in /src • we have our compiled binaries in /ebin • the only file we need in the moment is: src/umleitung_web.erl • open it in your editor
  5. 5. act on GET/HEAD Method when Method =:= 'GET'; Method =:= 'HEAD' -> case lookup(Path) of {ok, Dest} -> Req:respond({200, [], Dest}); _ -> Req:respond({501, [], quot;errorquot;}) end;
  6. 6. the lookup/1 %% Internal API lookup(Path) -> io:format(quot;Path is: ~s~nquot;, [Path]), {ok, Path}. ...
  7. 7. testing • recompile and see that the running project is reloaded • to recompile just run make • request http://localhost:8000/test • look at the console output as well
  8. 8. CouchDB • get CouchDB from http:// couchdb.apache.org • best compile from source • http://wiki.apache.org/couchdb/ Installing_from_source • run couchdb (sudo couchdb) • start futon on http://localhost:5984/_utils
  9. 9. create DB • click on “create database” • name it “umleitung” • create a document: type: redir path: test destination: http://ideegeo.com
  10. 10. create view • select the view: Custom query... • in the map function add: function(doc) { if(doc.type == 'redir'){ emit(doc.path, doc.destination); } }
  11. 11. create view 2 • choose save as • name the design document: _design/redir • and the view: match
  12. 12. what we have • we have a mochiweb server interpreting our path • we have a couchdb that can save path:destination tuples • ... lets connect them
  13. 13. erlang_couchdb • based on mochiweb • slim library, small foot print • git clone git://github.com/ngerakines/ erlang_couchdb.git • cd erlang_couchdb • make dist-src • and link it to /deps (ext. dependencies)
  14. 14. the view request • erlang_couchdb:invoke_view({quot;HOSTquot;, PORT}, quot;DBquot;, quot;DESIGN DOCquot;, quot;VIEWquot;, [{quot;keyquot;, quot;quot;quot; ++ REQUEST KEY ++ quot;quot;quot;}]), • our request key is the “Path” variable that holds the path we want to match against • note the enclosing ‘quot;’
  15. 15. major tuple hacking • the erlang_couchdb lib is small, that means less comfort • the data structure is deep nested json tuples • the view request looks like this: {json,{struct, Props}} = erlang_couchdb:invoke_view( {quot;localhostquot;, 5984}, quot;umleitungquot;, quot;redirquot;, quot;matchquot;, [{quot;keyquot;, quot;quot;quot; ++ Path ++ quot;quot;quot;}]),
  16. 16. fiddle it together lookup(Path) -> io:format(quot;PATH: ~s~nquot;, [Path]), {json,{struct, Props}} = erlang_couchdb:invoke_view({quot;localhostquot;, 5984}, quot;umleitungquot;, quot;redirquot;, quot;matchquot;, [{quot;keyquot;, quot;quot;quot; ++ Path ++ quot;quot;quot;}]), try proplists:get_value(<<quot;rowsquot;>>, Props) of [{struct, Rows} | _] -> {ok, proplists:get_value(<<quot;valuequot;>>, Rows)}; _ -> {error, unknown} catch _:_ -> {error, unknown} end.
  17. 17. testing • recompile the project again • go to http://localhost:8000/test • then go to http://localhost:8000/bla • we have connected CouchDB to our mochiweb server • ... relax ...
  18. 18. do the redirect • the final step is doing the redirect now • change the response line to: Req:respond({302, [{quot;Locationquot;, Dest}], quot;quot;}); • recompile • reload the http://localhost:8000/test
  19. 19. cosmetics • getting rid of that ugly compile warning and serving a static file case lookup(Path) of {ok, Dest} -> Req:respond({302, [{quot;Locationquot;, Dest}], quot;quot;}); _ -> Req:serve_file(quot;404.htmlquot;, DocRoot) end; • add a 404.html to priv/www/
  20. 20. and now? • sources are on github • git clone git://github.com/norbu09/ umleitung.git • go out and play, extend, rewrite it • add a web interface (based on nitrogen?)
  21. 21. hope you enjoyed it lenz@ideegeo.com http://github.com/norbu09/umleitung/tree
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×