Skip to content

Commit

Permalink
Merge pull request #3 from voyagegroup/req_handler_header
Browse files Browse the repository at this point in the history
Req handler header
  • Loading branch information
ajiyoshi-vg authored Apr 3, 2019
2 parents ff1113d + a5540f6 commit e1ba055
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 44 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.rebar3
_*
.eunit
*.o
*.beam
*.plt
*.swp
*.swo
.erlang.cookie
ebin
log
erl_crash.dump
.rebar
logs
_build
.idea
*.iml
rebar3.crashdump
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 VOYAGE GROUP, Inc.
Copyright (c) 2018 VOYAGE GROUP, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
http_gateway
=====

# http_gateway

## license
License
-----

MIT LICENSE
(see LICENSE.txt)
3 changes: 2 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
{deps, [
{cowboy, ".*", {git, "https://github.com/extend/cowboy.git", {branch, "master"}}}]}.
{cowboy, {git, "https://github.com/ninenines/cowboy.git", {tag, "2.3.0"}}}
]}.
24 changes: 14 additions & 10 deletions src/http_gateway.app.src
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{application, http_gateway,
[
{description, ""},
{vsn, "1"},
[{description, ""},
{vsn, "0.1.0"},
{registered, []},
{applications, [
kernel,
stdlib,
ranch,
cowboy
]},
{mod, { http_gateway_app, []}},
{env, []}
{applications,
[
kernel,
stdlib,
cowboy
]},
{env, []},
{modules, []},

{maintainers, []},
{licenses, ["MIT"]},
{links, []}
]}.
32 changes: 18 additions & 14 deletions src/http_gateway_app.erl
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
%%%-------------------------------------------------------------------
%% @doc http_gateway public API
%% @end
%%%-------------------------------------------------------------------

-module(http_gateway_app).

-behaviour(application).

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

%% Application callbacks
-export([start/0]).
-export([start/2, stop/1]).

-define(APP_NAME, http_gateway).
-define(DEFAULT_PORT, 8887).

%% ===================================================================
%% API callbacks
%% ===================================================================
%%====================================================================
%% API
%%====================================================================

start() ->
application:start(http_gateway).

%% ===================================================================
%% Application callbacks
%% ===================================================================

start(_StartType, _StartArgs) ->
Dispatch = cowboy_router:compile([
{'_', get_routes()}
]),
cowboy:start_http(http, 100, [{port, get_port()}], [
{env, [{dispatch, Dispatch}]}
]),
cowboy:start_clear(http, [{port, get_port()}], #{
env => #{dispatch => Dispatch}
}),
http_gateway_sup:start_link().

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


%%====================================================================
%% Internal functions
%%====================================================================
get_routes() ->
case application:get_env(?APP_NAME, routes) of
{ok, Routes} -> Routes;
_ -> []
_ -> [{"/", route_handler, []}]
end.

get_port() ->
Expand Down
11 changes: 10 additions & 1 deletion src/http_gateway_sup.erl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
%%%-------------------------------------------------------------------
%% @doc http_gateway top level supervisor.
%% @end
%%%-------------------------------------------------------------------

-module(http_gateway_sup).

Expand All @@ -9,6 +13,8 @@
%% Supervisor callbacks
-export([init/1]).

-define(SERVER, ?MODULE).

%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).

Expand All @@ -17,7 +23,7 @@
%% ===================================================================

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

%% ===================================================================
%% Supervisor callbacks
Expand All @@ -26,3 +32,6 @@ start_link() ->
init([]) ->
{ok, { {one_for_one, 5, 10}, []} }.

%%====================================================================
%% Internal functions
%%====================================================================
24 changes: 18 additions & 6 deletions src/req_handler.erl
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
-module(req_handler).

-export([getdata/1, postdata/1]).
-export([getdata/1, postdata/1, header/2]).

-define(POST_SIZE, 160000).
-define(APP_NAME, http_gateway).
-define(POST_SIZE, 320000).

getdata(Req) ->
case cowboy_req:method(Req) of
{<<"GET">>, _} ->
{Qs, _} = cowboy_req:qs_vals(Req),
<<"GET">> ->
Qs = cowboy_req:parse_qs(Req),
Qs;
_ -> no_getdata
end.

postdata(Req) ->
case cowboy_req:method(Req) of
{<<"POST">>, _} ->
{ok, Qs, _} = cowboy_req:body_qs(Req, [{length, ?POST_SIZE}]),
<<"POST">> ->
{ok, Qs, _} = cowboy_req:read_urlencoded_body(Req, #{length => get_postsize()}),
Qs;
_ -> no_postdata
end.

get_postsize() ->
case application:get_env(?APP_NAME, postsize) of
{ok, Postsize} -> Postsize;
_ -> ?POST_SIZE
end.

-spec header(binary(), cowboy_req:req()) -> binary() | undefined.
header(Name, Req) ->
cowboy_req:header(Name, Req).

19 changes: 11 additions & 8 deletions src/route_handler.erl
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
-module(route_handler).

-export([init/3]).
-export([init/2]).
-export([handle/3]).
-export([terminate/3]).

init(_Transport, Req, []) ->
{ok, Req, undefined}.

init(Req0, State) ->
Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/plain">>
}, <<"Hello">>, Req0),
{ok, Req, State}.

handle(Req, State, F) ->
Res = F(Req),
{ok, Req2} = make_reply(Res, Req),
Req2 = make_reply(Res, Req),
{ok, Req2, State}.

make_reply(Body, Req) ->
Headers = [{<<"content-type">>, <<"text/plain">>}],
cowboy_req:reply(200, Headers, Body, Req).
Headers = #{<<"content-type">> => <<"text/plain">>},
cowboy_req:reply(200, Headers, Body, Req).

terminate(_Reason, _Req, _State) ->
ok.
ok.

0 comments on commit e1ba055

Please sign in to comment.