Erlang a high-performance web framework Cowboy usage notes

Source: Internet
Author: User

Environment: Ubuntu_server 1210 Objective: To build a web version of the Hello World program Reference link: http://roberto-aloi.com/blog/2013/07/13/ Create-deploy-erlang-cowboy-application-heroku/ 1. Building a project's base directory using rebar   first get rebar tools $ git Clone https ://github.com/rebar/rebar.git$ CD rebar$./bootstrap$ CD. Initialize a new Git repository and use rebar to create the skeleton for a new Erlang app. I decided to call my application Erlblog. Call your application differently, replacing every occurrence of Erlblog with your favourite application name in the InStr Uctions below. Please note that this is not optional, since, applications cannot has the same name on Heroku and you don ' t dare to CL Ash with my own application.  create a directory erlblog copy the rebar compiled and generated executables rebar to the Erlblog directory, and execute the following command to generate the project base directory $./rebar Create-app Appid=erlblog generate rebar Required configuration file, the name must be rebar.config$ cat Rebar.config profile content as follows: {deps, [        {cowboy, "0.8.4", {git, "https://github.com/extend/cowboy.git", {tag, "0.8.4"}}}       ]}. ADD Cowboy To The list of applications in your. app.src file. Also, set the HTTP_PORT environment variable to 8080 (see next paragraphs) .  generate Erlblog configuration file under the SRC directory of Erlblog directory, Erlang virtual The machine launches the application based on this file the contents of the cat Src/erlblog.app.src file: {application, erlblog, [  {description, ""},  {VSN, "1"},   {registered, []},  {applications, [                  KERNEL,&NBSP ;                 stdlib,                &NB Sp cowboy                 ]},  {mod, {Erlblog_app, []}},  {env, [{HTT] P_port, 8080}]} ]}. Modify the START/2 function of the Erlblog_app.erl file, Erlblog to start a process pool when the cowboy application starts, The Receive user connection configures the cowboy routing dispatcher as a single path, all requests are routed to the root path "/" and the contents of the module are modified using Erlblog_handler processing user requests: $ cat src/erlblog_app.erl - Module (ERLBLOG_APP).  -behaviour (Application).  %% application Callbacks-export ([START/2, STOP/1]) .  -define (C_acceptors,  100). percent = = =================================================================%% Application callbacks%% ====================== ============================================= start (_starttype, _startargs)->    Routes    = routes (),    Dispatch  = cowboy_router:compile (routes),    Port      = PO RT (),    transopts = [{port, port}],    protoopts = [{env, [{dispatch, dispatch}]}],    {OK, _}   = Cowboy:start_http (http,? C_acceptors, Transopts, protoopts),    Erlblog_sup:start_link ().  stop (_state)->    OK.  %% ===================================================================%% Internal functions%% ============== =====================================================routes ()->    [     {' _ ', [  & nbsp         {"/", Erlblog_handler, []}           ]}   ]. por T ()->  &NBsp Case Os:getenv ("port") of        false->            {OK, port} = App Lication:get_env (http_port),            port;        other->  & nbsp         List_to_integer (other)     end.  Add request Processing module $ cat src/erlblog_handler.erl - Module (erlblog_handler).  -export ([INIT/3]).-export ([HANDLE/2]).-export ([TERMINATE/3]).  init (_ Transport, Req, [])->    {OK, Req, Undefined}. handle (Req, state)->    {OK, Req2} = COWBO Y_req:reply ($, [], << "Hello world!" >&gt, Req),    {OK, Req2, state}. terminate (_reason, _req, _state)->    OK. Finally, let's create an interface module which would be a responsible for starting your Erlblog application together with Al L its Dependencies. erlblog module contents: $ cat Src/erlblog.erl -module (erlblog).  -export ([start/0]) .  Start ()->&nbsP   OK = Application:start (crypto),    OK = Application:start (ranch),    OK = Application:start (co Wboy),    OK = Application:start (erlblog) .  use rebar to get cowboy programs and dependent programs and compile $./rebar get-deps compile  Launcher $ ERL-PA Ebin deps/*/ebin-s erlblog1> application:which_applications () .  finally use the browser to access the test/HTTP server ip:8080/   Return Hello, World says to use cowboy correctly

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.