2014-07-06 13:10:35 +02:00
|
|
|
::: Getting started
|
2013-06-27 00:02:12 +02:00
|
|
|
|
2014-06-25 11:23:58 +02:00
|
|
|
Erlang is more than a language, it is also an operating system
|
|
|
|
for your applications. Erlang developers rarely write standalone
|
|
|
|
modules, they write libraries or applications, and then bundle
|
|
|
|
those into what is called a release. A release contains the
|
|
|
|
Erlang VM plus all applications required to run the node, so
|
|
|
|
it can be pushed to production directly.
|
|
|
|
|
|
|
|
This chapter walks you through all the steps of setting up
|
|
|
|
Cowboy, writing your first application and generating your first
|
|
|
|
release. At the end of this chapter you should know everything
|
|
|
|
you need to push your first Cowboy application to production.
|
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
:: Bootstrap
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-02 12:38:15 +02:00
|
|
|
We are going to use the ^"erlang.mk^https://github.com/ninenines/erlang.mk
|
2014-08-01 14:27:04 +02:00
|
|
|
build system. It also offers bootstrap features allowing us to
|
|
|
|
quickly get started without having to deal with minute details.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
First, let's create the directory for our application.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
|
|
|
``` bash
|
|
|
|
$ mkdir hello_erlang
|
|
|
|
$ cd hello_erlang
|
|
|
|
```
|
2013-06-27 00:02:12 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
Then we need to download `erlang.mk`. Either use the following
|
|
|
|
command or download it manually.
|
2013-06-27 00:02:12 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
``` bash
|
2014-08-02 12:38:15 +02:00
|
|
|
$ wget https://raw.githubusercontent.com/ninenines/erlang.mk/master/erlang.mk
|
2013-06-27 00:02:12 +02:00
|
|
|
```
|
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
We can now bootstrap our application. Since we are going to generate
|
|
|
|
a release, we will also bootstrap it at the same time.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
``` bash
|
|
|
|
$ make -f erlang.mk bootstrap bootstrap-rel
|
|
|
|
```
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
This creates a Makefile, a base application, and the release files
|
|
|
|
necessary for creating the release. We can already build and start
|
|
|
|
this release.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
``` bash
|
|
|
|
$ make
|
|
|
|
...
|
|
|
|
$ ./_rel/hello_erlang_release/bin/hello_erlang_release console
|
|
|
|
...
|
|
|
|
(hello_erlang@127.0.0.1)1>
|
2013-09-07 12:54:55 +02:00
|
|
|
```
|
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
Entering the command `i().` will show the running processes, including
|
|
|
|
one called `hello_erlang_sup`. This is the supervisor for our
|
2013-09-07 12:54:55 +02:00
|
|
|
application.
|
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
The release currently does nothing. In the rest of this chapter we
|
|
|
|
will add Cowboy as a dependency and write a simple "Hello world!"
|
|
|
|
handler.
|
2014-06-25 11:23:58 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
:: Cowboy setup
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-02 12:55:15 +02:00
|
|
|
To add Cowboy as a dependency to your application, you need to modify
|
2014-08-01 14:27:04 +02:00
|
|
|
two files: the Makefile and the application resource file.
|
2013-06-27 00:02:12 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
Modifying the Makefile allows the build system to know it needs to
|
|
|
|
fetch and compile Cowboy. To do that we simply need to add one line
|
|
|
|
to our Makefile to make it look like this:
|
2013-06-27 00:02:12 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
``` Makefile
|
|
|
|
PROJECT = hello_erlang
|
|
|
|
DEPS = cowboy
|
|
|
|
include erlang.mk
|
2013-06-27 00:02:12 +02:00
|
|
|
```
|
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
Modifying the application resource file, `src/hello_erlang.app.src`,
|
|
|
|
allows the build system to know it needs to include Cowboy in the
|
|
|
|
release and start it automatically. This is a different step because
|
|
|
|
some dependencies are only needed during development.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
We are simply going to add `cowboy` to the list of `applications`,
|
|
|
|
right after `stdlib`. Don't forget the comma separator.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
|
|
|
``` erlang
|
2014-08-01 14:27:04 +02:00
|
|
|
{application, hello_erlang, [
|
|
|
|
{description, "Hello Erlang!"},
|
|
|
|
{vsn, "0.1.0"},
|
|
|
|
{modules, []},
|
|
|
|
{registered, []},
|
|
|
|
{applications, [
|
|
|
|
kernel,
|
|
|
|
stdlib,
|
|
|
|
cowboy
|
|
|
|
]},
|
|
|
|
{mod, {hello_erlang_app, []}},
|
|
|
|
{env, []}
|
|
|
|
]}.
|
2013-09-07 12:54:55 +02:00
|
|
|
```
|
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
You may want to set a description for the application while you
|
|
|
|
are editing the file.
|
2013-06-27 00:02:12 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
If you run `make` now and start the release, Cowboy will be included
|
|
|
|
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.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
:: Listening for connections
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
We will do this when our application starts. It's a two step process.
|
|
|
|
First we need to define and compile the dispatch list, a list of
|
|
|
|
routes that Cowboy will use to map requests to handler modules.
|
|
|
|
Then we tell Cowboy to listen for connections.
|
2014-06-25 11:23:58 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
Open the `src/hello_erlang_app.erl` file and add the necessary
|
|
|
|
code to the `start/2` function to make it look like this:
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
``` erlang
|
|
|
|
start(_Type, _Args) ->
|
|
|
|
Dispatch = cowboy_router:compile([
|
|
|
|
{'_', [{"/", hello_handler, []}]}
|
|
|
|
]),
|
|
|
|
cowboy:start_http(my_http_listener, 100, [{port, 8080}],
|
|
|
|
[{env, [{dispatch, Dispatch}]}]
|
|
|
|
),
|
|
|
|
hello_erlang_sup:start_link().
|
2013-09-07 12:54:55 +02:00
|
|
|
```
|
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
The dispatch list is explained in great details in the
|
|
|
|
^"Routing^routing^ chapter. For this tutorial we map the
|
|
|
|
path `/` to the handler module `hello_handler`. This module
|
|
|
|
doesn't exist yet, we still have to write it.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
If you build the release, start it and open ^http://localhost:8080
|
|
|
|
now, you will get an error because the module is missing. Any
|
|
|
|
other URL, like ^http://localhost:8080/test^, will result in a
|
|
|
|
404 error.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
:: Handling requests
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
Cowboy features different kinds of handlers, including REST
|
|
|
|
and Websocket handlers. For this tutorial we will use a plain
|
|
|
|
HTTP handler.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
First, let's generate a handler from a template.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
|
|
|
``` bash
|
2014-08-01 14:27:04 +02:00
|
|
|
$ make new t=cowboy_http n=hello_handler
|
2013-09-07 12:54:55 +02:00
|
|
|
```
|
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
You can then open the `src/hello_handler.erl` file and modify
|
2014-09-30 20:12:13 +03:00
|
|
|
the `init/2` function like this to send a reply.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
|
|
|
``` erlang
|
2014-09-30 20:12:13 +03:00
|
|
|
init(Req, Opts) ->
|
2014-09-23 16:43:29 +03:00
|
|
|
Req2 = cowboy_req:reply(200,
|
2014-08-01 14:27:04 +02:00
|
|
|
[{<<"content-type">>, <<"text/plain">>}],
|
|
|
|
<<"Hello Erlang!">>,
|
|
|
|
Req),
|
2014-09-30 20:12:13 +03:00
|
|
|
{ok, Req2, Opts}.
|
2013-09-07 12:54:55 +02:00
|
|
|
```
|
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
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!`.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
If you build the release, start it and open ^http://localhost:8080
|
|
|
|
in your browser, you should get a nice `Hello Erlang!` displayed!
|