-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswarm_web.erl
68 lines (53 loc) · 1.72 KB
/
swarm_web.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
%% @author J.R. Bedard <jrbedard@gmail.com>
%% @copyright 2008 jrbedard.
%% @doc Web server for swarm.
-module(swarm_web).
-author('jrbedard <jrbedard@gmail.com>').
-export([start/1, stop/0, loop/2]).
%% External API
start(Options) ->
{DocRoot, Options1} = get_option(docroot, Options),
Loop = fun (Req) ->
?MODULE:loop(Req, DocRoot)
end,
mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
stop() ->
mochiweb_http:stop(?MODULE).
loop(Req, DocRoot) ->
"/" ++ Path = Req:get(path),
io:format("Request from '' : ~p~n", [Path]),
case Req:get(method) of
Method when Method =:= 'GET'; Method =:= 'HEAD' ->
case Path of
"people/" ++ Args ->
Xml = swarm_people:request(Args),
Req:ok({"text/xml", Xml});
"locations/" ++ Args ->
Xml = swarm_locations:request(Args),
Req:ok({"text/xml", Xml});
"events/" ++ Args ->
Xml = swarm_events:request(Args),
Req:ok({"text/xml", Xml});
"maps" ++ Args ->
Req:serve_file("map.html", DocRoot);
%%MapOut = swarm_maps:request(Args),
%%Req:ok({"text/html", MapOut});
"messages/" ->
Req:serve_file(Path, DocRoot);
"images/" ++ ImgPath ->
Req:serve_file(ImgPath, DocRoot);
_ ->
Req:ok({"text/plain", io_lib:format("test: ~p~n", [Path])})
%%Req:serve_file(Path, DocRoot)
end;
'POST' ->
case Path of
_ ->
Req:not_found()
end;
_ ->
Req:respond({501, [], []})
end.
%% Internal API
get_option(Option, Options) ->
{proplists:get_value(Option, Options), proplists:delete(Option, Options)}.