2016-04-19 6 views
1

私はyawsのドキュメントとWeb検索を使用してこの問題のトラブルシューティングを試みるのに数時間を費やしました。ここにある既存のスレッドは私を助けませんでした。yawを組み込みモードで起動するにはどうすればよいですか?

私はerlangに新しく、http://yaws.hyber.org/embed.yawsで提供されているサンプルコードを使用して埋め込みモードでヨーイングを実行しようとしています。私はそれを働かせることができないので、何かが欠けている。

ybed.app

{application, ybed_app, 
[ 
    {description, "Yaws Embedded Application Test"}, 
    {vsn, "0.1.0"}, 
    {registered, []}, 
    {applications, [kernel, stdlib, yaws]}, 
    {mod, {ybed_app, []}}, 
    {env, []} 
]}. 

ybed_app.erl

-module(ybed_app). 
-behaviour(application). 

%% Application callbacks 
-export([start/2, 
     stop/1]). 

start(_StartType, _StartArgs) -> 
    case ybed_sup:start_link() of 
     {ok, Pid} -> 
      {ok, Pid}; 
     Other -> 
      {error, Other} 
    end. 

stop(_State) -> 
    ok. 

ybed_sup.erl

-module(ybed_sup). 
-behaviour(supervisor). 

%% API 
-export([start_link/0]). 

%% Supervisor callbacks 
-export([init/1]). 

start_link() -> 
    supervisor:start_link({local, ?MODULE}, ?MODULE, []). 

init([]) -> 
    YBed = {ybed, {ybed,start,[]}, 
      permanent,2000,worker,[ybed]}, 
    {ok,{{one_for_all,0,1}, [YBed]}}. 

ybed.erl

-module(ybed). 
-compile(export_all). 

start() -> 
    {ok, spawn(?MODULE, run, [])}. 

run() -> 
    Id = "embedded", 
    GconfList = [{id, Id}], 
    Docroot = "/tmp", 
    SconfList = [{port, 8000}, 
       {servername, "foobar"}, 
       {listen, {127,0,0,1}}, 
       {docroot, Docroot}], 
    {ok, SCList, GC, ChildSpecs} = 
     yaws_api:embedded_start_conf(Docroot, SconfList, GconfList, Id), 
    [supervisor:start_child(ybed_sup, Ch) || Ch <- ChildSpecs], 
    yaws_api:setconf(GC, SCList), 
    {ok, self()}. 
:私は4つのファイルを持っています

私はそれをコンパイル(首尾よく)し、アプリケーションを起動しようとすると、私は戻り値を取得する:

{error,{not_loaded,yaws}} 

私はコンパイルybed.erlを実行しようとするとは、ybed:実行を()、私が手:

** exception error: undefined function yaws_api:embedded_start_conf/4 
    in function ybed:run/0 (src/ybed.erl, line 16) 

私がアプリケーションを起動する前にヨーイングを開始しても、まだ動作しません。

まだリリースをビルドしようとしていません。組み込みモードでヨーイングをコンパイルしてテストするだけです。私が何が欠けているか教えてもらえますか?あなたがエラー

** exception error: undefined function yaws_api:embedded_start_conf/4 
    in function ybed:run/0 (src/ybed.erl, line 16) 

を取得するとあなたは明確にあなたのcode server検索パスにyaws_api.beamを持っていない、事前

答えて

0

感謝。 erlを適切な-pa引数で開始するか、埋め込みモードを使用する予定がない場合は、アプリケーションの初期化でcode:add_patha/1を呼び出します。

yawsyawsのドキュメントに記載されていますが、完全なコードはありませんので、ここでは1つのモジュールで、きちんとしたデバッグリソースとRESTサービスの準備ができています。

-module(ybed_yaws). 

-behaviour(supervisor). 

-include_lib("yaws/include/yaws_api.hrl"). 

%% API 
-export([start_link/0]). 

%% Supervisor callbacks 
-export([init/1]). 

%% Internal functions export 
-export([init_yaws/1, out/1]). 

%%%=================================================================== 
%%% Defaults 
%%%=================================================================== 

default_global() -> 
    #{id => "yaws", logdir => "log"}. 

default_server() -> 
    #{port => 9900, 
     listen => {0,0,0,0}, 
     docroot => "www", 
     appmods => [{"/", ?MODULE}]}. 

%%%=================================================================== 
%%% API functions 
%%%=================================================================== 

start_link() -> 
    supervisor:start_link(?MODULE, []). 

%%%=================================================================== 
%%% Supervisor callbacks 
%%%=================================================================== 

init([]) -> 
    {ok, 
    {{one_for_all, 0, 1}, 
     [#{id => init_yaws, 
     start => {?MODULE, init_yaws, [self()]}, 
     restart => transient}] 
    }}. 

%%%=================================================================== 
%%% Internal functions 
%%%=================================================================== 

init_yaws(Sup) -> 
    {ok, proc_lib:spawn_link(fun() -> config(Sup) end)}. 

ensure_dir(Dir) -> 
    {ok, App} = application:get_application(), 
    D = filename:join([code:priv_dir(App), Dir]) 
    filelib:ensure_dir(filename:join([D, "foo"])), 
    D. 

config(Supervisor) -> 
    #{id := Id} = GCMap = default_global(), 
    #{docroot := DR} = SCMap = default_server(), 
    Docroot = ensure_dir(DR), 
    {ok, SC, GC, ChildSpecs} = 
     yaws_api:embedded_start_conf(
      Docroot, 
      maps:to_list(SCMap#{docroot => Docroot}), 
      maps:to_list(GCMap), 
      Id), 
    [supervisor:start_child(Supervisor, Ch) || Ch <- ChildSpecs], 
    yaws_api:setconf(GC, SC), 
    ok. 

-compile({inline, [h/1, f/2]}). 
h(A) when is_atom(A) -> h(atom_to_binary(A, latin1)); 
h(S) -> yaws_api:htmlize(S). 

f(Fmt, Args) -> yaws_api:f(Fmt, Args). 

box(Str) -> 
    {'div',[{class,"box"}], 
    {pre, [], h(Str)}}. 

out(A) -> 
    PathString = case A#arg.pathinfo of 
       undefined -> ""; 
       P -> P 
      end, 
    Path = string:tokens(PathString, "/"), 
    Method = A#arg.req#http_request.method, 
    out(A, Method, Path). 

out(A, Method, Path) -> 
    {ehtml, 
    {html, [], 
     [{head}, 
     {body, [], 
     [ 
     {h5, [], "Paths:"}, 
     {hr}, 
     box(f("Method = ~p~n" 
       "Path = ~p~n" 
       "A#arg.querydata = ~p~n", 
       [Method, 
       Path, 
       A#arg.querydata])), 
     {h5, [], "Headers:"}, 
     {hr}, 
     {ol, [], yaws_api:reformat_header(
        A#arg.headers, 
        fun(H, V)-> 
          {li, [], [h(H), ": ", {code, [], h(V)}]} 
        end 
        )} 
     ]} 
     ]} 
    }. 

yaws OTP準拠した一時的なプロセスではなく、gen_serverせずに初期化されるかの方法。

.configファイルに{yaws, [{embedded, true}]}を追加して、アプリケーション共通サービスの開始を開始しないようにしてください。yawsそれがなくても動作しますが、完全には埋め込まれません。

+0

ありがとうございます! VMやアプリケーションの設定にすべてのビームパスを含めるなど、私のようなnoobsにとってはちょっとしたことです。これは私の問題を解決しました。 – Will

関連する問題