Why is my cowboy server not running?

2019-09-12 04:28发布

问题:

I'm trying to run my server. I copied my src files into a fresh cowboy install, this is the error I'm getting.

~/cowboy_ninja:.ls src
action_handler.erl  gate.beam       resolution.beam
arena.beam      gate.erl        resolution.erl
arena.erl       guestbook.beam      temple.beam
cowboy_ninja_app.erl    guestbook.erl       temple.erl
cowboy_ninja_sup.erl    hello_handler.erl
~/cowboy_ninja:.

cat src/cowboy_ninja_app.erl

-module(cowboy_ninja_app).
-behaviour(application).

-export([start/2]).
-export([stop/1]).

start(_Type, _Args) ->
    Dispatch = cowboy_router:compile([
        {'_', [
        {"/action", action_handler, []},
        {"/join", hello_handler, []}]}
    ]),
    {ok, _} = cowboy:start_clear(my_http_listener, 100,
        [{port, 8080}],
        #{env => #{dispatch => Dispatch}}
    ),
    {ok, Pid} = gate:start_link(),
    register(gate, Pid),
    {ok, Guestbook} = guestbook:start_link(),
    register(guestbook, Guestbook),
    gate:action(Pid, {fight}),
    cowboy_ninja_sup:start_link().

stop(_State) ->
    ok.

See below.

~/cowboy_ninja:.gmake run
===> Starting relx build process ...
===> Resolving OTP Applications from directories:
          /Users/quantum/cowboy_ninja/ebin
          /Users/quantum/cowboy_ninja/deps
          /usr/local/Cellar/erlang/19.1/lib/erlang/lib
          /Users/quantum/cowboy_ninja/apps
          /Users/quantum/cowboy_ninja/_rel
===> Resolved cowboy_ninja_release-1
===> Including Erts from /usr/local/Cellar/erlang/19.1/lib/erlang
===> release successfully created!
===> tarball /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release/cowboy_ninja_release-1.tar.gz successfully created!
Exec: /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release/erts-8.1/bin/erlexec -boot /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release/releases/1/cowboy_ninja_release -mode embedded -boot_var ERTS_LIB_DIR /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release/erts-8.1/../lib -config /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release/releases/1/sys.config -args_file /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release/releases/1/vm.args -- console
Root: /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release
/Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release
heart_beat_kill_pid = 52128
Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]


=INFO REPORT==== 1-Jan-2017::17:03:57 ===
    application: cowboy_ninja
    exited: {bad_return,
                {{cowboy_ninja_app,start,[normal,[]]},
                 {'EXIT',
                     {undef,
                         [{cowboy_router,compile,
                              [[{'_',
                                    [{"/action",action_handler,[]},
                                     {"/join",hello_handler,[]}]}]],
                              []},
                          {cowboy_ninja_app,start,2,
                              [{file,"src/cowboy_ninja_app.erl"},{line,8}]},
                          {application_master,start_it_old,4,
                              [{file,"application_master.erl"},
                               {line,273}]}]}}}}
    type: permanent
{"Kernel pid terminated",application_controller,"{application_start_failure,cowboy_ninja,{bad_return,{{cowboy_ninja_app,start,[normal,[]]},{'EXIT',{undef,[{cowboy_router,compile,[[{'_',[{\"/action\",action_handler,[]},{\"/join\",hello_handler,[]}]}]],[]},{cowboy_ninja_app,start,2,[{file,\"src/cowboy_ninja_app.erl\"},{line,8}]},{application_master,start_it_old,4,[{file,\"application_master.erl\"},{line,273}]}]}}}}}"}
Kernel pid terminated (application_controller) ({application_start_failure,cowboy_ninja,{bad_return,{{cowboy_ninja_app,start,[normal,[]]},{'EXIT',{undef,[{cowboy_router,compile,[[{'_',[{"/action",acti
heart: Sun Jan  1 17:03:58 2017: Erlang is crashing .. (waiting for crash dump file)
heart: Sun Jan  1 17:03:58 2017: Would reboot. Terminating.
gmake: *** [erlang.mk:6448: run] Error 1

It looks like it can't find my files? What is this error message saying?

回答1:

You need to create a cowboy_ninja.app file and list all the applications you are using (cowboy, etc.). Relx will look at that file to see which applications it needs to include in your release.

http://learnyousomeerlang.com/building-otp-applications

Currently it is not including the cowboy application or any of it's compiled modules into your release.

Here is an example from the cowboy examples

{application, 'hello_world', [
    {description, "Cowboy Hello World example"},
    {vsn, "1"},
    {modules, ['hello_world_app','hello_world_sup','toppage_handler']},
    {registered, [hello_world_sup]},
    {applications, [kernel,stdlib,cowboy]},
    {mod, {hello_world_app, []}},
    {env, []}
]}.

You also need to tell relx where to look for your applications (in your relx.config).

{lib_dirs, [
  "/usr/local/lib/elixir/lib/*/ebin", "./_build/prod/lib/*/ebin", "./deps/*/ebin"]}.

If you are not using a build tool for your app take a look at erlang.mk. it automatically creates an app file for you.



标签: erlang cowboy