Skip to content

Commit 5838a0c

Browse files
committed
Add templates as Erlang.mk plugin
1 parent f7094ad commit 5838a0c

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

doc/src/guide/getting_started.asciidoc

+8-3
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,15 @@ PROJECT = hello_erlang
7171
DEPS = cowboy
7272
dep_cowboy_commit = master
7373
74+
DEP_PLUGINS = cowboy
75+
7476
include erlang.mk
7577
----
7678

77-
If you run `make run` now, Cowboy will be included in the release
79+
We also tell the build system to load the plugins Cowboy provides.
80+
These include predefined templates that we will use soon.
81+
82+
If you do `make run` now, Cowboy will be included in the release
7883
and started automatically. This is not enough however, as Cowboy
7984
doesn't do anything by default. We still need to tell Cowboy to
8085
listen for connections.
@@ -119,7 +124,7 @@ HTTP handler.
119124
Generate a handler from a template:
120125

121126
[source,bash]
122-
$ make new t=cowboy_http n=hello_handler
127+
$ make new t=cowboy.http n=hello_handler
123128

124129
Then, open the 'src/hello_handler.erl' file and modify
125130
the `init/2` function like this to send a reply.
@@ -134,7 +139,7 @@ init(Req0, State) ->
134139
{ok, Req, State}.
135140
----
136141

137-
What the above code does is send a `200 OK` reply, with the
142+
What the above code does is send a 200 OK reply, with the
138143
Content-type header set to `text/plain` and the response
139144
body set to `Hello Erlang!`.
140145

plugins.mk

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# See LICENSE for licensing information.
2+
3+
# Plain HTTP handlers.
4+
define tpl_cowboy.http
5+
-module($(n)).
6+
-behavior(cowboy_handler).
7+
8+
-export([init/2]).
9+
10+
init(Req, State) ->
11+
{ok, Req, State}.
12+
endef
13+
14+
# Loop handlers.
15+
define tpl_cowboy.loop
16+
-module($(n)).
17+
-behavior(cowboy_loop).
18+
19+
-export([init/2]).
20+
-export([info/3]).
21+
22+
init(Req, State) ->
23+
{cowboy_loop, Req, State, 5000, hibernate}.
24+
25+
info(_Info, Req, State) ->
26+
{ok, Req, State, hibernate}.
27+
endef
28+
29+
# REST handlers.
30+
define tpl_cowboy.rest
31+
-module($(n)).
32+
-behavior(cowboy_rest).
33+
34+
-export([init/2]).
35+
-export([content_types_provided/2]).
36+
-export([to_html/2]).
37+
38+
init(Req, State) ->
39+
{cowboy_rest, Req, State}.
40+
41+
content_types_provided(Req, State) ->
42+
{[
43+
{{<<"text">>, <<"html">>, '*'}, to_html}
44+
], Req, State}.
45+
46+
to_html(Req, State) ->
47+
{<<"<html><body>This is REST!</body></html>">>, Req, State}.
48+
endef
49+
50+
# Websocket handlers.
51+
define tpl_cowboy.ws
52+
-module($(n)).
53+
-behavior(cowboy_websocket).
54+
55+
-export([init/2]).
56+
-export([websocket_init/1]).
57+
-export([websocket_handle/2]).
58+
-export([websocket_info/2]).
59+
60+
init(Req, State) ->
61+
{cowboy_websocket, Req, State}.
62+
63+
websocket_init(State) ->
64+
{ok, State}.
65+
66+
websocket_handle({text, Data}, State) ->
67+
{reply, {text, Data}, State};
68+
websocket_handle({binary, Data}, State) ->
69+
{reply, {binary, Data}, State};
70+
websocket_handle(_Frame, State) ->
71+
{ok, State}.
72+
73+
websocket_info(_Info, State) ->
74+
{ok, State}.
75+
endef

0 commit comments

Comments
 (0)