0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Use syntax highlighted code block in README.md

GitHub just implemented that and we should use it.
https://github.com/blog/832-rolling-out-the-redcarpet
This commit is contained in:
Anthony Ramine 2011-04-30 12:28:07 +02:00
parent 20293be912
commit 2c21f0cd09

View file

@ -54,30 +54,34 @@ listener a unique name.
Code speaks more than words: Code speaks more than words:
application:start(cowboy), ``` erlang
Dispatch = [ application:start(cowboy),
%% {Host, list({Path, Handler, Opts})} Dispatch = [
{'_', [{'_', my_handler, []}]} %% {Host, list({Path, Handler, Opts})}
], {'_', [{'_', my_handler, []}]}
%% Name, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts ],
cowboy:start_listener(http, 100, %% Name, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
cowboy_tcp_transport, [{port, 8080}], cowboy:start_listener(http, 100,
cowboy_http_protocol, [{dispatch, Dispatch}] cowboy_tcp_transport, [{port, 8080}],
). cowboy_http_protocol, [{dispatch, Dispatch}]
).
```
You must also write the `my_handler` module to process requests. You can You must also write the `my_handler` module to process requests. You can
use one of the predefined handlers or write your own. An hello world HTTP use one of the predefined handlers or write your own. An hello world HTTP
handler could be written like this: handler could be written like this:
-module(my_handler). ``` erlang
-export([init/3, handle/2, terminate/2]). -module(my_handler).
-export([init/3, handle/2, terminate/2]).
init({tcp, http}, Req, Opts) -> init({tcp, http}, Req, Opts) ->
{ok, Req, undefined}. {ok, Req, undefined}.
handle(Req, State) -> handle(Req, State) ->
{ok, Req2} = cowboy_http_req:reply(200, [], "Hello World!", Req), {ok, Req2} = cowboy_http_req:reply(200, [], "Hello World!", Req),
{ok, Req2, State}. {ok, Req2, State}.
terminate(Req, State) -> terminate(Req, State) ->
ok. ok.
```