Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
332 lines (258 sloc) 11.2 KB
out(A) ->
{ssi, "", "%%",[{"embed", "choosen"}]}.
<div id="entry">
<h1>Running yaws embedded in another application</h1>
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>In order to run Yaws inside another application, we need to
perform the following steps.
<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><p> Provide the application environment <code>{embedded,
true}</code> to Yaws.</p>
<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>
<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>
<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
<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>
<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>
<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
<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>
<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">
[{logdir, "/var/log/my_server"},
{ebin_dir, ["/example1/ebin", "/example2/ebin"]},
{id, "my_server"}].
<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">
[{docroot, "/var/yaws/www"},
{port, 8080},
{listen, {127,0,0,1}},
{appmods, [{"/", my_appmod}]}].
<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>
<li><p><code>SCList</code> is a list of <code>#sconf{}</code> records
created using the values from the passed-in server configuration
<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
<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">
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),
<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
<p>See the example below:</p>
<div class="box">
%% 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}]).
<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
<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
<p>If you compile both modules, you can run them as shown below:</p>
<div class="box">
1> {ok, Sup} = ybed_sup:start_link().
=INFO REPORT==== 12-Apr-2010::02:42:09 ===
Yaws: Listening to for <1> virtual servers:
- http://foobar:8888 under /tmp
<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
supervisor as the code in the <code>ybed</code> module itself.
<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,
<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">
SC#sconf{opaque = {mystruct, foobar},
<p>A <code>.yaws</code> web page can then do:</p>
<div class="box">
out(Arg) ->
MyStruct = Arg#arg.opaque
<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">
{appmods, [{"/", myapp, [["js"], ["top", "static"], ["icons"]]}]},
<p>or in <code>#sconf{}</code> record terms:</p>
<div class="box">
appmods = {"/", myapp, [["js"], ["top", "static"], ["icons"]]},
out(A) -> {ssi, "END2",[],[]}.
Something went wrong with that request. Please try again.