Permalink
Browse files

Initial.

  • Loading branch information...
0 parents commit e9c28001fb1c0775d74c949d69199753f1323d82 @cmeiklejohn committed Jun 17, 2012
@@ -0,0 +1,19 @@
+ERL ?= erl
+APP := herloku
+
+.PHONY: deps
+
+all: deps
+ @./rebar compile
+
+deps:
+ @./rebar get-deps
+
+clean:
+ @./rebar clean
+
+distclean: clean
+ @./rebar delete-deps
+
+docs:
+ @erl -noshell -run edoc_run application '$(APP)' '"."' '[]'
@@ -0,0 +1,35 @@
+Project Skeleton for the herloku app.
+
+You should find in this directory:
+
+README : this file
+Makefile : simple make commands
+rebar : the Rebar build tool for Erlang applications
+rebar.config : configuration for Rebar
+start.sh : simple startup script for running herloku
+/ebin
+ /herloku.app : the Erlang app specification
+/src
+ /herloku_app.erl : base module for the Erlang application
+ /herloku_sup.erl : OTP supervisor for the application
+ /herloku_resource.erl : a simple example Webmachine resource
+/priv
+ /dispatch.conf : the Webmachine URL-dispatching table
+ /www : a convenient place to put your static web content
+
+You probably want to do one of a couple of things at this point:
+
+0. Build the skeleton application:
+ $ make
+ - or -
+ $ ./rebar compile
+
+1. Start up the skeleton application:
+ $ ./start.sh
+
+2. Change the basic application:
+ edit src/herloku_resource.erl
+
+3. Add some new resources:
+ edit src/YOUR_NEW_RESOURCE.erl
+ edit priv/dispatch.conf
Submodule mochiweb added at 2984b7
Submodule webmachine added at effab6
@@ -0,0 +1,8 @@
+{application,herloku,
+ [{description,"herloku"},
+ {vsn,"1"},
+ {modules,[herloku,herloku_app,herloku_resource,herloku_sup]},
+ {registered,[]},
+ {applications,[kernel,stdlib,inets,crypto,mochiweb,webmachine]},
+ {mod,{herloku_app,[]}},
+ {env,[]}]}.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
+%%-*- mode: erlang -*-
+{[], herloku_resource, []}.
@@ -0,0 +1,2 @@
+127.0.0.1 - - [17/Jun/2012:11:45:55 -0400] "GET / HTTP/1.1" 200 42 "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5"
+127.0.0.1 - - [17/Jun/2012:11:45:56 -0400] "GET /favicon.ico HTTP/1.1" 404 193 "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5"
Binary file not shown.
@@ -0,0 +1,3 @@
+%%-*- mode: erlang -*-
+
+{deps, [{webmachine, "1.9.*", {git, "git://github.com/basho/webmachine", "HEAD"}}]}.
@@ -0,0 +1,18 @@
+%%-*- mode: erlang -*-
+{application, herloku,
+ [
+ {description, "herloku"},
+ {vsn, "1"},
+ {modules, []},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib,
+ inets,
+ crypto,
+ mochiweb,
+ webmachine
+ ]},
+ {mod, { herloku_app, []}},
+ {env, []}
+ ]}.
@@ -0,0 +1,48 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc herloku startup code
+
+-module(herloku).
+-author('author <author@example.com>').
+-export([start/0, start_link/0, stop/0]).
+
+ensure_started(App) ->
+ case application:start(App) of
+ ok ->
+ ok;
+ {error, {already_started, App}} ->
+ ok
+ end.
+
+%% @spec start_link() -> {ok,Pid::pid()}
+%% @doc Starts the app for inclusion in a supervisor tree
+start_link() ->
+ ensure_started(inets),
+ ensure_started(crypto),
+ ensure_started(mochiweb),
+ application:set_env(webmachine, webmachine_logger_module,
+ webmachine_logger),
+ ensure_started(webmachine),
+ herloku_sup:start_link().
+
+%% @spec start() -> ok
+%% @doc Start the herloku server.
+start() ->
+ ensure_started(inets),
+ ensure_started(crypto),
+ ensure_started(mochiweb),
+ application:set_env(webmachine, webmachine_logger_module,
+ webmachine_logger),
+ ensure_started(webmachine),
+ application:start(herloku).
+
+%% @spec stop() -> ok
+%% @doc Stop the herloku server.
+stop() ->
+ Res = application:stop(herloku),
+ application:stop(webmachine),
+ application:stop(mochiweb),
+ application:stop(crypto),
+ application:stop(inets),
+ Res.
@@ -0,0 +1,21 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Callbacks for the herloku application.
+
+-module(herloku_app).
+-author('author <author@example.com>').
+
+-behaviour(application).
+-export([start/2,stop/1]).
+
+
+%% @spec start(_Type, _StartArgs) -> ServerRet
+%% @doc application start callback for herloku.
+start(_Type, _StartArgs) ->
+ herloku_sup:start_link().
+
+%% @spec stop(_State) -> ServerRet
+%% @doc application stop callback for herloku.
+stop(_State) ->
+ ok.
@@ -0,0 +1,13 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+%% @doc Example webmachine_resource.
+
+-module(herloku_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}.
@@ -0,0 +1,57 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Supervisor for the herloku application.
+
+-module(herloku_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, App} = application:get_application(?MODULE),
+ {ok, Dispatch} = file:consult(filename:join([code:priv_dir(App),
+ "dispatch.conf"])),
+ WebConfig = [
+ {ip, Ip},
+ {port, 8000},
+ {log_dir, "priv/log"},
+ {dispatch, Dispatch}],
+ Web = {webmachine_mochiweb,
+ {webmachine_mochiweb, start, [WebConfig]},
+ permanent, 5000, worker, [mochiweb_socket_server]},
+ Processes = [Web],
+ {ok, { {one_for_one, 10, 10}, Processes} }.
@@ -0,0 +1,3 @@
+#!/bin/sh
+cd `dirname $0`
+exec erl -pa $PWD/ebin $PWD/deps/*/ebin -boot start_sasl -s reloader -s herloku

0 comments on commit e9c2800

Please sign in to comment.