The FASTEST web framework 2020! Simple TODO service with Actix [1/3]

By | February 14, 2020

Hi guys So I was taking a look at Tech Empower’s benchmarks and the results of the last round surprised me. This framework is taking the first two positions and… there is a large gap with the next in the rank. if I’m not mistaken, this amount is “request per seconds”, and it’s ridiculous! 700000 requests per second! In fact, of the first 50 frameworks I only know VERTX The other ones are way down on the list. The top framework this time is called Actix for the Rust programming language. So what I’m going to do… right now is, I’m going to learn Rust, learn Actix, and build a pretty simple TODO service to get a sense of how it is to develop on this platform. So… What is Actix? Actix is a powerful Actor system for the rust programming language. Actix-web is a web framework built on top of Actix, which supports HTTP 1 and 2 Actix aims to be Type Safe since everything has “types”. Feature rich by providing a lot of features out of the box. Such as logging and HTTP 2 Extensible and blazingly fast, as we saw on the benchmarks. But… why Rust? Rust is also blazingly fast… memory-efficient… all that without sacrificing safety. It is able to eliminate many classes of bugs at compile time. And has really great tooling! So… let’s start by installing Rust we can do that easily by running this simple command in the terminal. Once that is done, I should have Cargo, the package manager and Rustc, the compiler So let’s create a folder Change to that directory… and… We will create our project by using Cargo init… specify –bin Since we want a binary, and the name of the project: todo-actix… A folder structure is created for us with a git repository by default. The project contains a Cargo.toml file and a source (src) directory… inside that source directory we have a main(.rs) file with a simple “Hello world” code. We can run the code we just created by using “cargo run”… it will comple the files and we will see the “Hello world” message. So… I’ll use Visual Studio Code to work on the project… since it’s free In the Cargo.toml file, we have the metadata of the project and we can specify the dependencies In the main(.rs) file we have a simple main function that prints “Hello, world!” So let’s start by adding the dependencies in the Cargo.toml file We’ll add “actix-rt” and “actix-web”… Back in the main(.rs) file, we will create an HTTP server To do so, we will call the “new” function. To access items in a module or call a static method, we use “::” in Rust. “new” expects a “factory” to create the App. So we will write an anonymous function with no parameters that will return the “App” Let’s create the App… so we’ll write “App”, call the “new” function. And this will be a builder… since this is a builder we can add routes like so: We call the “route” function and pass a path and a handler as parameters. So let’s create a “status” endpoint in the root path (“/”). In this case this will be a handler for “GET” requests… and the handler function will be called “status”. This will be an asynchronous function, so we will use the “async” keyword. And the result type will be something that implements a “Responder”. To make things easier, we will just return the json as a string. So let’s write a simple json with status “Up” That should be all for the handler… Now we just need to specify the host and the port of the server by using the method “bind”, we will tell the server to run and (a)wait The main function should also be asynchronous And the last thing will be to specify this main function so it runs in the Actix runtime (rt) and that’s done using this macro from the actix-rt module I just need to fix this typo: use (a)wait and put a “;” on the imports And also specify the return type of the main function In this case we will use an empty Result And this “()” just means “nothing”, it’s the “unit”. similar to void in other programming languages And that should be all. So let’s save and run this application It should start downloading the new libraries and compiling them. Once that is done, our server should be up To make it more obvious, let’s just print a message at the beginning so we know which host and port we are using… perfect! now… to test our server we’ll just use a curl command We got the status… To make things more interesting, instead of using a string we can add a library so we can serialize our datastructures That library is serde, so let’s add that to the dependencies To keep things a bit more organized let’s create a new file to have our models Once we create the new file we must specify the module (mod) in the main file so that’s included in the app… Great! so let’s start by creating a public (pub) struct for our Status we just need a single public attribute that will be a String Let’s also import serde so we can Deserialize our data. To do so, we will use a macro that will derive the code to Deserialize this struct. Now we can replace this string with an HttpResponse or type “Ok” (that will return a status 200) and we can call (.)json that will handle the conversion and will also add a “Content-Type” Header Now we can include our model… once we do that we can create an instance of that struct with the attribute “Ok” and we convert that to a string… We can see something is wrong in the editor, so let’s run “cargo check” Once it’s finished it should tell us the error that we are having. So it seems that we need to “Serialize”, not “Deseriaze” So we just need to change that in the models file, make sure to save and now the error is gone We check again… everything looks fine and we can run the app again Let’s try with the curl command… it’s there… we just made (a small mistake) we printed “Ok” instead of “Up “… easy to fix. We fix that… done! perfect! We have the same result So that will be all for this video In the next video we will add the configuration for the service we will make the database connection and we’ll create the models for our TODO service Thanks for watching! If you liked the video please consider hitting the like button Subscribe and hit the “bell” (button) for future videos and if you have any question, just leave a comment. Thanks for watching!

6 thoughts on “The FASTEST web framework 2020! Simple TODO service with Actix [1/3]

  1. genus-v Post author

    Make sure to take a look at the source code in the description!
    – 0:07 – Benchmarks
    – 0:56 – What is Actix and Rust?
    – 1:49 – Installing Rust and creating a project
    – 3:04 – Creating your first service (status)
    – 5:33 – Serialize data as json

  2. Isaak Barra Post author

    Nice tutorial can you please add quassar as frontend ?

  3. Shirshak Post author

    thanks 🙂 A video of login system using cookie, redis would be highly cool 😀 Thanks again.

  4. discite Post author

    Good job, subscribed because you put a good effort, hope you do well


Leave a Reply

Your email address will not be published. Required fields are marked *