getting_started.asciidoc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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,make]
  50. ----
  51. PROJECT = hello_erlang
  52. DEPS = cowboy
  53. dep_cowboy_commit = master
  54. include erlang.mk
  55. ----
  56. If you run `make run` now, Cowboy will be included in the release
  57. and started automatically. This is not enough however, as Cowboy
  58. doesn't do anything by default. We still need to tell Cowboy to
  59. listen for connections.
  60. === Listening for connections
  61. First we define the routes that Cowboy will use to map requests
  62. to handler modules, and then we start the listener. This is best
  63. done at application startup.
  64. Open the 'src/hello_erlang_app.erl' file and add the necessary
  65. code to the `start/2` function to make it look like this:
  66. [source,erlang]
  67. ----
  68. start(_Type, _Args) ->
  69. Dispatch = cowboy_router:compile([
  70. {'_', [{"/", hello_handler, []}]}
  71. ]),
  72. {ok, _} = cowboy:start_clear(my_http_listener, 100,
  73. [{port, 8080}],
  74. #{env => #{dispatch => Dispatch}}
  75. ),
  76. hello_erlang_sup:start_link().
  77. ----
  78. Routes are explained in details in the xref:routing[Routing]
  79. chapter. For this tutorial we map the path `/` to the handler
  80. module `hello_handler`. This module doesn't exist yet.
  81. Build and start the release, then open http://localhost:8080
  82. in your browser. You will get an error because the module is missing.
  83. Any other URL, like http://localhost:8080/test, will result in a
  84. 404 error.
  85. === Handling requests
  86. Cowboy features different kinds of handlers, including REST
  87. and Websocket handlers. For this tutorial we will use a plain
  88. HTTP handler.
  89. Generate a handler from a template:
  90. [source,bash]
  91. $ make new t=cowboy_http n=hello_handler
  92. Then, open the 'src/hello_handler.erl' file and modify
  93. the `init/2` function like this to send a reply.
  94. [source,erlang]
  95. ----
  96. init(Req, State) ->
  97. cowboy_req:reply(200,
  98. #{<<"content-type">> => <<"text/plain">>},
  99. <<"Hello Erlang!">>,
  100. Req),
  101. {ok, Req, State}.
  102. ----
  103. What the above code does is send a `200 OK` reply, with the
  104. Content-type header set to `text/plain` and the response
  105. body set to `Hello Erlang!`.
  106. If you run the release and open http://localhost:8080
  107. in your browser, you should get a nice `Hello Erlang!` displayed!