embedded モードで appmods 使う

Yaws を embedded モードで動かす為のメモ(適当)

appmods で使う為のモジュール

適当に、arg を出力するモジュールを準備する。

-module(put_arg).
-author('cooldaemon@gmail.com').

-include("/path/to/yaws/include/yaws_api.hrl").
-export([out/1]).

out(A) ->
  Peer = A#arg.client_ip_port,
  Req = A#arg.req,
  H = yaws_api:reformat_header(A#arg.headers),
  {ehtml, [
    {html, [], [
      {head, [], {title, [], "put arg"}},
      {body, [], [
        {h5, [], "The headers passed to us were:"},
        {hr, [], []},
        {ol, [], lists:map(fun(S) -> {li, [], {p, [], S}} end, H)},
        {h5, [], "The request"},
        {ul, [],
          [
            {li, [], yaws_api:f("method: ~s", [Req#http_request.method])},
            {li, [], yaws_api:f("path: ~p", [Req#http_request.path])},
            {li, [], yaws_api:f("version: ~p", [Req#http_request.version])}
          ]
        },
        {hr,[],[]},
        {h5, [], "Other items"},
        {ul, [],
          [
            {li, [], yaws_api:f("Peer: ~p", [Peer])},
            {li, [], yaws_api:f("docroot: ~s", [A#arg.docroot])},
            {li, [], yaws_api:f("fullpath: ~s", [A#arg.fullpath])},
            {li, [], yaws_api:f("appmoddata: ~s", [A#arg.appmoddata])}
          ]
        },
        {hr, [], []},
        {h5, [], "Parsed query data"},
        {pre, [], yaws_api:f("~p", [yaws_api:parse_query(A)])},
        {hr, [], []},
        {h5, [], "Parsed POST data"},
        {pre, [], yaws_api:f("~p", [yaws_api:parse_post(A)])}
      ]}
    ]}
  ]}.

Yaws を起動するモジュール

-module(embedded_yaws).
-author('cooldaemon@gmail.com').

-include("/path/to/yaws/include/yaws.hrl").
-export([start/0]).

start() ->
  application:start(yaws),

  GC = #gconf{
    yaws_dir = "/path/to/yaws",
    trace    = false,
    logdir   = ".",
    ebin_dir = ["/path/to/ebin"],
    yaws     = "Yaws 1.74",
    tmpdir   = "/path/to/.yaws"
  },

  SC = #sconf{
    port       = 80,
    servername = "localhost",
    listen     = {0,0,0,0},
    docroot    = "/path/to/docroot",
    appmods    = [{"/", put_arg}]
  },

  yaws_api:setconf(GC, [[SC]]).
ebin_dir put_arg.beam が存在するディレクトリのパス
docroot appmods で '/' を使っているので、/tmp でも何でも良い

yaws を起動

> sudo erl -pa /path/to/yaws/ebin -yaws embedded true -s embedded_yaws
Password:
Erlang (BEAM) emulator version 5.6 [source] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.6  (abort with ^G)
1> =INFO REPORT==== 31-Jan-2008::19:46:03 ===
Yaws: Listening to 0.0.0.0:80 for servers
 - http://localhost under /path/to/docroot