Guest User

Untitled

a guest
Jan 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.30 KB | None | 0 0
  1. -module(misultin_rest).
  2. -export([start/1, stop/0]).
  3.  
  4. % start misultin http server
  5. start(Port) ->
  6.     misultin:start_link([{port, Port}, {loop, fun(Req) -> handle_http(Req) end}]).
  7.  
  8. % stop misultin
  9. stop() ->
  10.     misultin:stop().
  11.  
  12. % callback function called on incoming http request
  13. handle_http(Req) ->
  14.     % dispatch to rest
  15.     handle(Req:get(method), Req:resource([lowercase, urldecode]), Req).
  16.  
  17. % ---------------------------- \/ handle rest --------------------------------------------------------------
  18.  
  19. % handle a GET on /
  20. handle('GET', [], Req) ->
  21.     Req:ok([{"Content-Type", "text/plain"}], "Main home page.");
  22.  
  23. % handle a GET on /users
  24. handle('GET', ["users"], Req) ->
  25.     Req:ok([{"Content-Type", "text/plain"}], "Main users root.");
  26.  
  27. % handle a GET on /users/{username}
  28. handle('GET', ["users", UserName], Req) ->
  29.     Req:ok([{"Content-Type", "text/plain"}], "This is ~s's page.", [UserName]);
  30.  
  31. % handle a GET on /users/{username}/messages
  32. handle('GET', ["users", UserName, "messages"], Req) ->
  33.     Req:ok([{"Content-Type", "text/plain"}], "This is ~s's messages page.", [UserName]);
  34.  
  35. % handle the 404 page not found
  36. handle(_, _, Req) ->
  37.     Req:ok([{"Content-Type", "text/plain"}], "Page not found.").
  38.    
  39. % ---------------------------- /\ handle rest --------------------------------------------------------------
Add Comment
Please, Sign In to add comment