0

Is there a way to run (the Erlang version of) Webmachine in an "embedded?" I want to embed the web app in an application I am writing. The web app will just be a front end communicating with a back end I am writing. I want everything (webmachine, mochiweb, custom web app, custom back end) in one code base, running on one virtual machine.

Thanks.

user3355020
  • 315
  • 1
  • 9

1 Answers1

1

Rebar offers convenient ways to build application releases and manage their dependencies. As the subject is quite complex, I suggest you to have a look at the very good site learnyousomeerlang (I pointed the chapter about releases) to learn about the underlying method used by Rebar. You will need also to have a good knowledge about the way the applications are start in OTP.

In your case, the back-end is the application, all the other applications are dependencies.

[edit] I wouldn't say you have to build an application in Erlang. As there is no link, all the module simply have to be in the code/library search path when the VM need them. It is even possible to modify this path while running.

The OTP define a standard way to organize the different files, and the VM defines a way to organize the libraries. What rebar does, is to help you to create and maintain this organization for your application and the ones it depends on. It helps you to retrieve from repositories the application you need, and simplifies the build of the whole thing. So it helps you to distribute your application to other users/machines.

You can have a look at the webmachine code itself, since it uses rebar to build iteself and get the application it depends on (mochiweb, meck and ibrowse). Here is a copy of some file available at https://github.com/basho/webmachine under Apache License, Version 2.0. The rebar.config file which describes the dependencies and the "compilation" options:

%%-*- mode: erlang -*-
{erl_opts, [warnings_as_errors]}.
{cover_enabled, true}.
{edoc_opts, [{preprocess, true}]}.

{xref_checks, [undefined_function_calls]}.

{deps,
 [{mochiweb, "1.5.1*", {git, "git://github.com/basho/mochiweb.git", {tag, "1.5.1p6"}}},
  {meck, "0.8.1", {git, "git://github.com/basho/meck.git", {tag, "0.8.1"}}},
  {ibrowse, "4.0.1", {git, "git://github.com/cmullaparthi/ibrowse.git", {tag, "v4.0.1"}}}
 ]}.

And here is the file webmachine.app.src which describes the application that should be running and the starting point of the application (webmachine_app:start/2)

%%-*- mode: erlang -*-
{application, webmachine,
 [
  {description, "webmachine"},
  {vsn, git},
  {modules, []},
  {registered, []},
  {applications, [kernel,
                  stdlib,
                  crypto,
                  mochiweb]},
  {mod, {webmachine_app, []}},
  {env, []}
 ]}.

and finally the code which starts everything:

%% @author Justin Sheehy <justin@basho.com>
%% @author Andy Gross <andy@basho.com>
%% @copyright 2007-2008 Basho Technologies
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

%% @doc Callbacks for the webmachine application.

-module(webmachine_app).
-author('Justin Sheehy <justin@basho.com>').
-author('Andy Gross <andy@basho.com>').

-behaviour(application).

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

-include("webmachine_logger.hrl").

%% @spec start(_Type, _StartArgs) -> ServerRet
%% @doc application start callback for webmachine.
start(_Type, _StartArgs) ->
    webmachine_deps:ensure(),
    {ok, _Pid} = SupLinkRes = webmachine_sup:start_link(),
    Handlers = case application:get_env(webmachine, log_handlers) of
        undefined ->
            [];
        {ok, Val} ->
            Val
    end,
    %% handlers failing to start are handled in the handler_watcher
    _ = [supervisor:start_child(webmachine_logger_watcher_sup,
                                [?EVENT_LOGGER, Module, Config]) ||
            {Module, Config} <- Handlers],
    SupLinkRes.

%% @spec stop(_State) -> ServerRet
%% @doc application stop callback for webmachine.
stop(_State) ->
    ok.
Pascal
  • 13,977
  • 2
  • 24
  • 32
  • Are you saying I should use rebar to somehow "build" a way to have webmachine embedded in my app? Isn't there some easier way? – user3355020 Mar 20 '14 at 17:08
  • See the edit. I hope it is Ok to reproduce code from the git repositery? – Pascal Mar 20 '14 at 20:50
  • Holy crap, my eyes just seriously glazed over. It seems there should be an easier way besides wading through all that crap. I guess nobody else has a need for this except me, so the developers didn't provide an easier method? I'd like to simply download some webmachine libraries and simply use them from my own application. – user3355020 Mar 20 '14 at 22:25
  • In this case, just put the applications in the search path (`erl -pa "path1" "path2 ..."`), and start them in your code (`application:start(webmachine), ...`). The draw back is if you want to go to a new machine, share your work, or use different versions. – Pascal Mar 21 '14 at 04:37