Permalink
Browse files

First commit with builds, HTTP server.

Now let's build this goddamn thing.
  • Loading branch information...
pablo-meier committed Mar 10, 2015
0 parents commit 03898640772871b4276144fc894df15a702723eb
@@ -0,0 +1,18 @@
.rebar3
_*
.eunit
*.o
*.beam
*.plt
*.swp
*.swo
.erlang.cookie
ebin
log
erl_crash.dump
.rebar
_rel
_deps
_plugins
_tdeps
logs
@@ -0,0 +1,9 @@
ghostlight
=====

An OTP application

Build
-----

$ rebar3 compile
@@ -0,0 +1,10 @@
{application, ghostlight,
[{description, "Bringing the Internet to performing artists."},
{vsn, "0.1.0"},
{registered, []},
{mod, {'ghostlight_app', []}},
{applications,
[lager, cowboy, kernel, stdlib]},
{env,[]},
{modules, []}
]}.
@@ -0,0 +1,43 @@
%%%-------------------------------------------------------------------
%% @doc ghostlight public API
%% @end
%%%-------------------------------------------------------------------

-module(ghostlight_app).

-behaviour(application).
%-compile([{compile_transform, lager_transform}]).
%% Application callbacks
-export([start/2, stop/1]).

%%====================================================================
%% API
%%====================================================================

start(_StartType, _StartArgs) ->
lager:start(),
lager:info("HELLO GHOSTLIGHT"),
initiate_listening_to_endpoints(),
ghostlight_sup:start_link().

%%--------------------------------------------------------------------
stop(_State) ->
ok.

%%====================================================================
%% Internal functions
%%====================================================================


initiate_listening_to_endpoints() ->
Port = 8080,
Dispatch = cowboy_router:compile([
{'_', [
{"/shows", ghostlight_show, []}
]}
]),
{ok, _} = cowboy:start_http(http, 100, [{port, Port}], [
{env, [{dispatch, Dispatch}]}
]),
lager:info("Started the Cowboy server on Port ~p~n", [Port]).

@@ -0,0 +1,135 @@
%%%-------------------------------------------------------------------
%%% @author pablo
%%% @copyright (C) 2015, pablo
%%% @doc
%%% gen_server for the connection to the Neo4j database.
%%% @end
%%% Created : 2015-03-08 20:46:29.917402
%%%-------------------------------------------------------------------
-module(ghostlight_db).
-behaviour(gen_server).

%% API
-export([start_link/0]).

%% gen_server callbacks
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).

-define(SERVER, ?MODULE).

-record(state, {}).

%%%===================================================================
%%% API
%%%===================================================================

%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
start_link() ->
io:format("Hello World!"),
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
init([]) ->
{ok, #state{}}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @spec handle_call(Request, From, State) ->
%% {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
{noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
{noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Convert process state when code is changed
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%%%===================================================================
%%% Internal functions
%%%===================================================================




@@ -0,0 +1,37 @@
-module(ghostlight_show).
-export([init/2]).
-export([content_types_provided/2]).
-export([show_to_html/2]).
-export([show_to_json/2]).
-export([show_to_text/2]).


init(Req, Opts) ->
{cowboy_rest, Req, Opts}.

content_types_provided(Req, State) ->
{[
{<<"text/html">>, show_to_html},
{<<"application/json">>, show_to_json},
{<<"text/plain">>, show_to_text}
], Req, State}.


show_to_html(Req, State) ->
Body = <<"<html>
<head>
<meta charset=\"utf-8\">
<title>REST Show!</title>
</head>
<body>
<p>It's a show!</p>
</body>
</html>">>,
{Body, Req, State}.

show_to_json(Req, State) ->
Body = <<"{\"data\": \"It's a show!\"}">>,
{Body, Req, State}.

show_to_text(Req, State) ->
{<<"It's a show!">>, Req, State}.
@@ -0,0 +1,41 @@
%%%-------------------------------------------------------------------
%% @doc ghostlight top level supervisor.
%% @end
%%%-------------------------------------------------------------------

-module(ghostlight_sup).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

-define(SERVER, ?MODULE).

%%====================================================================
%% API functions
%%====================================================================

start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%%====================================================================
%% Supervisor callbacks
%%====================================================================

%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
init([]) ->
{ok, {{one_for_all, 5, 60},
[{neo4j_server,
{ghostlight_db, start_link, []},
permanent,
5000,
worker,
[ghostlight_db]}]}}.

%%====================================================================
%% Internal functions
%%====================================================================
@@ -0,0 +1,3 @@
[
{'ghostlight', []}
].
@@ -0,0 +1,6 @@
-name ghostlight

-setcookie ghostlight_cookie

+K true
+A30
@@ -0,0 +1,26 @@
{erl_opts, [debug_info, {parse_transform, lager_transform}]}.

{deps, [{lager, {git, "git://github.com/basho/lager.git", {tag, "2.1.1"}}},
{cowboy, {git, "git://github.com/ninenines/cowboy.git", {tag, "1.0.1"}}},
{erlydtl, {git, "git://github.com/erlydtl/erlydtl.git", {tag, "0.10.0"}}},
{neo4j, {git, "git://github.com/dmitriid/neo4j-erlang.git", {tag, "0.2"}}},
{jiffy, {git, "git://github.com/davisp/jiffy.git", {tag, "0.13.3"}}}
]}.

{relx, [{release, {'ghostlight', "0.1.0"},
[ghostlight,
sasl]},

{sys_config, "./config/sys.config"},
{vm_args, "./config/vm.args"},

{dev_mode, true},
{include_erts, false},

{extended_start_script, true}]
}.

{profiles, [{prod, [{relx, [{dev_mode, false},
{include_erts, true}]}]
}]
}.

0 comments on commit 0389864

Please sign in to comment.