1
0
mirror of https://github.com/actix/examples synced 2025-06-27 01:27:43 +02:00

improve mysql readme

This commit is contained in:
Rob Ede
2023-07-18 01:01:26 +01:00
parent f3cf37bb0d
commit 048f4fd884
13 changed files with 168 additions and 216 deletions

View File

@ -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