getting_started.asciidoc 4.2 KB

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