Build an app for Erlang

Source: Internet
Author: User

It's easy to build your own app in Erlang, and you can customize the app yourself, but it's just a simple record of typical practices under Erlang.
That is to build the OTP application.
Build custom A application can be Xxx.app file, you can put the app file in config folder
Eg:config/gs.app
First look at the app file:
App files are all called application resource file

Used to specify the purpose of application && how to start ...

[CPP]View PlainCopy
  1. {application,"app name",
  2. [
  3. {description,"app description"},
  4. {VSN,"version number"},
  5. {ID, Id},%%app ID with Erl-id ID
  6. {Modules,[modules]},%%app contains the module that the SysTools module uses to generate script, tar files
  7. {maxp,num},%% Process Maximum value
  8. {Maxt,time},%%app run-time unit milliseconds
  9. {registered,[mod]},%% Specifies the app name module, SysTools is used to resolve name collisions
  10. {included_applictions, [xx]},%% Specifies the child app, loads only, but does not start
  11. {applictions,[xxxx]},%% launches your app before it starts, the app will start this list first
  12. {env,[xxxx]},%% Configure the app's env, you can use application:get_env to get
  13. {mod,{xxx,args}},%% Specifies the app launch module, parameters, corresponding to your app's application behavior
  14. {start_phases,[{xxx,xxx}]]%% Specifies some actions for the start phase, corresponding to the OTP application Start_phase function
  15. ]
  16. }



Customize the app files to your needs, here my app files are:

[CPP]View PlainCopy
  1. {
  2. Application, GS,
  3. [
  4. {description, "Just GS."},
  5. {VSN, "1.0a"},
  6. {modules, [Gs_app,gs_sup]},
  7. {registered, [Gs_sup]},
  8. {mod, {gs_app, []}},
  9. {APPLICTIONS,[KERNEL,STDLIB,SASL]},
  10. {env,[{author,"JJ"}]},
  11. {start_phases, []}
  12. ]
  13. }.




OK, next to the custom OTP application:
and put the code file under SRC.

[CPP]View PlainCopy
  1. %%src/gs_app.erl
  2. -module (Gs_app).
  3. -behaviour (application).
  4. -export ([start/2,start/0, STOP/1]).
  5. Start ()
  6. Application:start (GS).
  7. Start (_, [])
  8. Io:format ("GS start ... ~n"),
  9. {OK, Pid} = Gs_sup:start_link (),
  10. Io:format ("GS Main PID is ~p ~n", [PID]),
  11. {OK, Pid}.
  12. Stop (_state),
  13. Io:format ("GS stop ... ~n").



One of the gs_sup here is in the app registered module, a typical OTP in supervisor, of course, you can also implement a module ...

[CPP]View PlainCopy
    1. %%SRC/GS_SUP.ERL  
    2. -module (gs_sup) .  
    3. -behaviour (supervisor).   
    4. -export ([START_LINK/0,INIT/1]) .  
    5.   
    6. start_link ()  ->  
    7.      supervisor:start_link ({local,? module}, ? Module, []).   
    8.   
    9.   
    10. init ([])  - >   
    11.     {ok, {     
    12.             {one_for_one, 3, 10 },     
    13.              []           
    14.      }}.   



To do this, you can simply write a emakefile to implement Erl-make, and put the beam file
Output to Ebin folder

[CPP]View PlainCopy
    1. {["src/*"]
    2. , [
    3. {OutDir, "./ebin"}
    4. ]
    5. }.


OK, basically, in order to manage the need, you can simply write a script file to launch the app, under Windows
You can do this:

[CPP]View PlainCopy
    1. Start.bat
    2. CD config/
    3. Erl-pa. /ebin/-name [email protected]-setcookie abc-boot start_sasl-s gs_app start
    4. Cmd


Finally execute the bat file ...
The app is running and can be application:loaded_applications ().

[CPP]View PlainCopy
  1. GS start ....
  2. GS Main Pid is <0.48.0>
  3. =progress report==== 28-dec-2012::15:51:46 = = =
  4. Application:gs
  5. STARTED_AT: [Email protected]
  6. Eshell V5.9 (abort with ^g)
  7. ([email protected]) 1>
  8. Eshell V5.9 (abort with ^g)
  9. ([email protected]) 1> application:loaded_applications ().
  10. [{kernel,"ERTS CXC 138","2.15"},
  11. {SASL,"SASL CXC 138","2.2"},
  12. {GS,"just GS.","1.0a"},
  13. {stdlib,"ERTS CXC 138","1.18"}]
  14. ([email protected]) 2>





In this way, the simple app is complete ....
Of course, it's just a very, very simple app. Only the parent supervisor is implemented.

Build an app for Erlang

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.