In this tutorial, you’ll learn how to build a web server with Rust
and the Actix-web
framework on a DigitalOcean Droplet. You’ll set up a basic server and create routes to manage a simple list of Todo items, supporting common RESTful operations like retrieving and creating Todos.
Before you begin, ensure you have the following:
SSH into your DigitalOcean Droplet: Open your terminal and use the following command to SSH into your Droplet. Replace your_username
with your actual username and your_droplet_ip
with the IP address of your Droplet:
Update the package list: Once logged in, update the package list to ensure you have the latest information on available packages:
Install Rust: To install Rust, use the following command, which downloads and runs the Rust installation script:
Follow the on-screen instructions to complete the installation. After installation, you may need to restart your terminal or run:
Install necessary libraries: Install the required libraries for building Rust applications. You can install build-essential
and libssl-dev
using:
Verify Rust installation: To confirm that Rust has been installed correctly, check the version:
Install Cargo (Rust package manager): Cargo is installed automatically with Rust. You can verify its installation by checking the version:
Let’s create a new rust project sammy_todos
using cargo
Next, find the Cargo.toml
file. Navigate to the root directory of your Rust project. The Cargo.toml
file is located in the same directory where you created your project (e.g., sammy_todos
). You can use the following command to change to your project directory:
Below are the Rust project files files which are automatically generated:
Add actix-web
and serde
as dependencies in Cargo.toml
. Here, serde
is needed for Serialization and Deserialization.
And add the following lines under the dependencies
.
Now to download and compile dependencies use the below command:
In main.rs
, add the following lines of code to set up a basic server. You must navigate to the src
directory inside your Project.
Run the server:
To test the server, open a new terminal session and run the following command:
You should get the following response:
Todo model is needed to serialize and deserialize the request and response. Add the following lines of code inside the main.rs
file.
This API creates todo, and this is a dummy API that returns your todo only, but you can implement it with databases to make it persistent
This API returns todo by id:
This is how the rust server main.rs
would look like:
Now, you can test it by sending a POST
request with curl
:
Congratulations! You have successfully set up a basic web server using Rust and Actix. This tutorial taught you to create endpoints to retrieve and create Todo items, providing a solid foundation for building more complex applications. To enhance your app further, consider integrating a database solution for data persistence, allowing you to store and manage your Todo items effectively. Keep exploring, and happy coding!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!