Elixir trello api1/12/2024 ![]() # Build a connection which is GET request on / url # Create a test with the name "return ok" # Note: is module attribute unary operator # returned options in a Module attribute opts. # We call the Plug init/1 function with the options then store # which can be used to make the HTTP request # This makes the conn object avaiable in the scope of the tests, # tests in this module concurrently with tests in other modules # Bringing ExUnit's case module to scope and configure it to run Replace file rest_api_test.exs in the test directory with the following: defmodule RestApiTest.Router do But we can, of course, unit test our router. For this tutorial, we are just going to define the empty root path ( /) handler that sends a 200 OK status response back.Īt this point, our application server is still not in a runnable state. We have defined a new module called Router in our RestApi project where we will use the Plug.Router to handle our incoming connections. # Fallback handler when there was no match # Dispatch the connection to the matched handler # parse the body if there is a matching route.(Using the Jayson parser) The order is important here, as we only want to # Once there is a match, parse the response body if the content-type # Tell Plug to match the incoming request with the defined endpoints # Attach the Logger to log incoming requests Next, run this command in the terminal so that mix can install all the required dependencies: $ mix deps.get Install all dependencies Handle Routesīefore we can start testing our application, we need to build the router and configure our application to start listening on port 8080.Ĭreate a new file router.ex in lib/rest_api directory with the following code: defmodule RestApi.Router do This is similar to the body-parser middleware in express. jason- It is a super-fast JSON parser and generator which we will use to handle JSON. ![]() Both Plug & Cowboy together provide a simple framework like express.js. ![]() It is a web server, written in Erlang (not Elixir), that handles all the connections and processes any incoming and outgoing request. This is where Cowboy comes into the picture. Plug gives us tools to work with HTTP requests, like building routers and endpoints, setting status code, body parsing, etc., but it does not know how to handle connections. plug_cowboy - This module is composed of two other different modules Plug and Cowboy.The first thing we need to do is add some dependencies. In production, once our application crashes beyond repair, we would want the whole VM-node to terminate otherwise, our monitoring systems would not recognize any errors. deps: It returns a list of dependencies of this project.application: It is the entry point to our application.project: It returns the project configuration like the project name, version, elixir version to use, etc.The exs are used for scripting and are not intended to be compiled with the application. The first thing you will notice that is different about the mix file is the extension. This file defines a special module called RestApi.MixProject that contains the information about our project, application and lists all our dependencies. The mix.exs file is similar to package.json in the node.js world. If you are using VSCode, you can simply execute $ code rest_api. └── rest_api_test.exs (File to test our application)Ĭhange your directory to rest_api and open this directory in your favourite text editor. ├── test_helper.exs (File to write our test helpers) │ └── rest_api/application.ex (File defining a OTP Application with supervisor) │ ├── rest_api.ex (File defining the root Module for our application) ├── mix.exs (Mix project definition file. gitignore (A default gitignore file with all temporary files ignored) formatter.exs (Used by mix to format your elixir files) On successful execution, you will see that the following files have been created in a directory named rest_api. We will learn more about why we need a supervision tree later in this chapter. -sup flag creates our application with a supervision tree.rest_api is the name of our application.mix new command used to create a new project in the current directory.mix it is similar to npm in Node.js universe but much more powerful. mix is the built-in build tool that comes packaged with Elixir, which can be used to create, compile, test and manage dependencies for elixir projects. ![]() Let's deconstruct this command to understand what each of the tokens means. Open your terminal, go to the directory where you keep your personal project and type in the following command: $ mix new rest_api -sup Once that is done, the first thing that we need to do is create our project. I assume that you have Elixir already set up on your system if not, you can follow the official Installing Elixir guide. The source code is also available on Github.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |