Anonymous avatar Anonymous committed 80d04ed

initial webmachine app

Comments (0)

Files changed (15)

+.*\.beam
+.*~
+logs\/.*
+.DS_Store
+ebin\/.*
+priv\/log\/.*
+erl_crash.dump
+.*\.pyc
+.*\.orig
+
+% -*- mode: erlang -*-
+{["src/*"], 
+ [{i, "include"},
+  {outdir, "ebin"},
+  debug_info]
+}.
+ERL          ?= erl
+EBIN_DIRS    := $(wildcard deps/*/ebin)
+APP          := wmexamples
+
+all: erl ebin/$(APP).app
+
+erl:
+	@$(ERL) -pa $(EBIN_DIRS) -noinput +B \
+	  -eval 'case make:all() of up_to_date -> halt(0); error -> halt(1) end.'
+
+docs:
+	@erl -noshell -run edoc_run application '$(APP)' '"."' '[]'
+
+clean: 
+	@echo "removing:"
+	@rm -fv ebin/*.beam ebin/*.app
+
+ebin/$(APP).app: src/$(APP).app
+	@cp -v src/$(APP).app $@
+/Users/bryan/projects/code/webmachine/scripts/../ebin/..

priv/dispatch.conf

+{[], wmexamples_resource, []}.

priv/www/index.html

+<html>
+<head>
+<title>It Worked</title>
+</head>
+<body>
+MochiWeb running.
+</body>
+</html>

src/wmexamples.app

+{application, wmexamples,
+ [{description, "wmexamples"},
+  {vsn, "0.1"},
+  {modules, [
+    wmexamples,
+    wmexamples_app,
+    wmexamples_sup,
+    wmexamples_deps,
+    wmexamples_resource
+  ]},
+  {registered, []},
+  {mod, {wmexamples_app, []}},
+  {env, []},
+  {applications, [kernel, stdlib, crypto]}]}.

src/wmexamples.erl

+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc TEMPLATE.
+
+-module(wmexamples).
+-author('author <author@example.com>').
+-export([start/0, stop/0]).
+
+ensure_started(App) ->
+    case application:start(App) of
+	ok ->
+	    ok;
+	{error, {already_started, App}} ->
+	    ok
+    end.
+	
+%% @spec start() -> ok
+%% @doc Start the wmexamples server.
+start() ->
+    wmexamples_deps:ensure(),
+    ensure_started(crypto),
+    ensure_started(webmachine),
+    application:start(wmexamples).
+
+%% @spec stop() -> ok
+%% @doc Stop the wmexamples server.
+stop() ->
+    Res = application:stop(wmexamples),
+    application:stop(webmachine),
+    application:stop(crypto),
+    Res.

src/wmexamples.hrl

+

src/wmexamples_app.erl

+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Callbacks for the wmexamples application.
+
+-module(wmexamples_app).
+-author('author <author@example.com>').
+
+-behaviour(application).
+-export([start/2,stop/1]).
+
+
+%% @spec start(_Type, _StartArgs) -> ServerRet
+%% @doc application start callback for wmexamples.
+start(_Type, _StartArgs) ->
+    wmexamples_deps:ensure(),
+    wmexamples_sup:start_link().
+
+%% @spec stop(_State) -> ServerRet
+%% @doc application stop callback for wmexamples.
+stop(_State) ->
+    ok.

src/wmexamples_deps.erl

+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Ensure that the relatively-installed dependencies are on the code
+%%      loading path, and locate resources relative
+%%      to this application's path.
+
+-module(wmexamples_deps).
+-author('author <author@example.com>').
+
+-export([ensure/0, ensure/1]).
+-export([get_base_dir/0, get_base_dir/1]).
+-export([local_path/1, local_path/2]).
+-export([deps_on_path/0, new_siblings/1]).
+
+%% @spec deps_on_path() -> [ProjNameAndVers]
+%% @doc List of project dependencies on the path.
+deps_on_path() ->
+    F = fun (X, Acc) ->
+                ProjDir = filename:dirname(X),
+                case {filename:basename(X),
+                      filename:basename(filename:dirname(ProjDir))} of
+                    {"ebin", "deps"} ->
+                        [filename:basename(ProjDir) | Acc];
+                    _ ->
+                        Acc
+                end
+        end,
+    ordsets:from_list(lists:foldl(F, [], code:get_path())).
+    
+%% @spec new_siblings(Module) -> [Dir]
+%% @doc Find new siblings paths relative to Module that aren't already on the
+%%      code path.
+new_siblings(Module) ->
+    Existing = deps_on_path(),
+    SiblingEbin = filelib:wildcard(local_path(["deps", "*", "ebin"], Module)),
+    Siblings = [filename:dirname(X) || X <- SiblingEbin,
+                           ordsets:is_element(
+                             filename:basename(filename:dirname(X)),
+                             Existing) =:= false],
+    lists:filter(fun filelib:is_dir/1, 
+                 lists:append([[filename:join([X, "ebin"]),
+                                filename:join([X, "include"])] ||
+                                  X <- Siblings])).
+        
+
+%% @spec ensure(Module) -> ok
+%% @doc Ensure that all ebin and include paths for dependencies
+%%      of the application for Module are on the code path.
+ensure(Module) ->
+    code:add_paths(new_siblings(Module)),
+    code:clash(),
+    ok.
+
+%% @spec ensure() -> ok
+%% @doc Ensure that the ebin and include paths for dependencies of
+%%      this application are on the code path. Equivalent to
+%%      ensure(?Module).
+ensure() ->
+    ensure(?MODULE).
+
+%% @spec get_base_dir(Module) -> string()
+%% @doc Return the application directory for Module. It assumes Module is in
+%%      a standard OTP layout application in the ebin or src directory.
+get_base_dir(Module) ->
+    {file, Here} = code:is_loaded(Module),
+    filename:dirname(filename:dirname(Here)).
+
+%% @spec get_base_dir() -> string()
+%% @doc Return the application directory for this application. Equivalent to
+%%      get_base_dir(?MODULE).
+get_base_dir() ->
+    get_base_dir(?MODULE).
+
+%% @spec local_path([string()], Module) -> string()
+%% @doc Return an application-relative directory from Module's application.
+local_path(Components, Module) ->
+    filename:join([get_base_dir(Module) | Components]).
+
+%% @spec local_path(Components) -> string()
+%% @doc Return an application-relative directory for this application.
+%%      Equivalent to local_path(Components, ?MODULE).
+local_path(Components) ->
+    local_path(Components, ?MODULE).

src/wmexamples_resource.erl

+%% @author author <author@example.com>
+%% @copyright YYYY author.
+%% @doc Example webmachine_resource.
+
+-module(wmexamples_resource).
+-export([init/1, to_html/2]).
+
+-include_lib("webmachine/include/webmachine.hrl").
+
+init([]) -> {ok, undefined}.
+
+to_html(ReqData, State) ->
+    {"<html><body>Hello, new world</body></html>", ReqData, State}.

src/wmexamples_sup.erl

+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Supervisor for the wmexamples application.
+
+-module(wmexamples_sup).
+-author('author <author@example.com>').
+
+-behaviour(supervisor).
+
+%% External exports
+-export([start_link/0, upgrade/0]).
+
+%% supervisor callbacks
+-export([init/1]).
+
+%% @spec start_link() -> ServerRet
+%% @doc API for starting the supervisor.
+start_link() ->
+    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% @spec upgrade() -> ok
+%% @doc Add processes if necessary.
+upgrade() ->
+    {ok, {_, Specs}} = init([]),
+
+    Old = sets:from_list(
+	    [Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]),
+    New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]),
+    Kill = sets:subtract(Old, New),
+
+    sets:fold(fun (Id, ok) ->
+		      supervisor:terminate_child(?MODULE, Id),
+		      supervisor:delete_child(?MODULE, Id),
+		      ok
+	      end, ok, Kill),
+
+    [supervisor:start_child(?MODULE, Spec) || Spec <- Specs],
+    ok.
+
+%% @spec init([]) -> SupervisorTree
+%% @doc supervisor callback.
+init([]) ->
+    Ip = case os:getenv("WEBMACHINE_IP") of false -> "0.0.0.0"; Any -> Any end,   
+    {ok, Dispatch} = file:consult("priv/dispatch.conf"),
+    WebConfig = [
+		 {ip, Ip},
+		 {port, 8000},
+                 {log_dir, "priv/log"},
+		 {dispatch, Dispatch}],
+    Web = {webmachine_mochiweb,
+	   {webmachine_mochiweb, start, [WebConfig]},
+	   permanent, 5000, worker, dynamic},
+    Processes = [Web],
+    {ok, {{one_for_one, 10, 10}, Processes}}.
+#!/bin/sh
+cd `dirname $0`
+exec erl -pa $PWD/ebin $PWD/deps/*/ebin $PWD/deps/*/deps/*/ebin -boot start_sasl -s reloader -s wmexamples
+#!/bin/sh
+cd `dirname $0`
+exec erl -pa $PWD/ebin $PWD/deps/*/ebin $PWD/deps/*/deps/*/ebin -boot start_sasl -s wmexamples
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.