feat(docs): reorganized docs

This commit is contained in:
minhtrannhat 2024-05-04 11:34:20 -04:00
parent c8fe98eb64
commit 5090157d91
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
5 changed files with 40 additions and 20 deletions

10
docs/actix_web.md Normal file
View File

@ -0,0 +1,10 @@
# Actix-web Notes
- Actix-web runtime model: Spins up a worker process per CPU core. Each worker runs its own copy of of the application provided in the closure that `HttpServer` takes as an argument. Therefore, everything in the closure (including `app_data` below) has to implement the `Clone` trait.
## `app_data`
When we set up our web app, we can attach a resource to the app with `app_data`. And then our route handlers can access this application data with `HttpRequest::app_data()`. It is similar to FastAPI's dependency injection.
In our app, we want to inject a `db_conn` to the route handlers, so that these routes can handle PostgreSQL read/write.

5
docs/database.md Normal file
View File

@ -0,0 +1,5 @@
# Database Notes
## SQLx
The SQLx library will run compile time checks to make sure our SQL queries are valid. This is done by running PostgreSQL queries during compile time. Therefore, it is important that DATABASE_URL must be properly set.

View File

@ -5,24 +5,9 @@
- `health_check`: returns a HTTP code 200 if the server is up and running. Response body is empty.
- `subscribe` returns a HTTP code 200 if the user successfully subscribed to our email newsletter service. 400 if data is missing or invalid.
## The `tokio` Async Runtime
## Other topics
`tokio` will be the engine in charge of driving Futures to completion
- `#[tokio::main]` macro is just a builder to help building the runtime. It is basically saying, we want this async function to be run.
- `tokio::spawn(/*async task*/)` will spawn an async task to be run. We can continue executing other code concurrently while `task` runs in the background. If we need to wait for `task` to complete before proceeding, we can use `task.await` (which `#[tokio::test]` will take care for us in the mean time).
### Testing with `tokio`
- Each test has to be marked `#[tokio::test]`.
- This macro will build a new async runtime for that one test.
- By calling `spawn_app()`, we will get a API HttpServer running for the entire duration of the single test that the macro above belongs to.
## The Test Suite
- The OS will find an available port for the test suite to use.
- We use the same PostgreSQL database instance for both testing and production environment (might bite us in the ass later ?).
## SQLx
The SQLx library will run compile time checks to make sure our SQL queries are valid. This is done by running PostgreSQL queries during compile time. Therefore, it is important that DATABASE_URL must be properly set.
- [Database (PostgreSQL)](./database.md)
- [Testing](./technical_write_up.md)
- [Actic-web](./actix_web.md)
- [tokio](./tokio.md)

12
docs/testing.md Normal file
View File

@ -0,0 +1,12 @@
# Testing Notes
## The Test Suite
- The OS will find an available port for the test suite to use.
- We use the same PostgreSQL database instance for both testing and production environment (might bite us in the ass later ?).
## Testing with `tokio`
- Each test has to be marked `#[tokio::test]`.
- This macro will build a new async runtime for that one test.
- By calling `spawn_app()`, we will get a API HttpServer running for the entire duration of the single test that the macro above belongs to.

8
docs/tokio.md Normal file
View File

@ -0,0 +1,8 @@
# tokio Notes
## The `tokio` Async Runtime
`tokio` will be the engine in charge of driving Futures to completion
- `#[tokio::main]` macro is just a builder to help building the runtime. It is basically saying, we want this async function to be run.
- `tokio::spawn(/*async task*/)` will spawn an async task to be run. We can continue executing other code concurrently while `task` runs in the background. If we need to wait for `task` to complete before proceeding, we can use `task.await` (which `#[tokio::test]` will take care for us in the mean time).