japerk / routy

Yaws URL routing.

Clone this repository (size: 29.2 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/japerk/routy/
commit 5: fe0a1d679254
parent 4: 1384f38dd773
branch: default
Summary: Compile urls defined in routes
Bruno Mahe
7 months ago

Changed (Δ189 bytes):

raw changeset »

src/routy.erl (11 lines added, 7 lines removed)

Up to file-list src/routy.erl:

@@ -32,7 +32,13 @@ config_change(_Changed, _New, _Removed)
32
32
	Props = application:get_all_env(routy),
33
33
	
34
34
	P = fun(Mod) ->
35
			Mod:routes()
35
			lists:map(
36
						fun ({Route, Handlers}) -> 
37
								{ok, Compiledroute} = re:compile(Route), 
38
								{{Route, Compiledroute}, Handlers} 
39
						end,
40
						Mod:routes()
41
						)
36
42
		end,
37
43
	
38
44
	% TODO: routes need to be queried on demand, not created at start or app
@@ -94,9 +100,9 @@ out_routes(A) ->
94
100
	{ok, Routes} = application:get_env(routy, routes),
95
101
	
96
102
	MatchingUrl = lists:dropwhile(
97
								fun(Url) ->
103
								fun({_, CompiledUrl}) ->
98
104
99
										case re:run(A#arg.server_path, Url) of
105
										case re:run(A#arg.server_path, CompiledUrl) of
100
106
													nomatch -> true;
101
107
													_ -> false
102
108
										end
@@ -105,14 +111,12 @@ out_routes(A) ->
105
111
								proplists:get_keys(Routes)
106
112
								),
107
113
108
	io:format("Matching urls: ~p~n", [MatchingUrl]),
109
110
114
	case MatchingUrl of
111
115
		[] ->
112
116
			error_logger:error_report([{not_found, A#arg.server_path}]),
113
117
			{status, 404};
114
		[FirstMatchedUrl | _] ->
115
			Methods = proplists:get_value(FirstMatchedUrl, Routes),
118
		[{FirstMatchedUrl, FirstMatchedCompiledUrl} | _] ->
119
			Methods = proplists:get_value({FirstMatchedUrl, FirstMatchedCompiledUrl} , Routes),
116
120
			out_method(A, (A#arg.req)#http_request.method, {FirstMatchedUrl, Methods})
117
121
	end.
118
122