[erlang-questions] separating application from gen server

Tom Whitcomb thomaswhitcomb@REDACTED
Wed Nov 28 05:33:16 CET 2007


Hi,

I want to use application:get_env in my gen_server but it feels wrong to create a dependency between my gen_server and a specific application.

Is there a recommended way for a gen_server to receive and reference the application that contains it?

What if I do something like below which passes the application name from the .app to a start_link/1 to the init/1 in the gen_server?

Thanks for any recommendations.

Tom


% .app
% ----

  {application, my_app, 
  [{mod, {my_app,[my_app]}}]}. 


% my application
% --------------

  -module(my_app).
  -behavior(application).

  start(Type,Args) ->
    my_server:start_link(Args).
 
  stop(_State) ->
    ok.


% my gen server
% -------------

  -module(my_server).
  -behavior(gen_server).

  start_link(Args) -> 
    gen_server:start_link({local,?MODULE},?MODULE,Args,[]).

  stop() -> 
    gen_server:stop(?MODULE).

  init([App]) ->
    application:get_env(App).






More information about the erlang-questions mailing list