mirror of
https://github.com/actix/examples
synced 2024-11-23 14:31:07 +01:00
improve mysql readme
This commit is contained in:
parent
f3cf37bb0d
commit
048f4fd884
@ -26,6 +26,7 @@ async fn main() -> io::Result<()> {
|
||||
.service(user::info)
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.workers(2)
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -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=<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
|
||||
|
34
databases/mysql/apis/bank.md
Normal file
34
databases/mysql/apis/bank.md
Normal file
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
34
databases/mysql/apis/branch.md
Normal file
34
databases/mysql/apis/branch.md
Normal file
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
34
databases/mysql/apis/customer.md
Normal file
34
databases/mysql/apis/customer.md
Normal file
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
34
databases/mysql/apis/teller.md
Normal file
34
databases/mysql/apis/teller.md
Normal file
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
1
databases/mysql/sql/create_database.sql
Normal file
1
databases/mysql/sql/create_database.sql
Normal file
@ -0,0 +1 @@
|
||||
CREATE DATABASE my_bank CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
|
@ -62,6 +62,7 @@ async fn main() -> io::Result<()> {
|
||||
.service(routes::get_customer)
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.workers(2)
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user