Let's go ahead and open your favorite Command-line terminal and create a new Cargo project via cargo new my-service -bin.
If you'd like to follow along with code by your side, I've made the source code for this project available on GitHub. If this is your first time seeing the Rust programming language, I hope this blog post inspires you to take a deeper look into an interesting statically typed language and ecosystem.
The easiest way to install these tools is to use rustup.Īlso have Docker installed on your machine so we can easily create and connect to a PostgreSQL database instance. Prerequisitesīefore getting started make sure that you have Cargo and Rust installed on your machine. In this blog post, we are going to implement the Health Check API pattern using the actix-web web framework that uses sqlx to connect to a local PostgreSQL database instance. However, in many web frameworks you have to build this Health Check API behavior yourself. In web frameworks like Spring a drop-in solution like Spring Actuator is available for you to integrate into your Spring project. This /health API endpoint solution is an implementation of the Health Check API pattern, a pattern for checking the health of your API service. I was able to accomplish this requirement by adding a /health API endpoint that responds with an Ok or ServiceUnavailable HTTP status based on the current state of your service. Here we tell diesel to install only postgres specific configuration since we are only interested in PostgreSQL as a database.This week while I was preparing a Rust-based backend service to be deployed to a Kubernetes cluster, I realized I hadn't yet configured my backend service to be probed by kubelet for liveness and readiness checks. You can do this by running source $ HOME /. > If you run into an error here, make sure you've added Cargo's bin directory in your system's PATH environment variable. For that, we will need to start with installing the diesel CLI: cargo install diesel_cli -no-default-features -features postgres This is a great resource for setting up PostgreSQL on Mac.ĭiesel needs its own setup steps. > Note Make sure you have PostgreSQL installed before running the next command. The next step is to put our environment variable named DATABASE_URL in the file. Since our API always needs a backing database, this is an irrecoverable error for us. Our main function tries to get the value of that variable and aborts if it is not set. We also need database connection information, which we get from an environment variable called DATABASE_URL. This enables the handler functions to interact with the database independently.
The most important change since the previous version is passing in the database connection pool to each of the handlers via a. toml file and edit it to add all the packages that we need, the file should look like this: Ĭhrono = This will create a directory with the given name and a few files in it. We will then initialize an empty project using Cargo: cargo init -bin rust-blogpost-auth-async During installation, select the default option (which should amend $ PATH to include cargo installation directory). The installation instructions are available here. The community supported method is using Rustup, so that's what we'll use in this tutorial. The first step is to install Rust and all related tools. Finally, we will implement authentication for our API using Auth0. Our API will be backed by a Postgres database using Diesel. In this article, we will explore the actix-web web framework by writing a small CRUD API using it. Although the project was temporarily on hold in early 2020, the project ownership has moved to a new maintainer, and development continues. The Actix project also maintains a RESTful API development framework, which is widely regarded as a fast and performant web framework.
Microsoft, for example, sponsors the Actix project on GitHub, which is a general purpose open source actor framework based on Rust. Companies like Amazon and Microsoft have adopted it for a growing number of use cases.
Rust has picked up a lot of momentum since we last looked at it in 2015.