带有牛仔 2.0.0 的 Erlang 简单版本不起作用

Erlang simple release with cowboy 2.0.0 do not work

我正在使用 Erlang/OTP 20.0 和 rebar3。当我启动一个使用 cowboy 2.0.0 的新版本时,该版本无法启动。

以下是我构建项目的步骤。怎么了?

  1. 创建发布项目

    $ rebar3 new release cowboy2
    
  2. 添加牛仔包

    {deps, [{cowboy, {git, "https://github.com/ninenines/cowboy.git", {tag, "2.0.0"}}}]}.
    
  3. 添加基本调度程序

    start(_StartType, _StartArgs) ->
        Dispatch = cowboy_router:compile([
                {'_', [
                    {"/", toppage_handler, []}
                ]}
            ]),
        {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{
            env => #{dispatch => Dispatch}
        }),
        cowboy2_sup:start_link().
    
  4. 添加处理程序。出于本示例的目的,我使用 (https://raw.githubusercontent.com/ninenines/cowboy/master/examples/hello_world/src/toppage_handler.erl)

  5. 编译发布

    $ rebar3 compile && rebar3 release
    
  6. 运行 申请

    $ ./_build/default/rel/cowboy2/bin/cowboy2-0.1.0 console
    

输出

$ ./_build/default/rel/cowboy2/bin/cowboy2-0.1.0 console
Exec: /home/deimos/.asdf/installs/erlang/20.0/lib/erlang/erts-9.0/bin/erlexec 
-boot /home/deimos/Dev/personal/cowboy2/_build/default/rel/cowboy2/releases/0.1.0/cowboy2
-mode embedded -boot_var ERTS_LIB_DIR /home/deimos/.asdf/installs/erlang/20.0/lib/erlang/lib 
-config /home/deimos/Dev/personal/cowboy2/_build/default/rel/cowboy2/releases/0.1.0/sys.config 
-args_file /home/deimos/Dev/personal/cowboy2/_build/default/rel/cowboy2/releases/0.1.0/vm.args 
-pa -- console

Root: /home/deimos/Dev/personal/cowboy2/_build/default/rel/cowboy2
/home/deimos/Dev/personal/cowboy2/_build/default/rel/cowboy2
Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:30] [hipe] [kernel-poll:true]


=INFO REPORT==== 11-Oct-2017::21:00:52 ===
    application: cowboy2
    exited: {bad_return,
                {{cowboy2_app,start,[normal,[]]},
                 {'EXIT',
                     {undef,
                         [{cowboy_router,compile,
                              [[{'_',[{"/",toppage_handler,[]}]}]],
                              []},
                          {cowboy2_app,start,2,
                                  [{file,
                                   "/home/deimos/Dev/personal/cowboy2/_build/default/lib/cowboy2/src/cowboy2_app.erl"},
                                   {line,18}]},
                          {application_master,start_it_old,4,
                              [{file,"application_master.erl"},
                               {line,273}]}]}}}}
    type: permanent
    (...)
    Kernel pid terminated (application_controller)
     ({application_start_failure,cowboy2,{bad_return,{{cowboy2_app,start,[normal,[]]},{'EXIT',{undef,[{cowboy_router,compile,[[{'_',[{"/",toppage_handler,[]}]

Crash dump is being written to: erl_crash.dump...done

正如@maze-le 指出的那样,问题是 cowboy 应用程序没有启动。解决方案是将 cowboy 添加到文件 apps/cowboy2/src/cowboy2.app.src 中,该文件也是由命令 $rebar3 new release cowboy2 生成的。我添加文件是为了完整性。

apps/cowboy2/src/cowboy2.app.src

{application, cowboy2,
 [{description, "An OTP application"},
  {vsn, "0.1.0"},
  {registered, []},
  {mod, { cowboy2_app, []}},
  {applications,
   [kernel,
    stdlib,
    cowboy
   ]},
  {env,[]},
  {modules, []},

  {maintainers, []},
  {licenses, ["Apache 2.0"]},
  {links, []}
 ]}.