Web Service Tutorial in Rust
Overview
This project serves as a comprehensive tutorial for building a web service using the Rust programming language. It covers various aspects, from setting up a PostgreSQL database to handling JSON responses.
Technologies Used
- Rust: The core programming language used for this project.
- Tokio: An asynchronous runtime for Rust.
- SQLx: A Rust library for connecting to PostgreSQL databases.
- Dotenv: A Rust crate for handling
.envfiles. - Serde: A Rust crate for serializing and deserializing data structures.
- Actix-Web: A Rust framework for building web services.
- Chrono: A time manipulation library for Rust.
Documentation
To generate documentation for this project:
Getting Started
Prerequisites
- Ensure you have Docker Compose installed. If not, you can download it from Docker Desktop.
Setup
-
Environment Variables: Copy the sample environment file and configure the variables.
-
Docker Compose: Start the Docker containers.
-
Upload Postman Collection: Open up Postman locally via the desktop client. Then upload the file entitled
Web Service Tutorial.postman_collection.json. You should now have access to the Postman collection. -
Get a Bearer Token:
- Go to the
create_userroute under theauthfolder. - Create a user by hitting the
Sendbutton. If there's a problem, change both the username and password. - After you have successfully created a new user, check the headers. Under the
authorizationheader, you should see the bearer token. Copy everything after where it saysBearer. - Now click on the
Web Service TutorialPostman collection. - Under the
Authorizationpage, you should see aTypedropdown. - Select
Bearer Token. After you selectBearer Token, underneath should apear another dropdown that saysToken. Paste the copied bearer token here from before. You should now be authorized for four hours, or for however longJWT_HOURS_ACTIVEis set to in your .env file. - If you have any issues hitting the routes, try changing every
localhost:8080tohttp://127.0.0.1:8080
- Go to the
-
Test Routes: Open your browser or use a tool like Postman to hit the following route:
http://127.0.0.1:8080/blog
Database GUI (PgAdmin4)
- Access the PgAdmin4 interface at
http://localhost:16543. - Username:
test@test.com - Password:
test
For detailed instructions on adding a PostgreSQL server in PgAdmin4, refer to the PG Admin guide.
SQL Schema
The init.sql file contains the SQL statements that define the database schema. Feel free to explore it to understand the database structure.
Authentication
The SKIP_AUTH environment variable controls JWT authentication. Set it to true to disable JWT during development.