PageRenderTime 532ms CodeModel.GetById 516ms RepoModel.GetById 1ms 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
Possible License(s): MPL-2.0-no-copyleft-exception
  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. %% Public API
  28. -export([run/2]).
  29. %% Useful middleware
  30. -export([module_mw/2, rpc_mw/4, mfa_mw/3]).
  31. -include_lib("ewgi.hrl").
  32. %% @spec run(Application::ewgi_app(), Context::ewgi_context()) -> Context1::ewgi_context()
  33. %% @doc Runs an EWGI application with Context and returns the new Context1.
  34. -spec run(ewgi_app(), ewgi_context()) -> ewgi_context().
  35. run(Application, Context) when is_function(Application, 1) ->
  36. Application(Context).
  37. %% @spec module_mw(Module::term(), Args:any()) -> ewgi_app()
  38. %% @doc Produces a middleware application which calls the run/1 function exported by Module.
  39. -spec module_mw(any(), any()) -> ewgi_app().
  40. module_mw(Module, Args) ->
  41. F = fun(Context) ->
  42. Module:run(Context, Args)
  43. end,
  44. F.
  45. %% @spec rpc_mw(Node::atom(), Module::atom(), Fun::atom(), Args:any()) -> ewgi_app()
  46. %% @doc Produces a middleware application which calls the remote function Module:Fun on Node.
  47. -spec rpc_mw(atom(), atom(), atom(), any()) -> ewgi_app().
  48. rpc_mw(Node, Module, Fun, Args) ->
  49. F = fun(Context) ->
  50. rpc:call(Node, Module, Fun, [Context, Args])
  51. end,
  52. F.
  53. %% @spec mfa_mw(Module::atom(), Fun::atom(), Args:any()) -> ewgi_app()
  54. %% @doc Produces a middleware application which calls the function Module:Fun(Context, Args)
  55. -spec mfa_mw(atom(), atom(), any()) -> ewgi_app().
  56. mfa_mw(Module, Fun, Args) when is_atom(Module), is_atom(Fun) ->
  57. F = fun(Context) ->
  58. apply(Module, Fun, [Context, Args])
  59. end,
  60. F.