getting_started.ezdoc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ::: Getting started
  2. Erlang is more than a language, it is also an operating system
  3. for your applications. Erlang developers rarely write standalone
  4. modules, they write libraries or applications, and then bundle
  5. those into what is called a release. A release contains the
  6. Erlang VM plus all applications required to run the node, so
  7. it can be pushed to production directly.
  8. This chapter walks you through all the steps of setting up
  9. Cowboy, writing your first application and generating your first
  10. release. At the end of this chapter you should know everything
  11. you need to push your first Cowboy application to production.
  12. :: Bootstrap
  13. We are going to use the ^"erlang.mk^https://github.com/ninenines/erlang.mk
  14. build system. It also offers bootstrap features allowing us to
  15. quickly get started without having to deal with minute details.
  16. First, let's create the directory for our application.
  17. ``` bash
  18. $ mkdir hello_erlang
  19. $ cd hello_erlang
  20. ```
  21. Then we need to download `erlang.mk`. Either use the following
  22. command or download it manually.
  23. ``` bash
  24. $ wget https://raw.githubusercontent.com/ninenines/erlang.mk/master/erlang.mk
  25. ```
  26. We can now bootstrap our application. Since we are going to generate
  27. a release, we will also bootstrap it at the same time.
  28. ``` bash
  29. $ make -f erlang.mk bootstrap bootstrap-rel
  30. ```
  31. This creates a Makefile, a base application, and the release files
  32. necessary for creating the release. We can already build and start
  33. this release.
  34. ``` bash
  35. $ make
  36. ...
  37. $ ./_rel/hello_erlang_release/bin/hello_erlang_release console
  38. ...
  39. (hello_erlang@127.0.0.1)1>
  40. ```
  41. Entering the command `i().` will show the running processes, including
  42. one called `hello_erlang_sup`. This is the supervisor for our
  43. application.
  44. The release currently does nothing. In the rest of this chapter we
  45. will add Cowboy as a dependency and write a simple "Hello world!"
  46. handler.
  47. :: Cowboy setup
  48. To add Cowboy as a dependency to your application, you need to modify
  49. two files: the Makefile and the application resource file.
  50. Modifying the Makefile allows the build system to know it needs to
  51. fetch and compile Cowboy. To do that we simply need to add one line
  52. to our Makefile to make it look like this:
  53. ``` Makefile
  54. PROJECT = hello_erlang
  55. DEPS = cowboy
  56. include erlang.mk
  57. ```
  58. Modifying the application resource file, `src/hello_erlang.app.src`,
  59. allows the build system to know it needs to include Cowboy in the
  60. release and start it automatically. This is a different step because
  61. some dependencies are only needed during development.
  62. We are simply going to add `cowboy` to the list of `applications`,
  63. right after `stdlib`. Don't forget the comma separator.
  64. ``` erlang
  65. {application, hello_erlang, [
  66. {description, "Hello Erlang!"},
  67. {vsn, "0.1.0"},
  68. {modules, []},
  69. {registered, []},
  70. {applications, [
  71. kernel,
  72. stdlib,
  73. cowboy
  74. ]},
  75. {mod, {hello_erlang_app, []}},
  76. {env, []}
  77. ]}.
  78. ```
  79. You may want to set a description for the application while you
  80. are editing the file.
  81. If you run `make` now and start the release, Cowboy will be included
  82. and started automatically. This is not enough however, as Cowboy
  83. doesn't do anything by default. We still need to tell Cowboy to
  84. listen for connections.
  85. :: Listening for connections
  86. We will do this when our application starts. It's a two step process.
  87. First we need to define and compile the dispatch list, a list of
  88. routes that Cowboy will use to map requests to handler modules.
  89. Then we tell Cowboy to listen for connections.
  90. Open the `src/hello_erlang_app.erl` file and add the necessary
  91. code to the `start/2` function to make it look like this:
  92. ``` erlang
  93. start(_Type, _Args) ->
  94. Dispatch = cowboy_router:compile([
  95. {'_', [{"/", hello_handler, []}]}
  96. ]),
  97. cowboy:start_http(my_http_listener, 100, [{port, 8080}],
  98. [{env, [{dispatch, Dispatch}]}]
  99. ),
  100. hello_erlang_sup:start_link().
  101. ```
  102. The dispatch list is explained in great details in the
  103. ^"Routing^routing^ chapter. For this tutorial we map the
  104. path `/` to the handler module `hello_handler`. This module
  105. doesn't exist yet, we still have to write it.
  106. If you build the release, start it and open ^http://localhost:8080
  107. now, you will get an error because the module is missing. Any
  108. other URL, like ^http://localhost:8080/test^, will result in a
  109. 404 error.
  110. :: Handling requests
  111. Cowboy features different kinds of handlers, including REST
  112. and Websocket handlers. For this tutorial we will use a plain
  113. HTTP handler.
  114. First, let's generate a handler from a template.
  115. ``` bash
  116. $ make new t=cowboy_http n=hello_handler
  117. ```
  118. You can then open the `src/hello_handler.erl` file and modify
  119. the `init/2` function like this to send a reply.
  120. ``` erlang
  121. init(Req, Opts) ->
  122. Req2 = cowboy_req:reply(200,
  123. [{<<"content-type">>, <<"text/plain">>}],
  124. <<"Hello Erlang!">>,
  125. Req),
  126. {ok, Req2, Opts}.
  127. ```
  128. What the above code does is send a `200 OK` reply, with the
  129. `content-type` header set to `text/plain` and the response
  130. body set to `Hello Erlang!`.
  131. If you build the release, start it and open ^http://localhost:8080
  132. in your browser, you should get a nice `Hello Erlang!` displayed!