PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

/src/ewgi_application.erl

http://github.com/skarab/ewgi
Erlang | 67 lines | 25 code | 7 blank | 35 comment | 0 complexity | 837a5082782b463d397bf0d276c701ea MD5 | raw file
 1%%%-------------------------------------------------------------------
 2%%% File    : ewgi_application.erl
 3%%% Authors : Filippo Pacini <filippo.pacini@gmail.com>
 4%%%           Hunter Morris <huntermorris@gmail.com>
 5%%% License :
 6%%% The contents of this file are subject to the Mozilla Public
 7%%% License Version 1.1 (the "License"); you may not use this file
 8%%% except in compliance with the License. You may obtain a copy of
 9%%% the License at http://www.mozilla.org/MPL/
10%%%
11%%% Software distributed under the License is distributed on an "AS IS"
12%%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
13%%% the License for the specific language governing rights and
14%%% limitations under the License.
15%%% The Initial Developer of the Original Code is S.G. Consulting
16%%% srl. Portions created by S.G. Consulting s.r.l. are Copyright (C)
17%%% 2007 S.G. Consulting srl. All Rights Reserved.
18%%%
19%%% @doc 
20%%% <p>ewgi application behaviour.</p>
21%%%
22%%% @end
23%%%
24%%% Created : 22 May 2008 by Hunter Morris <huntermorris@gmail.com>
25%%%-------------------------------------------------------------------
26-module(ewgi_application).
27
28%% Public API
29-export([run/2]).
30
31%% Useful middleware
32-export([module_mw/2, rpc_mw/4, mfa_mw/3]).
33
34-include_lib("ewgi.hrl").
35
36%% @spec run(Application::ewgi_app(), Context::ewgi_context()) -> Context1::ewgi_context()
37%% @doc Runs an EWGI application with Context and returns the new Context1.
38-spec run(ewgi_app(), ewgi_context()) -> ewgi_context().
39run(Application, Context) when is_function(Application, 1) ->
40    Application(Context).
41
42%% @spec module_mw(Module::term(), Args:any()) -> ewgi_app()
43%% @doc Produces a middleware application which calls the run/1 function exported by Module.
44-spec module_mw(any(), any()) -> ewgi_app().
45module_mw(Module, Args) ->
46    F = fun(Context) ->
47                Module:run(Context, Args)
48        end,
49    F.
50
51%% @spec rpc_mw(Node::atom(), Module::atom(), Fun::atom(), Args:any()) -> ewgi_app()
52%% @doc Produces a middleware application which calls the remote function Module:Fun on Node.
53-spec rpc_mw(atom(), atom(), atom(), any()) -> ewgi_app().
54rpc_mw(Node, Module, Fun, Args) ->
55    F = fun(Context) ->
56                rpc:call(Node, Module, Fun, [Context, Args])
57        end,
58    F.
59
60%% @spec mfa_mw(Module::atom(), Fun::atom(), Args:any()) -> ewgi_app()
61%% @doc Produces a middleware application which calls the function Module:Fun(Context, Args)
62-spec mfa_mw(atom(), atom(), any()) -> ewgi_app().
63mfa_mw(Module, Fun, Args) when is_atom(Module), is_atom(Fun) ->
64    F = fun(Context) ->
65                apply(Module, Fun, [Context, Args])
66        end,
67    F.