<erl>
out(A) ->
       {ssi, "TAB.inc", "%%",[{"embed", "choosen"}]}.
</erl>


<div id="entry">


  <h1>Running yaws embedded in another application</h1>
  <p>
    Yaws is ideal for embedding within another larger erlang application.
    Many typical erlang applications are control applications in need of a
    webgui specific to the actual application.
  </p>

  <p>In order to run Yaws inside another application, we need to
    perform  the following steps.
  </p>

  <ol>
    <li> <p>Either integrate Yaws into the build system of the larger
    application, or specifically provide the <code>ebin</code> path to Yaws
    for the larger application. </p>
    </li>

    <li><p> Provide the application environment <code>{embedded,
    true}</code> to Yaws.</p>
    </li>
  </ol>

  <p>Since the containing application typically has its configuration data
  fed from internal databases or other sources, it's usually not feasible
  to let Yaws read its configuration data from
  <code>/etc/yaws/yaws.conf</code> when it's running in embedded mode.</p>

  <p>To solve this, when Yaws is started in embedded mode, it doesn't read
  its configuration from <code>/etc/yaws/yaws.conf</code>, but rather it
  expects the larger application to feed its configuration through the
  function call <code>yaws_api:setconf(GC, Groups)</code>. The two
  arguments to this function are:</p>
  <ul>
    <li><p><code>GC</code>, which is a <code>#gconf{}</code> record</p></li>
    <li><p><code>Groups</code>, which is a list of lists of <code>#sconf</code>
    records</p></li>
  </ul>

  <p>The details of these records are unimportant, and we'll talk more
  about the <code>yaws_api:setconf</code> function later. First, let's
  discuss two ways applications can start Yaws in embedded mode.</p>

  <h2>Starting under your own supervisor</h2>

  <p>When not embedded, Yaws starts and runs as a regular application, but
  typically an application embedding Yaws wants to control it under its own
  supervisor(s). This means that an embedding application requires access
  to the Yaws supervisor child specifications. The exact list of Yaws child
  specifications depends on how the application intends to configure
  Yaws.</p>

  <p>The <code>yaws_api:embedded_start_conf/1,2,3,4</code> functions return
  the information an application needs to configure Yaws and start it under
  application supervisors. There are four variants of this function:</p>

  <ol>

    <li><p><code>yaws_api:embedded_start_conf/1</code> takes a single
    argument, which is the document root path for the web server. This
    variant uses default values for the <code>#gconf{}</code> and
    <code>#sconf{}</code> records.</p></li>

    <li><p><code>yaws_api:embedded_start_conf/2</code> takes a document
    root, same as the first variant above, and also a server configuration
    list. Such a list is either a list of properties for a single web
    server, or a list of property lists for multiple servers. We'll explain
    more about server configuration lists later, but for now note that
    they're used to create suitable <code>#sconf{}</code> record
    values. This variant uses a default value of the <code>#gconf{}</code>
    record.</p></li>

    <li><p><code>yaws_api:embedded_start_conf/3</code> takes a document
    root and a server configuration list, same as the second variant above,
    and also a global configuration list. Such a list is a property list
    that provides global configuration settings for the embedded Yaws
    instance, and is used to create a suitable <code>#gconf{}</code> record
    value. We'll explain more about global configuration lists
    later.</p></li>

    <li><p><code>yaws_api:embedded_start_conf/4</code>, the final variant,
    takes the same 3 arguments as the previous variant and also takes a
    string to identify the embedded Yaws instance.</p>
  </ol>

  <p>The values returned from these functions are described later.</p>

  <h3>Global configuration list</h3>

  <p>A global configuration list is a property list that provides global
  configuration settings for an embedded Yaws instance. Each property is a
  tuple consisting of property name and value. Allowed property names are
  those of the field names of the <code>#gconf{}</code> record type; see
  <code>yaws.hrl</code> for more details. An example global configuration
  list is shown below:</p>

  <div class="box">
    <pre>
      [{logdir, "/var/log/my_server"},
       {ebin_dir, ["/example1/ebin", "/example2/ebin"]},
       {id, "my_server"}].
    </pre>
  </div>
  <br/>

  <h3>Server configuration list</h3>

  <p>A server configuration list is a property list that provides
  configuration settings for a given web server instance. Because Yaws
  supports multiple servers simultaneously listening for requests, it's
  possible to supply a list of server configuration lists so that multiple
  servers can be configured in a single <code>yaws_api:setconf</code>
  function call. Each element in a server configuration list is a tuple
  consisting of property name and value. Allowed property names are those
  of the field names of the <code>#sconf{}</code> record type; see
  <code>yaws.hrl</code> for more details. An example server configuration
  list is shown below:</p>

  <div class="box">
    <pre>
      [{docroot, "/var/yaws/www"},
       {port, 8080},
       {listen, {127,0,0,1}},
       {appmods, [{"/", my_appmod}]}].
    </pre>
  </div>
  <br/>

  <p>You can often determine the correct embedded server configuration by
  first creating a <code>yaws.conf</code> file specifying the desired
  configuration, running a stand-alone Yaws using it, and then dumping the
  server's configuration by executing the command <code>yaws
  --running-config</code>. Still, some elements of the embedded server
  configuration list are non-obvious. For example, to enable basic auth,
  you need to specify an <code>auth</code> proplist for each target
  directory. For example, to enable basic auth on "/":</p>

  <div class="box">
    <pre>
      Docroot = "/var/yaws/www",
      Realm = "testrealm",
      SConfList = [{docroot, Docroot},
                   {port, 8080},
                   {listen, {0,0,0,0}},
                   {auth, [{docroot, Docroot},
                           {dir, "/"},
                           {realm, Realm},
                           {type, "Basic"},
                           {headers, ["WWW-Authenticate: Basic realm=\"", Realm,
                                      ["\"\r\n"]]},
                           {users, [{"foo","bar"}]}]}],
    </pre>
  </div>

  <p>If you want to enable auth on multiple directories, specify a <code>
  {auth, AuthProplist}</code> element for each one.</p>

  <p>Other elements, such as <code>dir_listings</code>,
  <code>access_log</code>, and <code>deflate</code>, are also non-obvious
  for embedded configuration because they're specified as part of the
  <code>#sconf.flags</code> element. You can find the server configuration
  flags, along with preprocessor macros for setting them on an
  <code>#sconf{}</code> record, in the <code>yaws.hrl</code> include
  file.</p>

  <h3>Using embedded_start_conf</h3>

  <p>The <code>yaws_api:embedded_start_conf/1,2,3,4</code> functions return
  <code>{ok, SCList, GC, ChildSpecs}</code>. The latter three elements of
  this tuple are described below.</p>

  <ul>

    <li><p><code>SCList</code> is a list of <code>#sconf{}</code> records
    created using the values from the passed-in server configuration
    lists</p></li>

    <li><p><code>GC</code> is a <code>#gconf{}</code> record created using
    the values from the passed-in global configuration list</p></li>

    <li><p><code>ChildSpecs</code> is a list of supervisor child
    specifications for the components of Yaws the application wants to
    start</p></li>

  </ul>

  <p>Below is an example of using the
  <code>yaws_api:embedded_start_conf/1,2,3,4</code> functions. It follows
  the steps of obtaining the embedded configuration and child
  specifications, starting the Yaws children under its own supervisor, and
  then setting the Yaws configuration.</p>

  <div class="box">
    <pre>
Id = "my_server",
GconfList = [{logdir, "/var/log/my_server"},
             {ebin_dir, ["/example1/ebin", "/example2/ebin"]},
             {id, Id}],
Docroot = "/var/yaws/www",
SconfList = [{docroot, Docroot},
             {port, 8080},
             {listen, {127,0,0,1}},
             {appmods, [{"/", my_appmod}]}],
{ok, SCList, GC, ChildSpecs} =
    yaws_api:embedded_start_conf(Docroot, SconfList, GconfList, Id),

%% assume our supervisor is registered as my_sup
[supervisor:start_child(my_sup, Ch) || Ch <- ChildSpecs],

%% now configure Yaws
yaws_api:setconf(GC, SCList),
    </pre>
  </div>

  <h2>Starting yaws as an embedded application</h2>

  <p>The four functions <code>yaws:start_embedded/1,2,3,4</code> start Yaws
  in embedded mode using <code>application:start</code>. This approach
  differs from the one above in that the embedding application need not
  start any Yaws components under its own supervisors, nor does it need to
  explicitly call <code>yaws:setconf</code> to set the Yaws
  configuration. This approach is slightly simpler but also gives the
  embedding application less control over Yaws.</p>

  <p>The arguments for these four functions are identical to those for the
  <code>yaws_api:embedded_start_conf/1,2,3,4</code> functions described
  earlier.</p>

  <p>See the example below:</p>

<div class="box">
  <pre>
%%
%% Check with inet:i(). that you are listening to port 8000!
%%
1> yaws:start_embedded("/home/tobbe/docroot").

%%
%% Alternative ways
%%
1> yaws:start_embedded("/home/tobbe/docroot",
                       [{servername, "sej"}, {listen, {0,0,0,0}}]).

1> yaws:start_embedded("/home/tobbe/docroot",
                       [{servername, "sej"}, {auth_log, false},
                        {listen, {0,0,0,0}}],
                       [{copy_errlog, false}]).

  </pre>
</div>

  <p>If you need more control on how to setup Yaws in embedded mode, use
  the <code>yaws_api:embedded_start_conf</code> functions instead.


  <h2>A very small example</h2>

  <p>We provide a minimal example that embeds Yaws in a small Erlang
  function.</p>

  <p>The <code>ybed</code> module is very small and is named <a
  href="code.yaws?file=/ybed.erl"><code>ybed.erl</code></a>. It has an
  accompanying simple supervisor named <a
  href="code.yaws?file=/ybed_sup.erl"><code>ybed_sup.erl</code></a>.</p>

  <p>If you compile both modules, you can run them as shown below:</p>

<div class="box">
  <verbatim>
1> {ok, Sup} = ybed_sup:start_link().
{ok,<0.40.0>}
2>
=INFO REPORT==== 12-Apr-2010::02:42:09 ===
Yaws: Listening to 0.0.0.0:8888 for <1> virtual servers:
 - http://foobar:8888 under /tmp

2>
  </verbatim>
</div>

<p>The actual web server runs inside the larger application. The
configuration of the web server was programmatically fed into Yaws from the
surrounding application, in this case, the <a
href="code.yaws?file=/ybed.erl"><code>ybed.erl</code></a> module. Note also
how the Yaws children are started under the same <a
href="code.yaws?file=/ybed_sup.erl"><code>ybed_sup.erl</code></a>
supervisor as the code in the <code>ybed</code> module itself. The
<code>ybed</code> serves only to start the Yaws children that need to
be run under its supervisor, after which it exits normally. For this reason,
its child specification uses the <code>temporary</code> restart strategy,
since it need run only once in the lifetime of its supervisor.

<h2>The opaque field in the sconf structure </h2>

<p>The <code>#sconf{}</code> record, which is constructed by the program
that starts and configures Yaws, contains a field,
<code>SC#sconf.opaque</code>.</p>

<p>This field is passed into the <code>#arg{}</code> record, so that any
application specific configuration data which is needed by the
<code>.yaws</code> pages that make up the web GUI application, is easily
available there.</p>


<p>In essence, if we construct the <code>#sconf</code> as</p>

<div class="box">
  <verbatim>
SC#sconf{opaque = {mystruct, foobar},
         .....
  </verbatim>
</div>


<p>A <code>.yaws</code> web page can then do:</p>

<div class="box">
  <verbatim>
out(Arg) ->
   MyStruct = Arg#arg.opaque
   .....

  </verbatim>
</div>

<p>thus passing data from the surrounding applications configuration
routines down to each <code>.yaws</code> web page.</p>

<p>Another important fact to consider when choosing whether to run your
Yaws application as an embedded yaws app or not is that all the Yaws
control functions are disabled when we use Yaws as an embedded web server,
including capabilities such as <code>yaws --ls</code> and <code>yaws
--stop</code>. Embedding thusassumes that you already have support for this
type of functionality in your application.</p>

<p>Finally, an interesting appmod definition that may apply to many
embedded yaws installations is the <code>/</code> appmod with a set of
exclude dirs. Here is an example server configuration list:</p>

<div class="box">
  <verbatim>
    [...
     {appmods, [{"/", myapp, [["js"], ["top", "static"], ["icons"]]}]},
     ...].
  </verbatim>
</div>

<p>or in <code>#sconf{}</code> record terms:</p>

<div class="box">
  <verbatim>
SC#sconf{.....
         appmods = {"/", myapp, [["js"], ["top", "static"], ["icons"]]},
         ....
  </verbatim>
</div>


</div>


<erl>
out(A) -> {ssi, "END2",[],[]}.
</erl>
