Browse Source

Added cowboy.

Heinz Nikolaus Gies 12 years ago
parent
commit
12d71edd83

+ 2 - 0
cowboy/.gitignore

@@ -0,0 +1,2 @@
+deps
+ebin

+ 4 - 0
cowboy/Makefile

@@ -0,0 +1,4 @@
+all:
+	./rebar get-deps
+	./rebar compile
+	erl -pa ebin deps/*/ebin -s hello_world -noshell -detached

+ 12 - 0
cowboy/benchmark_config

@@ -0,0 +1,12 @@
+{
+  "framework": "cowboy",
+  "tests": [{
+    "default": {
+      "setup_file": "setup_erlang",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/db?queries=",
+      "port": 8080,
+      "sort": 38
+  }}]
+}

BIN
cowboy/rebar


+ 7 - 0
cowboy/rebar.config

@@ -0,0 +1,7 @@
+{deps,
+ [
+  {jiffy, ".*", {git, "https://github.com/davisp/jiffy.git", {tag, "0.8.3"}}},
+  {jsx, ".*", {git, "http://github.com/talentdeficit/jsx.git", {tag, "v1.3.3"}}},
+  {mimetypes, ".*", {git, "http://github.com/spawngrid/mimetypes.git", {branch, master}}},
+  {cowboy, ".*", {git, "https://github.com/extend/cowboy.git", {tag, "0.8.2"}}}
+ ]}.

+ 21 - 0
cowboy/setup_erlang.py

@@ -0,0 +1,21 @@
+
+import subprocess
+import sys
+import setup_util
+
+def start(args):
+#  setup_util.replace_text("rails/config/database-ruby.yml", "host: .*", "host: " + args.database_host)
+  
+  try:
+    subprocess.check_call("./rebar get-deps", shell=True, cwd="cowboy")
+    subprocess.check_call("./rebar compile", shell=True, cwd="cowboy")
+    subprocess.check_call("erl -pa ebin deps/*/ebin -s hello_world -noshell -detached", shell=True, cwd="cowboy")
+    return 0
+  except subprocess.CalledProcessError:
+    return 1
+def stop():
+  try:
+    subprocess.check_call("killall beam", shell=True, cwd="/usr/bin")
+    return 0
+  except subprocess.CalledProcessError:
+    return 1

+ 15 - 0
cowboy/src/hello_world.app.src

@@ -0,0 +1,15 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+{application, hello_world, [
+	{description, "Cowboy Hello World example."},
+	{vsn, "1"},
+	{modules, []},
+	{registered, []},
+	{applications, [
+		kernel,
+		stdlib,
+		cowboy
+	]},
+	{mod, {hello_world_app, []}},
+	{env, []}
+]}.

+ 14 - 0
cowboy/src/hello_world.erl

@@ -0,0 +1,14 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(hello_world).
+
+%% API.
+-export([start/0]).
+
+%% API.
+
+start() ->
+	ok = application:start(crypto),
+	ok = application:start(ranch),
+	ok = application:start(cowboy),
+	ok = application:start(hello_world).

+ 26 - 0
cowboy/src/hello_world_app.erl

@@ -0,0 +1,26 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(hello_world_app).
+-behaviour(application).
+
+%% API.
+-export([start/2]).
+-export([stop/1]).
+
+%% API.
+
+start(_Type, _Args) ->
+        application:start(jiffy),
+	Dispatch = cowboy_router:compile([
+		{'_', [
+			{"/json", toppage_handler, []}
+		]}
+	]),
+	{ok, _} = cowboy:start_http(http, 200, [{port, 8080}], [
+		{env, [{dispatch, Dispatch}]}
+	]),
+	hello_world_sup:start_link().
+
+stop(_State) ->
+	ok.

+ 23 - 0
cowboy/src/hello_world_sup.erl

@@ -0,0 +1,23 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(hello_world_sup).
+-behaviour(supervisor).
+
+%% API.
+-export([start_link/0]).
+
+%% supervisor.
+-export([init/1]).
+
+%% API.
+
+-spec start_link() -> {ok, pid()}.
+start_link() ->
+	supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% supervisor.
+
+init([]) ->
+	Procs = [],
+	{ok, {{one_for_one, 10, 10}, Procs}}.

+ 18 - 0
cowboy/src/toppage_handler.erl

@@ -0,0 +1,18 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @doc Hello world handler.
+-module(toppage_handler).
+
+-export([init/3]).
+-export([handle/2]).
+-export([terminate/3]).
+
+init(_Transport, Req, []) ->
+	{ok, Req, undefined}.
+
+handle(Req, State) ->
+	{ok, Req2} = cowboy_req:reply(200, [{<<"Content-Type">>, <<"application/json">>}], jiffy:encode({[{<<"message">>, <<"Hello, World!">>}]}), Req),
+	{ok, Req2, State}.
+
+terminate(_Reason, _Req, _State) ->
+	ok.