使用rebar3创建erlang项目通过cowboy与页面交互

  1. 使用rebar3创建erlang项目
    1. rebar3 new app test_cowboy
  2. 修改rebar.config
    1. {plugins, [
              {rebar3_run, {git, "git://github.com/tsloughter/rebar3_run.git", {branch, "master"}}}
      ]}.
      {erl_opts, [debug_info]}.
      {deps, [
              {cowboy, {git, "git://github.com/extend/cowboy.git",{tag, "1.0.0"}}}
      ]}.
      {relx, [{release, {test_cowboy, "1.0.0"}, [test_cowboy]},
              {dev_mode, false},
              {include_erts, true},
              {system_libs, true},
              {include_src, false},
              {sys_config, "conf/sys.config"},
              {vm_args, "conf/vm.args"},
              {extended_start_script, true}
      ]}.
  3. 创建conf文件夹
    1. mkdir conf && cd conf
  4. 在conf下创建vm.args和sys.config文件
    1. vm.args
      1. ## Name of the node
        -name test_cowboy@127.0.0.1
        ## Cookie for distributed erlang
        -setcookie shadowolf
    2. sys.config
      1. [].
  5. 修改src/test_cowboy.app.src
    1. {application, test_cowboy,
      [{description, "Hello Erlang"},
          {vsn, "0.1.0"},
          {registered, []},
          {mod, { test_cowboy_app, []}},
          {applications,
              [   kernel,
                  stdlib,
                  cowboy
              ]},
          {env,[]},
          {modules, []}
      ]}.
  6. 修改src/test_cowboy_sup.erl
    1. -module(test_cowboy_sup).
      -behaviour(supervisor).
      -export([start_link/0]).
      -export([init/1]).
      -define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
      -define(SERVER, ?MODULE).
      start_link() ->
              supervisor:start_link({local, ?SERVER}, ?MODULE, []).
      init([]) ->
              {ok, { {one_for_all, 0, 1}, [?CHILD(test_cowboy_web_svr, worker)]} }.
  7. 新建src/test_cowboy_web_svr.erl文件
    1. -module(test_cowboy_web_svr).
      -author("zhangpeng"). -behaviour(gen_server).
      -export([start_link/0]).
      -export([init/1,
          handle_call/3,
          handle_cast/2,
          handle_info/2,
          terminate/2,
          code_change/3]).
      -define(SERVER, ?MODULE).
      -record(state, {}).
      start_link() ->
          gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
      init([]) ->
          Dispatch = cowboy_router:compile(
              [
                  {'_',
                      [
                          {"/test_cowboy_1", test_cowboy_1, []}
                      ]
                  }
              ]
          ),
          NbAcceptorts = 100,
          MaxConn = 1024 ,
          Port = 8003,
          {ok, _} = cowboy:start_http(test_cowboy_http, NbAcceptorts,
          [{port, Port},
          {max_connections, MaxConn}],
          [{env, [{dispatch, Dispatch}]}]),
          {ok, #state{}}.
      handle_call(_Request, _From, State) ->
          {reply, ok, State}.
      handle_cast(_Request, State) ->
          {noreply, State}.
      handle_info(_Info, State) ->
          {noreply, State}.
      terminate(_Reason, _State) ->
          ok.
      code_change(_OldVsn, State, _Extra) ->
          {ok, State}.
  8. 新建src/test_cowboy_1.erl
    1. -module(test_cowboy_1).
      -author("zhangpeng").
      -behaviour(cowboy_http_handler).
      -export([init/3]).
      -export([handle/2]).
      -export([terminate/3]).
      init(_, Req, _Opts) ->
          {ok, Req, undefined}.
      handle(Req, State) ->
          {Param1,Req1} = cowboy_req:qs_val(<<"param1">>,Req),
          {Param2,Req2} = cowboy_req:qs_val(<<"param2">>,Req1),
          {ok, ReqReturn} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/plain">>}], <<"Hello Erlang!">>, Req2),
          {ok, ReqReturn, State}.
      terminate(_Reason, _Req, _State) ->
          ok.

 

 

转载于:https://my.oschina.net/shadowolf/blog/847182

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值