Cowboy的使用

这一生的挚爱 提交于 2019-12-10 08:30:43
  1. 使用rebar3创建erlang项目

    rebar3 new app test_cowboy
    
  2. 修改rebar.config

    {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文件夹

    mkdir conf && cd conf
    
  4. 在conf下创建vm.args和sys.config文件

    • vm.args

      ## Name of the node
      -name test_cowboy@127.0.0.1
      ## Cookie for distributed erlang
      -setcookie shadowolf
      
    • sys.config

      [].
      
  5. 修改src/test_cowboy.app.src

    {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

    -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文件

    -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

    -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.
    
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!