Skip to content

Commit ed1aed8

Browse files
authored
Merge pull request #33 from rustkas/fix_doc
General improvements
2 parents 072e1c5 + f221a45 commit ed1aed8

15 files changed

+1089
-192
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.eunit
2-
doc/
32
deps
43
*.o
54
*.beam
@@ -14,3 +13,6 @@ _build
1413
_checkout
1514
compile_commands.json
1615
rebar3
16+
doc/*
17+
!doc/style.css
18+
!doc/overview.edoc

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ xref: $(REBAR)
2222
clean:
2323
$(REBAR) clean
2424

25+
##
26+
## Doc targets
27+
##
28+
docs: $(REBAR)
29+
$(REBAR) edoc
30+
31+
edoc_private: $(REBAR)
32+
$(REBAR) as edoc_private edoc
33+
2534
$(REBAR):
2635
$(ERL) -noshell -s inets -s ssl \
2736
-eval '{ok, saved_to_file} = httpc:request(get, {"$(REBAR_URL)", []}, [], [{stream, "$(REBAR)"}])' \

doc/overview.edoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@author Marc Worrell <marc@worrell.nl>
2+
@title cowmachine
3+
@doc Webmachine for <a href="http://zotonic.com/">Zotonic</a> and <a href="https://ninenines.eu/">Cowboy</a>.
4+
@copyright Apache-2.0
5+
@reference This is an adaptation of <a href="https://github.com/webmachine/webmachine">webmachine</a> for the Cowboy web server.

doc/style.css

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* standard EDoc style sheet */
2+
body {
3+
font-family: Verdana, Arial, Helvetica, sans-serif;
4+
margin-left: .25in;
5+
margin-right: .2in;
6+
margin-top: 0.2in;
7+
margin-bottom: 0.2in;
8+
color: #000000;
9+
background-color: #ffffff;
10+
}
11+
h1,h2 {
12+
margin-left: -0.2in;
13+
}
14+
div.navbar {
15+
background-color: #add8e6;
16+
padding: 0.2em;
17+
}
18+
h2.indextitle {
19+
padding: 0.4em;
20+
background-color: #add8e6;
21+
}
22+
h3.function,h3.typedecl {
23+
background-color: #add8e6;
24+
padding-left: 1em;
25+
}
26+
div.spec {
27+
margin-left: 2em;
28+
29+
background-color: #eeeeee;
30+
}
31+
a.module {
32+
text-decoration:none
33+
}
34+
a.module:hover {
35+
background-color: #eeeeee;
36+
}
37+
ul.definitions {
38+
list-style-type: none;
39+
}
40+
ul.index {
41+
list-style-type: none;
42+
background-color: #eeeeee;
43+
}
44+
45+
/*
46+
* Minor style tweaks
47+
*/
48+
ul {
49+
list-style-type: square;
50+
}
51+
table {
52+
border-collapse: collapse;
53+
}
54+
td {
55+
padding: 3px;
56+
vertical-align: middle;
57+
}
58+
59+
/*
60+
Tune styles
61+
*/
62+
63+
table[summary="navigation bar"] {
64+
background-image: url('http://zotonic.com/lib/images/logo.png');
65+
background-repeat: no-repeat;
66+
background-position: center;
67+
}
68+
69+
code, p>tt, a>tt {
70+
font-size: 1.2em;
71+
}
72+
73+
p {
74+
line-height: 1.5;
75+
}

rebar.config

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,28 @@
1818
]},
1919

2020
{xref_ignores, [
21-
]},
22-
23-
{dialyzer, [
21+
]}
22+
]},
23+
{edoc_private, [
24+
{edoc_opts, [
25+
{private, true}
26+
]}
27+
]},
28+
{check, [
29+
{dialyzer, [
2430
{warnings, [
2531
no_return
2632
]}
27-
]}
28-
]}
33+
]},
34+
35+
{erl_opts, [
36+
debug_info
37+
]}
38+
]
39+
}
40+
]}.
41+
42+
43+
{edoc_opts, [
44+
{preprocess, true}, {stylesheet, "style.css"}
2945
]}.

src/cowmachine.erl

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
%% @copyright 2016-2022 Marc Worrell
33
%%
44
%% @doc Cowmachine: webmachine middleware for Cowboy/Zotonic
5+
%% @end
56

67
%% Copyright 2016-2022 Marc Worrell
78
%%
@@ -34,11 +35,15 @@
3435
-include("cowmachine_state.hrl").
3536
-include("cowmachine_log.hrl").
3637

38+
%% @private
39+
-export_type([cmstate/0]).
40+
3741
%% @doc Cowboy middleware, route the new request. Continue with the cowmachine,
3842
%% requests a redirect or return a 400 on an unknown host.
39-
-spec execute(Req, Env) -> {ok, Req, Env} | {stop, Req}
43+
-spec execute(Req, Env) -> Result
4044
when Req :: cowboy_req:req(),
41-
Env :: cowboy_middleware:env().
45+
Env :: cowboy_middleware:env(),
46+
Result :: {ok, Req, Env} | {stop, Req}.
4247
execute(Req, #{ cowmachine_controller := _Controller } = Env) ->
4348
ContextEnv = maps:get(cowmachine_context, Env, undefined),
4449
Context = cowmachine_req:init_context(Req, Env, ContextEnv),
@@ -47,10 +52,13 @@ execute(Req, #{ cowmachine_controller := _Controller } = Env) ->
4752

4853
%% @doc Handle a request, executes the cowmachine http states. Can be used by middleware
4954
%% functions to add some additional initialization of controllers or context.
50-
-spec request(Context, Options::map()) -> {ok, Req, Env} | {stop, Req}
55+
56+
-spec request(Context, Options) -> Result
5157
when Context :: cowmachine_req:context(),
52-
Req :: cowboy_req:req(),
53-
Env :: cowboy_middleware:env().
58+
Options :: map(),
59+
Req :: cowboy_req:req(),
60+
Env :: cowboy_middleware:env(),
61+
Result :: {ok, Req, Env} | {stop, Req}.
5462
request(Context, Options) ->
5563
Req = cowmachine_req:req(Context),
5664
Env = cowmachine_req:env(Context),
@@ -62,14 +70,23 @@ request(Context, Options) ->
6270
Other
6371
end.
6472

73+
-spec request_1(Controller, Req, Env, Options, Context) -> Result when
74+
Controller :: atom(),
75+
Req :: cowboy_req:req(),
76+
Env :: cowboy_middleware:env(),
77+
Options :: map(),
78+
Context :: cowmachine_req:context(),
79+
Result :: {upgrade, UpgradeFun, State, Context} | cowboy_middleware:env() | any(),
80+
UpgradeFun :: atom(),
81+
State :: cmstate().
6582
request_1(Controller, Req, Env, Options, Context) ->
6683
State = #cmstate{
6784
controller = Controller,
6885
cache = #{},
6986
options = Options
7087
},
7188
Site = maps:get(site, Env, undefined),
72-
try
89+
ReqResult = try
7390
EnvInit = cowmachine_req:init_env(Req, Env),
7491
Context1 = cowmachine_req:set_env(EnvInit, Context),
7592
case cowmachine_decision_core:handle_request(State, Context1) of
@@ -116,9 +133,21 @@ request_1(Controller, Req, Env, Options, Context) ->
116133
class => Class, reason => Reason,
117134
stack => Stacktrace}, Req),
118135
handle_stop_request(500, Site, {throw, {Reason, Stacktrace}}, Req, Env, State, Context)
119-
end.
136+
end,
137+
ReqResult.
138+
120139

121140
% @todo add the error controller as an application env, if not defined then just terminate with the corresponding error code.
141+
142+
-spec handle_stop_request(ResponseCode, Site, Reason, Req, Env, State, Context) -> Result when
143+
ResponseCode :: integer(),
144+
Site :: atom() | undefined,
145+
Reason :: any(),
146+
Req :: cowboy_req:req(),
147+
Env :: cowboy_middleware:env(),
148+
State :: cmstate(),
149+
Context :: cowmachine_req:context(),
150+
Result :: {ok, Req, Env} | {stop, Req}.
122151
handle_stop_request(ResponseCode, _Site, Reason, Req, Env, State, Context) ->
123152
State1 = State#cmstate{
124153
controller = controller_http_error
@@ -154,13 +183,19 @@ handle_stop_request(ResponseCode, _Site, Reason, Req, Env, State, Context) ->
154183
%%
155184
%% Logging
156185
%%
157-
186+
-spec log(Report) -> Result when
187+
Report :: map(),
188+
Result :: any().
158189
log(#{ level := Level } = Report) ->
159190
log_report(Level, Report#{
160191
in => cowmachine,
161192
node => node()
162193
}).
163194

195+
-spec log(Report, Req) -> Result when
196+
Report :: map(),
197+
Req :: map(),
198+
Result :: any().
164199
log(#{ level := Level } = Report, Req) when is_map(Req) ->
165200
Report1 = lists:foldl(fun({Key, Fun}, Acc) ->
166201
case Fun(Req) of
@@ -175,6 +210,10 @@ log(#{ level := Level } = Report, Req) when is_map(Req) ->
175210
node => node()
176211
}).
177212

213+
-spec log_report(LogLevel, Report) -> Result when
214+
LogLevel :: debug | info | notice | warning | error,
215+
Report :: map(),
216+
Result :: any().
178217
log_report(debug, Report) when is_map(Report) ->
179218
?LOG_DEBUG(Report);
180219
log_report(info, Report) when is_map(Report) ->
@@ -186,16 +225,36 @@ log_report(warning, Report) when is_map(Report) ->
186225
log_report(error, Report) when is_map(Report) ->
187226
?LOG_ERROR(Report).
188227

228+
-spec src(IpInfo) -> Result when
229+
IpInfo :: #{ peer := {IP, Port} } | any(),
230+
Port :: integer(),
231+
IP :: tuple(),
232+
Result :: {ok, map()} | undefined.
189233
src(#{ peer := {IP, Port} }) -> {ok, ip_info(IP, Port)};
190234
src(_) -> undefined.
191235

236+
-spec dst(DstInfo) -> Result when
237+
DstInfo :: #{ sock := {IP, Port} } | #{ port := Port } | any(),
238+
IP :: tuple(),
239+
Port :: integer(),
240+
Result :: {ok, #{IPType => string(), port => Port}} | {ok, #{ port => Port }} | undefined,
241+
IPType :: ip4 | ip6.
192242
dst(#{ sock := {IP, Port} } ) -> {ok, ip_info(IP, Port)};
193243
dst(#{ port := Port }) -> {ok, #{ port => Port }};
194244
dst(_) -> undefined.
195245

246+
-spec path(PathInfo) -> Result when
247+
PathInfo :: #{ path := Path } | any(),
248+
Path :: any(),
249+
Result :: {ok, Path} | undefined.
196250
path(#{ path := Path }) -> {ok, Path};
197251
path(_) -> undefined.
198252

253+
-spec ip_info(IP, Port) -> Result when
254+
IP :: tuple(),
255+
Port :: integer(),
256+
IPType :: ip4 | ip6,
257+
Result :: #{IPType => string(), port => Port}.
199258
ip_info(IP, Port) ->
200259
IPType = case tuple_size(IP) of 4 -> ip4; 8 -> ip6 end,
201260
#{IPType => inet_parse:ntoa(IP), port => Port}.

0 commit comments

Comments
 (0)