Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial skeleton

  • Loading branch information...
commit 2324b2be0d827d15f3d435217c68fb079afc4a36 0 parents
@abhay abhay authored
5 Makefile
@@ -0,0 +1,5 @@
+all:
+ (cd src;$(MAKE))
+
+clean:
+ (cd src;$(MAKE) clean)
8 conf/rpc_server.config
@@ -0,0 +1,8 @@
+%% rotating log and minimal tty
+[{sasl, [
+ {sasl_error_logger, false},
+ {errlog_type, error},
+ {error_logger_mf_dir,"log"},
+ {error_logger_mf_maxbytes,52428800}, % 50 MB
+ {error_logger_mf_maxfiles, 10}
+]}].
1  deps/mochiweb-src
8 priv/www/index.html
@@ -0,0 +1,8 @@
+<html>
+<head>
+<title>It Worked</title>
+</head>
+<body>
+MochiWeb running.
+</body>
+</html>
9 src/Makefile
@@ -0,0 +1,9 @@
+include ../support/include.mk
+
+all: $(EBIN_FILES)
+
+debug:
+ $(MAKE) DEBUG=-DDEBUG
+
+clean:
+ rm -rf $(EBIN_FILES)
14 src/rpc_server.app
@@ -0,0 +1,14 @@
+{application, rpc_server,
+ [{description, "rpc_server"},
+ {vsn, "0.01"},
+ {modules, [
+ rpc_server,
+ rpc_server_app,
+ rpc_server_sup,
+ rpc_server_web,
+ rpc_server_deps
+ ]},
+ {registered, []},
+ {mod, {rpc_server_app, []}},
+ {env, []},
+ {applications, [kernel, stdlib, crypto]}]}.
30 src/rpc_server.erl
@@ -0,0 +1,30 @@
+%% @author Abhay Kumar <abhay@opensynapse.net>
+%% @copyright 2008 Abhay Kumar
+
+%% @doc TEMPLATE.
+
+-module(rpc_server).
+-author("Abhay Kumar <abhay@opensynapse.net>").
+-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 rpc_server server.
+start() ->
+ rpc_server_deps:ensure(),
+ ensure_started(crypto),
+ application:start(rpc_server).
+
+%% @spec stop() -> ok
+%% @doc Stop the rpc_server server.
+stop() ->
+ Res = application:stop(rpc_server),
+ application:stop(crypto),
+ Res.
1  src/rpc_server.hrl
@@ -0,0 +1 @@
+
22 src/rpc_server_app.erl
@@ -0,0 +1,22 @@
+%% @author Abhay Kumar <abhay@opensynapse.net>
+%% @copyright 2008 Abhay Kumar
+
+%% @doc Callbacks for the rpc_server application.
+
+-module(rpc_server_app).
+-author("Abhay Kumar <abhay@opensynapse.net>").
+
+-behaviour(application).
+-export([start/2,stop/1]).
+
+
+%% @spec start(_Type, _StartArgs) -> ServerRet
+%% @doc application start callback for rpc_server.
+start(_Type, _StartArgs) ->
+ rpc_server_deps:ensure(),
+ rpc_server_sup:start_link().
+
+%% @spec stop(_State) -> ServerRet
+%% @doc application stop callback for rpc_server.
+stop(_State) ->
+ ok.
84 src/rpc_server_deps.erl
@@ -0,0 +1,84 @@
+%% @author Abhay Kumar <abhay@opensynapse.net>
+%% @copyright 2008 Abhay Kumar
+
+%% @doc Ensure that the relatively-installed dependencies are on the code
+%% loading path, and locate resources relative
+%% to this application's path.
+
+-module(rpc_server_deps).
+-author("Abhay Kumar <abhay@opensynapse.net>").
+
+-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).
54 src/rpc_server_sup.erl
@@ -0,0 +1,54 @@
+%% @author Abhay Kumar <abhay@opensynapse.net>
+%% @copyright 2008 Abhay Kumar
+
+%% @doc Supervisor for the rpc_server application.
+
+-module(rpc_server_sup).
+-author("Abhay Kumar <abhay@opensynapse.net>").
+
+-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("MOCHIWEB_IP") of false -> "0.0.0.0"; Any -> Any end,
+ WebConfig = [
+ {ip, Ip},
+ {port, 8000},
+ {docroot, rpc_server_deps:local_path(["priv", "www"])}],
+ Web = {rpc_server_web,
+ {rpc_server_web, start, [WebConfig]},
+ permanent, 5000, worker, dynamic},
+
+ Processes = [Web],
+ {ok, {{one_for_one, 10, 10}, Processes}}.
43 src/rpc_server_web.erl
@@ -0,0 +1,43 @@
+%% @author Abhay Kumar <abhay@opensynapse.net>
+%% @copyright 2008 Abhay Kumar
+
+%% @doc Web server for rpc_server.
+
+-module(rpc_server_web).
+-author("Abhay Kumar <abhay@opensynapse.net>").
+
+-export([start/1, stop/0, loop/2]).
+
+%% External API
+
+start(Options) ->
+ {DocRoot, Options1} = get_option(docroot, Options),
+ Loop = fun (Req) ->
+ ?MODULE:loop(Req, DocRoot)
+ end,
+ mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
+
+stop() ->
+ mochiweb_http:stop(?MODULE).
+
+loop(Req, DocRoot) ->
+ "/" ++ Path = Req:get(path),
+ case Req:get(method) of
+ Method when Method =:= 'GET'; Method =:= 'HEAD' ->
+ case Path of
+ _ ->
+ Req:serve_file(Path, DocRoot)
+ end;
+ 'POST' ->
+ case Path of
+ _ ->
+ Req:not_found()
+ end;
+ _ ->
+ Req:respond({501, [], []})
+ end.
+
+%% Internal API
+
+get_option(Option, Options) ->
+ {proplists:get_value(Option, Options), proplists:delete(Option, Options)}.
3  start-dev.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+cd `dirname $0`
+exec erl +Bc +K true -smp auto -pa $PWD/ebin $PWD/deps/*/ebin -boot start_sasl -name rpc_server -config conf/rpc_server -s reloader -s rpc_server
3  start.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+cd `dirname $0`
+exec erl +Bc +K true -smp auto -pa $PWD/ebin $PWD/deps/*/ebin -boot start_sasl -name rpc_server -config conf/rpc_server -s rpc_server
46 support/include.mk
@@ -0,0 +1,46 @@
+## -*- makefile -*-
+
+######################################################################
+## Erlang
+
+ERL := erl
+ERLC := $(ERL)c
+
+INCLUDE_DIRS := ../include $(wildcard ../deps/*/include)
+EBIN_DIRS := $(wildcard ../deps/*/ebin)
+ERLC_FLAGS := -W $(INCLUDE_DIRS:../%=-I ../%) $(EBIN_DIRS:%=-pa %)
+
+ifndef no_debug_info
+ ERLC_FLAGS += +debug_info
+endif
+
+ifdef debug
+ ERLC_FLAGS += -Ddebug
+endif
+
+EBIN_DIR := ../ebin
+DOC_DIR := ../doc
+EMULATOR := beam
+
+ERL_SOURCES := $(wildcard *.erl)
+ERL_HEADERS := $(wildcard *.hrl) $(wildcard ../include/*.hrl)
+ERL_OBJECTS := $(ERL_SOURCES:%.erl=$(EBIN_DIR)/%.$(EMULATOR))
+ERL_DOCUMENTS := $(ERL_SOURCES:%.erl=$(DOC_DIR)/%.html)
+ERL_OBJECTS_LOCAL := $(ERL_SOURCES:%.erl=./%.$(EMULATOR))
+APP_FILES := $(wildcard *.app)
+EBIN_FILES = $(ERL_OBJECTS) $(ERL_DOCUMENTS) $(APP_FILES:%.app=../ebin/%.app)
+EBIN_FILES_NO_DOCS = $(ERL_OBJECTS) $(APP_FILES:%.app=../ebin/%.app)
+MODULES = $(ERL_SOURCES:%.erl=%)
+
+../ebin/%.app: %.app
+ cp $< $@
+
+$(EBIN_DIR)/%.$(EMULATOR): %.erl
+ $(ERLC) $(ERLC_FLAGS) -o $(EBIN_DIR) $<
+
+./%.$(EMULATOR): %.erl
+ $(ERLC) $(ERLC_FLAGS) -o . $<
+
+$(DOC_DIR)/%.html: %.erl
+ $(ERL) -noshell -run edoc file $< -run init stop
+ mv *.html $(DOC_DIR)
Please sign in to comment.
Something went wrong with that request. Please try again.