diff --git a/cors/backend/src/main.rs b/cors/backend/src/main.rs index 55ab0df..81dd87e 100644 --- a/cors/backend/src/main.rs +++ b/cors/backend/src/main.rs @@ -26,6 +26,7 @@ async fn main() -> io::Result<()> { .service(user::info) }) .bind(("127.0.0.1", 8080))? + .workers(2) .run() .await } diff --git a/databases/mysql/README.md b/databases/mysql/README.md index c790025..d99f367 100644 --- a/databases/mysql/README.md +++ b/databases/mysql/README.md @@ -1,79 +1,59 @@ # MySQL -This RESTful Actix Web API examples illustrates how to connect to MySQL database using a MySQL client library implemented in Rust; e.g., MySQL database driver. +This RESTful Actix Web API illustrates how to use a MySQL database as a data source for various endpoints. -Below APIs are supported: +You'll need to have a MySQL (or compatible) server running on your machine to test this example. -- Add Bank -- Add Branch -- Add Teller -- Add Customer -- Get Bank -- Get Branch -- Get Teller -- Get Customer +## Usage -The RESTful Actix Web API has below listed dependencies: +All the following commands assume that your current working directory is _this_ directory. I.e.: -- [Actix Web](https://github.com/actix/actix-web) web framework for Rust -- [Serde](https://github.com/serde-rs/serde) for serializing and deserializing Rust data structures -- [MySQL Server](https://github.com/mysql/mysql-server) MySQL database server -- [MySQL](https://github.com/blackbeam/rust-mysql-simple) MySQL database driver +```console +$ pwd +.../databases/mysql +``` -## Instructions +1. Create database and tables: -### NOTE: + The `sql` directory contains the SQL files used for database setup: -You may need to ensure that you are running the commands with the correct MySQL user/password. - -1. Access MySQL Server: - - Log in to the MySQL Server using a user account that has the `CREATE DATABASE` privilege. - -1. Create database: - - ```sql - CREATE DATABASE my_bank; + ```sh + mysql -u root -p < sql/create_database.sql + mysql -u root -p my_bank < sql/bank_details.sql + mysql -u root -p my_bank < sql/branch_details.sql + mysql -u root -p my_bank < sql/teller_details.sql + mysql -u root -p my_bank < sql/customer_details.sql ``` -1. Create tables in the database: + For each step you will be prompted for the root user's password. If there's no password set on the root use, just hit enter again. - The `sql` directory contains below listed ".sql" files: - - - `bankdetails.sql` - - `branch_details.sql` - - `teller_details.sql` - - `customer_details.sql` - - Copy the contents of each of the ".sql" and execute them separately on MySQL Server. This will create four tables in the database. - -1. Create `.env` file: +1. Create a `.env` file in this this directory: ```ini SERVER_ADDR=127.0.0.1:8080 - MYSQL_USER=XXX - MYSQL_PASSWORD=XXX + MYSQL_USER=root + MYSQL_PASSWORD= MYSQL_HOST=127.0.0.1 MYSQL_PORT=3306 MYSQL_DBNAME=my_bank ``` Update "MYSQL_USER" and "MYSQL_PASSWORD" values with the correct MySQL user/password. - If your password contains dollar sign "$", then remember to escape it eg "123$abc" will need to be changed to "123\\$abc" 1. Run the server: - ```shell + ```sh cargo run ``` -1. Using a different terminal send an HTTP GET/POST requests to the running server: +1. Using a different terminal send requests to the running server. For example, using [HTTPie]: - The `apis` directory contains below listed API's files: + ```sh + http POST :8080/bank bank_name="Bank ABC" country="Kenya" - - `bank.txt` - - `branch.txt` - - `teller.txt` - - `customer.txt` + http :8080/bank + ``` - Copy the curl request on each of the ".txt" and execute them on separate terminals. Each ".txt" contains curl request and expected JSON response data. + See [the API documentation pages](./apis/) for more info. + +[HTTPie]: https://httpie.io/cli diff --git a/databases/mysql/apis/bank.md b/databases/mysql/apis/bank.md new file mode 100644 index 0000000..a6cb960 --- /dev/null +++ b/databases/mysql/apis/bank.md @@ -0,0 +1,34 @@ +# Banks API + +All examples show cURL and [HTTPie](https://httpie.io/cli) snippets. + +## Adding A Bank + +```sh +curl -d '{"bank_name":"Bank ABC","country":"Kenya"}' -H 'Content-Type: application/json' http://localhost:8080/bank + +http POST :8080/bank bank_name="Bank ABC" country="Kenya" +``` + +You should expect a 204 No Content response. + +## Listing Banks + +```sh +curl http://localhost:8080/bank + +http :8080/bank +``` + +The response should be a 200 OK with the following JSON body: + +```json +{ + "bank_data": [ + { + "bank_name": "bank abc", + "country": "kenya" + } + ] +} +``` diff --git a/databases/mysql/apis/bank.txt b/databases/mysql/apis/bank.txt deleted file mode 100644 index da956c0..0000000 --- a/databases/mysql/apis/bank.txt +++ /dev/null @@ -1,41 +0,0 @@ -1. -Linux - -curl -d '{"bank_name":"Bank ABC","country":"Kenya"}' -H 'Content-Type: application/json' http://127.0.0.1:8080/bank - -2. -Windows - -curl -H "Content-type:application/json" --data-binary "{\"bank_name\":\"Bank ABC\",\"country\":\"Kenya\"}" http://127.0.0.1:8080/bank - -3. -JSON response - -{ - "status_code": 0, - "status_description": "Successful" -} - -4. -Linux - -curl 'http://localhost:8080/bank' - -5. -Windows - -curl http://localhost:8080/bank - -6. -JSON response - -{ - "status_code": 0, - "status_description": "Successful", - "bank_data": [ - { - "bank_name": "bank abc", - "country": "kenya" - } - ] -} diff --git a/databases/mysql/apis/branch.md b/databases/mysql/apis/branch.md new file mode 100644 index 0000000..d00adfa --- /dev/null +++ b/databases/mysql/apis/branch.md @@ -0,0 +1,34 @@ +# Branches API + +All examples show cURL and [HTTPie](https://httpie.io/cli) snippets. + +## Adding A Branch + +```sh +curl -d '{"branch_name":"HQ branch", "location":"Central Business District"}' -H 'Content-Type: application/json' http://localhost:8080/branch + +http POST :8080/branch branch_name="HQ branch" branch_name="Central Business District" +``` + +You should expect a 204 No Content response. + +## Listing Branches + +```sh +curl http://localhost:8080/branch + +http :8080/branch +``` + +The response should be a 200 OK with the following JSON body: + +```json +{ + "branch_data": [ + { + "branch_name": "hq branch", + "location": "central business district" + } + ] +} +``` diff --git a/databases/mysql/apis/branch.txt b/databases/mysql/apis/branch.txt deleted file mode 100644 index 01a4479..0000000 --- a/databases/mysql/apis/branch.txt +++ /dev/null @@ -1,42 +0,0 @@ -1. -Linux - -curl -d '{"branch_name":"HQ branch", "location":"Central Business District"}' -H 'Content-Type: application/json' http://127.0.0.1:8080/branch - -2. -Windows - -curl -H "Content-type:application/json" --data-binary "{\"branch_name\":\"HQ branch\", \"location\":\"Central Business District\"}" http://127.0.0.1:8080/branch - - -3. -JSON response - -{ - "status_code": 0, - "status_description": "Successful" -} - -4. -Linux - -curl 'http://localhost:8080/branch' - -5. -Windows - -curl http://localhost:8080/branch - -6. -JSON response - -{ - "status_code": 0, - "status_description": "Successful", - "branch_data": [ - { - "branch_name": "hq branch", - "location": "central business district" - } - ] -} diff --git a/databases/mysql/apis/customer.md b/databases/mysql/apis/customer.md new file mode 100644 index 0000000..5f09dd2 --- /dev/null +++ b/databases/mysql/apis/customer.md @@ -0,0 +1,34 @@ +# Customers API + +All examples show cURL and [HTTPie](https://httpie.io/cli) snippets. + +## Adding A Customer + +```sh +curl -d '{"customer_name":"Peter Paul", "branch_name":"Central Business District"}' -H 'Content-Type: application/json' http://localhost:8080/customer + +http POST :8080/customer customer_name="Peter Paul" branch_name="Central Business District" +``` + +You should expect a 204 No Content response. + +## Listing Customers + +```sh +curl http://localhost:8080/customer + +http :8080/customer +``` + +The response should be a 200 OK with the following JSON body: + +```json +{ + "customer_data": [ + { + "customer_name": "peter paul", + "branch_name": "central business district" + } + ] +} +``` diff --git a/databases/mysql/apis/customer.txt b/databases/mysql/apis/customer.txt deleted file mode 100644 index a086927..0000000 --- a/databases/mysql/apis/customer.txt +++ /dev/null @@ -1,42 +0,0 @@ -1. -Linux - -curl -d '{"customer_name":"Peter Paul", "branch_name":"Central Business District"}' -H 'Content-Type: application/json' http://127.0.0.1:8080/addcustomer - -2. -Windows - -curl -H "Content-type:application/json" --data-binary "{\"customer_name\":\"Peter Paul\", \"branch_name\":\"Central Business District\"}" http://127.0.0.1:8080/addcustomer - - -3. -JSON response - -{ - "status_code": 0, - "status_description": "Successful" -} - -4. -Linux - -curl 'http://localhost:8080/customer' - -5. -Windows - -curl http://localhost:8080/customer - -6. -JSON response - -{ - "status_code": 0, - "status_description": "Successful", - "customer_data": [ - { - "customer_name": "peter paul", - "branch_name": "central business district" - } - ] -} diff --git a/databases/mysql/apis/teller.md b/databases/mysql/apis/teller.md new file mode 100644 index 0000000..0e7c684 --- /dev/null +++ b/databases/mysql/apis/teller.md @@ -0,0 +1,34 @@ +# Tellers API + +All examples show cURL and [HTTPie](https://httpie.io/cli) snippets. + +## Adding A Teller + +```sh +curl -d '{"teller_name":"John Doe", "branch_name":"Central Business District"}' -H 'Content-Type: application/json' http://localhost:8080/teller + +http POST :8080/teller teller_name="John Doe" branch_name="Central Business District" +``` + +You should expect a 204 No Content response. + +## Listing Tellers + +```sh +curl http://localhost:8080/teller + +http :8080/teller +``` + +The response should be a 200 OK with the following JSON body: + +```json +{ + "teller_data": [ + { + "teller_name": "john doe", + "branch_name": "central business district" + } + ] +} +``` diff --git a/databases/mysql/apis/teller.txt b/databases/mysql/apis/teller.txt deleted file mode 100644 index 02310b8..0000000 --- a/databases/mysql/apis/teller.txt +++ /dev/null @@ -1,42 +0,0 @@ -1. -Linux - -curl -d '{"teller_name":"John Doe", "branch_name":"Central Business District"}' -H 'Content-Type: application/json' http://127.0.0.1:8080/teller - -2. -Windows - -curl -H "Content-type:application/json" --data-binary "{\"teller_name\":\"John Doe\", \"branch_name\":\"Central Business District\"}" http://127.0.0.1:8080/teller - - -3. -JSON response - -{ - "status_code": 0, - "status_description": "Successful" -} - -4. -Linux - -curl 'http://localhost:8080/teller' - -5. -Windows - -curl http://localhost:8080/teller - -6. -JSON response - -{ - "status_code": 0, - "status_description": "Successful", - "teller_data": [ - { - "teller_name": "john doe", - "branch_name": "central business district" - } - ] -} diff --git a/databases/mysql/sql/bankdetails.sql b/databases/mysql/sql/bank_details.sql similarity index 100% rename from databases/mysql/sql/bankdetails.sql rename to databases/mysql/sql/bank_details.sql diff --git a/databases/mysql/sql/create_database.sql b/databases/mysql/sql/create_database.sql new file mode 100644 index 0000000..b85eb0b --- /dev/null +++ b/databases/mysql/sql/create_database.sql @@ -0,0 +1 @@ +CREATE DATABASE my_bank CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; diff --git a/databases/mysql/src/main.rs b/databases/mysql/src/main.rs index f1ccf03..6210582 100644 --- a/databases/mysql/src/main.rs +++ b/databases/mysql/src/main.rs @@ -62,6 +62,7 @@ async fn main() -> io::Result<()> { .service(routes::get_customer) }) .bind(("127.0.0.1", 8080))? + .workers(2) .run() .await }