| 1 |
[[getting_started]] |
| 2 |
== Getting started |
| 3 |
|
| 4 |
Erlang is more than a language, it is also an operating system |
| 5 |
for your applications. Erlang developers rarely write standalone |
| 6 |
modules, they write libraries or applications, and then bundle |
| 7 |
those into what is called a release. A release contains the |
| 8 |
Erlang VM plus all applications required to run the node, so |
| 9 |
it can be pushed to production directly. |
| 10 |
|
| 11 |
This chapter walks you through all the steps of setting up |
| 12 |
Cowboy, writing your first application and generating your first |
| 13 |
release. At the end of this chapter you should know everything |
| 14 |
you need to push your first Cowboy application to production. |
| 15 |
|
| 16 |
=== Prerequisites |
| 17 |
|
| 18 |
We are going to use the https://github.com/ninenines/erlang.mk[Erlang.mk] |
| 19 |
build system. If you are using Windows, please check the |
| 20 |
http://erlang.mk/guide/installation.html[Installation instructions] |
| 21 |
to get your environment setup before you continue. |
| 22 |
|
| 23 |
=== Bootstrap |
| 24 |
|
| 25 |
First, let's create the directory for our application. |
| 26 |
|
| 27 |
[source,bash] |
| 28 |
$ mkdir hello_erlang |
| 29 |
$ cd hello_erlang |
| 30 |
|
| 31 |
Then we need to download Erlang.mk. Either use the following |
| 32 |
command or download it manually. |
| 33 |
|
| 34 |
[source,bash] |
| 35 |
$ wget https://erlang.mk/erlang.mk |
| 36 |
|
| 37 |
We can now bootstrap our application. Since we are going to generate |
| 38 |
a release, we will also bootstrap it at the same time. |
| 39 |
|
| 40 |
[source,bash] |
| 41 |
$ make -f erlang.mk bootstrap bootstrap-rel |
| 42 |
|
| 43 |
This creates a Makefile, a base application, and the release files |
| 44 |
necessary for creating the release. We can already build and start |
| 45 |
this release. |
| 46 |
|
| 47 |
[source,bash] |
| 48 |
---- |
| 49 |
$ make run |
| 50 |
... |
| 51 |
([email protected])1> |
| 52 |
---- |
| 53 |
|
| 54 |
Entering the command `i().` will show the running processes, including |
| 55 |
one called `hello_erlang_sup`. This is the supervisor for our |
| 56 |
application. |
| 57 |
|
| 58 |
The release currently does nothing. In the rest of this chapter we |
| 59 |
will add Cowboy as a dependency and write a simple "Hello world!" |
| 60 |
handler. |
| 61 |
|
| 62 |
=== Cowboy setup |
| 63 |
|
| 64 |
We will modify the 'Makefile' to tell the build system it needs to |
| 65 |
fetch and compile Cowboy, and that we will use releases: |
| 66 |
|
| 67 |
[source,makefile] |
| 68 |
---- |
| 69 |
PROJECT = hello_erlang |
| 70 |
|
| 71 |
DEPS = cowboy |
| 72 |
dep_cowboy_commit = 2.18.0 |
| 73 |
|
| 74 |
REL_DEPS = relx |
| 75 |
|
| 76 |
DEP_PLUGINS = cowboy |
| 77 |
|
| 78 |
include erlang.mk |
| 79 |
---- |
| 80 |
|
| 81 |
The `DEP_PLUGINS` line tells the build system to load the plugins |
| 82 |
Cowboy provides. These include predefined templates that we will |
| 83 |
use soon. |
| 84 |
|
| 85 |
The `REL_DEPS` line tells the build system to fetch and build |
| 86 |
`relx`, the library that will create the release. |
| 87 |
|
| 88 |
If you do `make run` now, Cowboy will be included in the release |
| 89 |
and started automatically. This is not enough however, as Cowboy |
| 90 |
doesn't do anything by default. We still need to tell Cowboy to |
| 91 |
listen for connections. |
| 92 |
|
| 93 |
=== Listening for connections |
| 94 |
|
| 95 |
First we define the routes that Cowboy will use to map requests |
| 96 |
to handler modules, and then we start the listener. This is best |
| 97 |
done at application startup. |
| 98 |
|
| 99 |
Open the 'src/hello_erlang_app.erl' file and add the necessary |
| 100 |
code to the `start/2` function to make it look like this: |
| 101 |
|
| 102 |
[source,erlang] |
| 103 |
---- |
| 104 |
start(_Type, _Args) -> |
| 105 |
Dispatch = cowboy_router:compile([ |
| 106 |
{'_', [{"/", hello_handler, []}]} |
| 107 |
]), |
| 108 |
{ok, _} = cowboy:start_clear(my_http_listener, |
| 109 |
[{port, 8080}], |
| 110 |
#{env => #{dispatch => Dispatch}} |
| 111 |
), |
| 112 |
hello_erlang_sup:start_link(). |
| 113 |
---- |
| 114 |
|
| 115 |
Routes are explained in details in the xref:routing[Routing] |
| 116 |
chapter. For this tutorial we map the path `/` to the handler |
| 117 |
module `hello_handler`. This module doesn't exist yet. |
| 118 |
|
| 119 |
Build and start the release, then open http://localhost:8080 |
| 120 |
in your browser. You will get a 500 error because the module is missing. |
| 121 |
Any other URL, like http://localhost:8080/test, will result in a |
| 122 |
404 error. |
| 123 |
|
| 124 |
=== Handling requests |
| 125 |
|
| 126 |
Cowboy features different kinds of handlers, including REST |
| 127 |
and Websocket handlers. For this tutorial we will use a plain |
| 128 |
HTTP handler. |
| 129 |
|
| 130 |
Generate a handler from a template: |
| 131 |
|
| 132 |
[source,bash] |
| 133 |
$ make new t=cowboy.http n=hello_handler |
| 134 |
|
| 135 |
Then, open the 'src/hello_handler.erl' file and modify |
| 136 |
the `init/2` function like this to send a reply. |
| 137 |
|
| 138 |
[source,erlang] |
| 139 |
---- |
| 140 |
init(Req0, State) -> |
| 141 |
Req = cowboy_req:reply(200, |
| 142 |
#{<<"content-type">> => <<"text/plain">>}, |
| 143 |
<<"Hello Erlang!">>, |
| 144 |
Req0), |
| 145 |
{ok, Req, State}. |
| 146 |
---- |
| 147 |
|
| 148 |
What the above code does is send a 200 OK reply, with the |
| 149 |
Content-type header set to `text/plain` and the response |
| 150 |
body set to `Hello Erlang!`. |
| 151 |
|
| 152 |
If you run the release and open http://localhost:8080 |
| 153 |
in your browser, you should get a nice `Hello Erlang!` displayed! |