mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 04:10:24 +00:00
Add templates as Erlang.mk plugin
This commit is contained in:
parent
f7094ad78f
commit
5838a0c81a
2 changed files with 83 additions and 3 deletions
|
@ -71,10 +71,15 @@ PROJECT = hello_erlang
|
|||
DEPS = cowboy
|
||||
dep_cowboy_commit = master
|
||||
|
||||
DEP_PLUGINS = cowboy
|
||||
|
||||
include erlang.mk
|
||||
----
|
||||
|
||||
If you run `make run` now, Cowboy will be included in the release
|
||||
We also tell the build system to load the plugins Cowboy provides.
|
||||
These include predefined templates that we will use soon.
|
||||
|
||||
If you do `make run` now, Cowboy will be included in the release
|
||||
and started automatically. This is not enough however, as Cowboy
|
||||
doesn't do anything by default. We still need to tell Cowboy to
|
||||
listen for connections.
|
||||
|
@ -119,7 +124,7 @@ HTTP handler.
|
|||
Generate a handler from a template:
|
||||
|
||||
[source,bash]
|
||||
$ make new t=cowboy_http n=hello_handler
|
||||
$ make new t=cowboy.http n=hello_handler
|
||||
|
||||
Then, open the 'src/hello_handler.erl' file and modify
|
||||
the `init/2` function like this to send a reply.
|
||||
|
@ -134,7 +139,7 @@ init(Req0, State) ->
|
|||
{ok, Req, State}.
|
||||
----
|
||||
|
||||
What the above code does is send a `200 OK` reply, with the
|
||||
What the above code does is send a 200 OK reply, with the
|
||||
Content-type header set to `text/plain` and the response
|
||||
body set to `Hello Erlang!`.
|
||||
|
||||
|
|
75
plugins.mk
Normal file
75
plugins.mk
Normal file
|
@ -0,0 +1,75 @@
|
|||
# See LICENSE for licensing information.
|
||||
|
||||
# Plain HTTP handlers.
|
||||
define tpl_cowboy.http
|
||||
-module($(n)).
|
||||
-behavior(cowboy_handler).
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, State) ->
|
||||
{ok, Req, State}.
|
||||
endef
|
||||
|
||||
# Loop handlers.
|
||||
define tpl_cowboy.loop
|
||||
-module($(n)).
|
||||
-behavior(cowboy_loop).
|
||||
|
||||
-export([init/2]).
|
||||
-export([info/3]).
|
||||
|
||||
init(Req, State) ->
|
||||
{cowboy_loop, Req, State, 5000, hibernate}.
|
||||
|
||||
info(_Info, Req, State) ->
|
||||
{ok, Req, State, hibernate}.
|
||||
endef
|
||||
|
||||
# REST handlers.
|
||||
define tpl_cowboy.rest
|
||||
-module($(n)).
|
||||
-behavior(cowboy_rest).
|
||||
|
||||
-export([init/2]).
|
||||
-export([content_types_provided/2]).
|
||||
-export([to_html/2]).
|
||||
|
||||
init(Req, State) ->
|
||||
{cowboy_rest, Req, State}.
|
||||
|
||||
content_types_provided(Req, State) ->
|
||||
{[
|
||||
{{<<"text">>, <<"html">>, '*'}, to_html}
|
||||
], Req, State}.
|
||||
|
||||
to_html(Req, State) ->
|
||||
{<<"<html><body>This is REST!</body></html>">>, Req, State}.
|
||||
endef
|
||||
|
||||
# Websocket handlers.
|
||||
define tpl_cowboy.ws
|
||||
-module($(n)).
|
||||
-behavior(cowboy_websocket).
|
||||
|
||||
-export([init/2]).
|
||||
-export([websocket_init/1]).
|
||||
-export([websocket_handle/2]).
|
||||
-export([websocket_info/2]).
|
||||
|
||||
init(Req, State) ->
|
||||
{cowboy_websocket, Req, State}.
|
||||
|
||||
websocket_init(State) ->
|
||||
{ok, State}.
|
||||
|
||||
websocket_handle({text, Data}, State) ->
|
||||
{reply, {text, Data}, State};
|
||||
websocket_handle({binary, Data}, State) ->
|
||||
{reply, {binary, Data}, State};
|
||||
websocket_handle(_Frame, State) ->
|
||||
{ok, State}.
|
||||
|
||||
websocket_info(_Info, State) ->
|
||||
{ok, State}.
|
||||
endef
|
Loading…
Add table
Add a link
Reference in a new issue