2016-01-14 13:35:25 +01:00
|
|
|
[[getting_started]]
|
|
|
|
== 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.
|
|
|
|
|
2016-05-24 14:50:27 +02:00
|
|
|
=== Prerequisites
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
We are going to use the https://github.com/ninenines/erlang.mk[Erlang.mk]
|
2016-05-24 14:50:27 +02:00
|
|
|
build system. If you are using Windows, please check the
|
|
|
|
http://erlang.mk/guide/installation.html[Installation instructions]
|
|
|
|
to get your environment setup before you continue.
|
|
|
|
|
|
|
|
=== Bootstrap
|
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
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,bash]
|
2013-09-07 12:54:55 +02:00
|
|
|
$ mkdir hello_erlang
|
|
|
|
$ cd hello_erlang
|
2013-06-27 00:02:12 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
Then we need to download Erlang.mk. Either use the following
|
2014-08-01 14:27:04 +02:00
|
|
|
command or download it manually.
|
2013-06-27 00:02:12 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,bash]
|
2016-05-24 14:50:27 +02:00
|
|
|
$ wget https://erlang.mk/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
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,bash]
|
2014-08-01 14:27:04 +02:00
|
|
|
$ 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
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,bash]
|
|
|
|
----
|
|
|
|
$ make run
|
2014-08-01 14:27:04 +02:00
|
|
|
...
|
|
|
|
(hello_erlang@127.0.0.1)1>
|
2016-01-14 13:35:25 +01:00
|
|
|
----
|
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
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
=== Cowboy setup
|
2013-06-27 00:02:12 +02:00
|
|
|
|
2016-05-24 14:50:27 +02:00
|
|
|
We will modify the 'Makefile' to tell the build system it needs to
|
|
|
|
fetch and compile Cowboy:
|
2013-06-27 00:02:12 +02:00
|
|
|
|
2016-08-30 13:03:26 +02:00
|
|
|
[source,makefile]
|
2016-01-15 11:19:59 +01:00
|
|
|
----
|
2014-08-01 14:27:04 +02:00
|
|
|
PROJECT = hello_erlang
|
2016-01-15 11:19:59 +01:00
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
DEPS = cowboy
|
2016-01-15 11:19:59 +01:00
|
|
|
dep_cowboy_commit = master
|
|
|
|
|
2016-12-28 17:54:50 +01:00
|
|
|
DEP_PLUGINS = cowboy
|
|
|
|
|
2014-08-01 14:27:04 +02:00
|
|
|
include erlang.mk
|
2016-01-15 11:19:59 +01:00
|
|
|
----
|
2016-01-14 13:35:25 +01:00
|
|
|
|
2016-12-28 17:54:50 +01:00
|
|
|
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
|
2014-08-01 14:27:04 +02:00
|
|
|
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
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
=== Listening for connections
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2016-05-24 14:50:27 +02:00
|
|
|
First we define the routes that Cowboy will use to map requests
|
|
|
|
to handler modules, and then we start the listener. This is best
|
|
|
|
done at application startup.
|
2014-06-25 11:23:58 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
Open the 'src/hello_erlang_app.erl' file and add the necessary
|
2014-08-01 14:27:04 +02:00
|
|
|
code to the `start/2` function to make it look like this:
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,erlang]
|
|
|
|
----
|
2014-08-01 14:27:04 +02:00
|
|
|
start(_Type, _Args) ->
|
2016-09-14 18:51:11 +02:00
|
|
|
Dispatch = cowboy_router:compile([
|
|
|
|
{'_', [{"/", hello_handler, []}]}
|
|
|
|
]),
|
|
|
|
{ok, _} = cowboy:start_clear(my_http_listener, 100,
|
|
|
|
[{port, 8080}],
|
|
|
|
#{env => #{dispatch => Dispatch}}
|
|
|
|
),
|
|
|
|
hello_erlang_sup:start_link().
|
2016-01-14 13:35:25 +01:00
|
|
|
----
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2016-05-24 14:50:27 +02:00
|
|
|
Routes are explained in details in the xref:routing[Routing]
|
|
|
|
chapter. For this tutorial we map the path `/` to the handler
|
|
|
|
module `hello_handler`. This module doesn't exist yet.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2016-05-24 14:50:27 +02:00
|
|
|
Build and start the release, then open http://localhost:8080
|
2016-08-24 17:25:33 +02:00
|
|
|
in your browser. You will get a 500 error because the module is missing.
|
2016-01-14 13:35:25 +01:00
|
|
|
Any other URL, like http://localhost:8080/test, will result in a
|
2014-08-01 14:27:04 +02:00
|
|
|
404 error.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2016-01-14 13:35:25 +01: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
|
|
|
|
2016-05-24 14:50:27 +02:00
|
|
|
Generate a handler from a template:
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,bash]
|
2016-12-28 17:54:50 +01:00
|
|
|
$ make new t=cowboy.http n=hello_handler
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2016-05-24 14:50:27 +02:00
|
|
|
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
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,erlang]
|
|
|
|
----
|
2016-08-24 17:25:33 +02:00
|
|
|
init(Req0, State) ->
|
2016-09-14 18:51:11 +02:00
|
|
|
Req = cowboy_req:reply(200,
|
|
|
|
#{<<"content-type">> => <<"text/plain">>},
|
|
|
|
<<"Hello Erlang!">>,
|
|
|
|
Req0),
|
|
|
|
{ok, Req, State}.
|
2016-01-14 13:35:25 +01:00
|
|
|
----
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2016-12-28 17:54:50 +01:00
|
|
|
What the above code does is send a 200 OK reply, with the
|
2016-05-24 14:50:27 +02:00
|
|
|
Content-type header set to `text/plain` and the response
|
2014-08-01 14:27:04 +02:00
|
|
|
body set to `Hello Erlang!`.
|
2013-09-07 12:54:55 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
If you run the release and open http://localhost:8080
|
2014-08-01 14:27:04 +02:00
|
|
|
in your browser, you should get a nice `Hello Erlang!` displayed!
|