mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
format markdown
This commit is contained in:
@ -48,30 +48,35 @@ cargo run
|
||||
Inserts a new user into the SQLite DB.
|
||||
|
||||
Provide a JSON payload with a name. Eg:
|
||||
|
||||
```json
|
||||
{ "name": "bill" }
|
||||
```
|
||||
|
||||
On success, a response like the following is returned:
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "9e46baba-a001-4bb3-b4cf-4b3e5bab5e97",
|
||||
"name": "bill"
|
||||
"id": "9e46baba-a001-4bb3-b4cf-4b3e5bab5e97",
|
||||
"name": "bill"
|
||||
}
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Client Examples</summary>
|
||||
|
||||
Using [HTTPie](https://httpie.org/):
|
||||
```sh
|
||||
http POST localhost:8080/user name=bill
|
||||
```
|
||||
Using [HTTPie](https://httpie.org/):
|
||||
|
||||
```sh
|
||||
http POST localhost:8080/user name=bill
|
||||
```
|
||||
|
||||
Using cURL:
|
||||
|
||||
```sh
|
||||
curl -S -X POST --header "Content-Type: application/json" --data '{"name":"bill"}' http://localhost:8080/user
|
||||
```
|
||||
|
||||
Using cURL:
|
||||
```sh
|
||||
curl -S -X POST --header "Content-Type: application/json" --data '{"name":"bill"}' http://localhost:8080/user
|
||||
```
|
||||
</details>
|
||||
|
||||
#### `GET /user/{user_uid}`
|
||||
@ -81,15 +86,18 @@ Gets a user from the DB using its UID (returned from the insert request or taken
|
||||
<details>
|
||||
<summary>Client Examples</summary>
|
||||
|
||||
Using [HTTPie](https://httpie.org/):
|
||||
```sh
|
||||
http localhost:8080/user/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97
|
||||
```
|
||||
Using [HTTPie](https://httpie.org/):
|
||||
|
||||
```sh
|
||||
http localhost:8080/user/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97
|
||||
```
|
||||
|
||||
Using cURL:
|
||||
|
||||
```sh
|
||||
curl -S http://localhost:8080/user/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97
|
||||
```
|
||||
|
||||
Using cURL:
|
||||
```sh
|
||||
curl -S http://localhost:8080/user/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97
|
||||
```
|
||||
</details>
|
||||
|
||||
### Explore The SQLite DB
|
||||
@ -103,7 +111,6 @@ sqlite> .tables
|
||||
sqlite> SELECT * FROM users;
|
||||
```
|
||||
|
||||
|
||||
## Using Other Databases
|
||||
|
||||
You can find a complete example of Diesel + PostgreSQL at: [https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Rust/actix](https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Rust/actix)
|
||||
|
@ -1,23 +1,16 @@
|
||||
# MongoDB
|
||||
|
||||
Simple example of MongoDB usage with Actix Web. For more information on the MongoDB Rust driver,
|
||||
visit the [documentation](https://docs.rs/mongodb/2.0.0/mongodb/index.html) and
|
||||
[source code](https://github.com/mongodb/mongo-rust-driver).
|
||||
Simple example of MongoDB usage with Actix Web. For more information on the MongoDB Rust driver, visit the [documentation](https://docs.rs/mongodb/2.0.0/mongodb/index.html) and [source code](https://github.com/mongodb/mongo-rust-driver).
|
||||
|
||||
## Usage
|
||||
|
||||
### Install MongoDB
|
||||
|
||||
Visit the [MongoDB Download Center](https://www.mongodb.com/try) for instructions on how to use
|
||||
MongoDB Atlas or set up MongoDB locally.
|
||||
Visit the [MongoDB Download Center](https://www.mongodb.com/try) for instructions on how to use MongoDB Atlas or set up MongoDB locally.
|
||||
|
||||
### Set an environment variable
|
||||
|
||||
The example code creates a client with the URI set by the `MONGODB_URI` environment variable. The
|
||||
default URI for a standalone `mongod` running on localhost is "mongodb://localhost:27017". For more
|
||||
information on MongoDB URIs, visit the
|
||||
[connection string](https://docs.mongodb.com/manual/reference/connection-string/) entry in the
|
||||
MongoDB manual.
|
||||
The example code creates a client with the URI set by the `MONGODB_URI` environment variable. The default URI for a standalone `mongod` running on localhost is "mongodb://localhost:27017". For more information on MongoDB URIs, visit the [connection string](https://docs.mongodb.com/manual/reference/connection-string/) entry in the MongoDB manual.
|
||||
|
||||
### Run the example
|
||||
|
||||
|
@ -11,62 +11,56 @@
|
||||
|
||||
1. Create database user
|
||||
|
||||
```shell
|
||||
createuser -P test_user
|
||||
```
|
||||
```shell
|
||||
createuser -P test_user
|
||||
```
|
||||
|
||||
Enter a password of your choice. The following instructions assume you
|
||||
used `testing` as password.
|
||||
Enter a password of your choice. The following instructions assume you used `testing` as password.
|
||||
|
||||
This step is **optional** and you can also use an existing database user
|
||||
for that. Just make sure to replace `test_user` by the database user
|
||||
of your choice in the following steps and change the `.env` file
|
||||
containing the configuration accordingly.
|
||||
This step is **optional** and you can also use an existing database user for that. Just make sure to replace `test_user` by the database user of your choice in the following steps and change the `.env` file containing the configuration accordingly.
|
||||
|
||||
2. Create database
|
||||
|
||||
```shell
|
||||
createdb -O test_user testing_db
|
||||
```
|
||||
```shell
|
||||
createdb -O test_user testing_db
|
||||
```
|
||||
|
||||
3. Initialize database
|
||||
|
||||
```shell
|
||||
psql -f sql/schema.sql testing_db
|
||||
```
|
||||
```shell
|
||||
psql -f sql/schema.sql testing_db
|
||||
```
|
||||
|
||||
This step can be repeated and clears the database as it drops and
|
||||
recreates the schema `testing` which is used within the database.
|
||||
This step can be repeated and clears the database as it drops and recreates the schema `testing` which is used within the database.
|
||||
|
||||
4. Create `.env` file:
|
||||
|
||||
```ini
|
||||
SERVER_ADDR=127.0.0.1:8080
|
||||
PG.USER=test_user
|
||||
PG.PASSWORD=testing
|
||||
PG.HOST=127.0.0.1
|
||||
PG.PORT=5432
|
||||
PG.DBNAME=testing_db
|
||||
PG.POOL.MAX_SIZE=16
|
||||
```
|
||||
```ini
|
||||
SERVER_ADDR=127.0.0.1:8080
|
||||
PG.USER=test_user
|
||||
PG.PASSWORD=testing
|
||||
PG.HOST=127.0.0.1
|
||||
PG.PORT=5432
|
||||
PG.DBNAME=testing_db
|
||||
PG.POOL.MAX_SIZE=16
|
||||
```
|
||||
|
||||
5. Run the server:
|
||||
|
||||
```shell
|
||||
cargo run
|
||||
```
|
||||
```shell
|
||||
cargo run
|
||||
```
|
||||
|
||||
6. Using a different terminal send an HTTP POST request to the running server:
|
||||
|
||||
```shell
|
||||
echo '{"email": "ferris@thecrab.com", "first_name": "ferris", "last_name": "crab", "username": "ferreal"}' | http -f --json --print h POST http://127.0.0.1:8080/users
|
||||
```
|
||||
```shell
|
||||
echo '{"email": "ferris@thecrab.com", "first_name": "ferris", "last_name": "crab", "username": "ferreal"}' | http -f --json --print h POST http://127.0.0.1:8080/users
|
||||
```
|
||||
|
||||
**...or using curl...**
|
||||
**...or using curl...**
|
||||
|
||||
```shell
|
||||
curl -d '{"email": "ferris@thecrab.com", "first_name": "ferris", "last_name": "crab", "username": "ferreal"}' -H 'Content-Type: application/json' http://127.0.0.1:8080/users
|
||||
```
|
||||
```shell
|
||||
curl -d '{"email": "ferris@thecrab.com", "first_name": "ferris", "last_name": "crab", "username": "ferreal"}' -H 'Content-Type: application/json' http://127.0.0.1:8080/users
|
||||
```
|
||||
|
||||
A unique constraint exists for username, so sending this request twice
|
||||
will return an internal server error (HTTP 500).
|
||||
A unique constraint exists for username, so sending this request twice will return an internal server error (HTTP 500).
|
||||
|
@ -61,5 +61,5 @@ At any time, verify the contents of Redis using its CLI:
|
||||
echo "MGET mydomain:one mydomain:two mydomain:three" | redis-cli
|
||||
```
|
||||
|
||||
[HTTPie]: https://httpie.org
|
||||
[cURL]: https://curl.haxx.se
|
||||
[httpie]: https://httpie.org
|
||||
[curl]: https://curl.haxx.se
|
||||
|
@ -5,13 +5,13 @@ Getting started using databases with Actix Web, asynchronously.
|
||||
### init database sqlite
|
||||
|
||||
From the root directory of this project:
|
||||
|
||||
```sh
|
||||
bash db/setup_db.sh
|
||||
```
|
||||
|
||||
This creates a sqlite database, weather.db, in the root.
|
||||
|
||||
|
||||
### server
|
||||
|
||||
```sh
|
||||
@ -26,7 +26,6 @@ cargo run
|
||||
|
||||
[http://127.0.0.1:8080/parallel_weather](http://127.0.0.1:8080/parallel_weather)
|
||||
|
||||
|
||||
### sqlite client
|
||||
|
||||
```sh
|
||||
|
Reference in New Issue
Block a user