Skip to content

Instantly share code, notes, and snippets.

@arjan
Last active August 29, 2015 14:07
Show Gist options
  • Save arjan/ca32bf875052876ed94a to your computer and use it in GitHub Desktop.
Save arjan/ca32bf875052876ed94a to your computer and use it in GitHub Desktop.
-module(router).
-export([g/0]).
%% [{attribute,1,file,
%% {"/home/arjan/devel/zotonic/src/router.erl",1}},
%% {attribute,1,module,router},
%% {attribute,1,lager_records,[]},
%% {attribute,3,export,[{route,1}]},
%% {function,5,route,1,
%% [{clause,5,
%% [{nil,5}],
%% [],
%% [{tuple,6,
%% [{atom,6,name},
%% {atom,6,controller_template},
%% {cons,6,{tuple,6,[...]},{nil,6}}]}]}]},
-define(ROUTER_MOD, z_dispatch_router).
-include_lib("wm_host_dispatch_list.hrl").
g() ->
Rules = z_sites_dispatcher:collect_dispatchrules(),
Forms = [{attribute, 0, module, ?ROUTER_MOD},
{attribute,3,export,[{get_dispatch_alias,1}]},
generate_dispatch_alias_fn(Rules)],
{ok, ?ROUTER_MOD, Binary} = compile:forms(Forms),
code:load_binary(?ROUTER_MOD, "z_dispatch_router.gen.erl", Binary).
generate_dispatch_alias_fn(Rules) ->
{function, 0, get_dispatch_alias, 1,
lists:flatten(
[
[{clause, 0, [erl_parse:abstract(WM#wm_host_dispatch_list.hostname)], [], [erl_parse:abstract(WM#wm_host_dispatch_list.hostname)]}]
++
[{clause, 0, [erl_parse:abstract(A)], [], [erl_parse:abstract(WM#wm_host_dispatch_list.hostname)]}
|| A <- WM#wm_host_dispatch_list.hostalias]
|| WM <- Rules]
) ++
[{clause, 0, [{var, 0, '_'}], [], [{atom, 0, undefined}]}]
}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment