-
-
Notifications
You must be signed in to change notification settings - Fork 807
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
168 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
|
||
The `sql` directory contains below listed ".sql" files: | ||
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. | ||
|
||
- `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=<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" | ||
|
||
http :8080/bank | ||
``` | ||
|
||
- `bank.txt` | ||
- `branch.txt` | ||
- `teller.txt` | ||
- `customer.txt` | ||
See [the API documentation pages](./apis/) for more info. | ||
|
||
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. | ||
[HTTPie]: https://httpie.io/cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} | ||
``` |
Oops, something went wrong.