1
0
mirror of https://github.com/actix/examples synced 2024-11-23 14:31:07 +01:00

format markdown

This commit is contained in:
Rob Ede 2022-03-06 00:43:10 +00:00
parent f27cc4b6b4
commit e239414a55
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
31 changed files with 165 additions and 188 deletions

View File

@ -44,6 +44,7 @@
- [Mozilla Services Skeleton App](https://github.com/mozilla-services/skeleton)
## Paid Resources
- [book] [Zero2prod by Luca Palmieri](https://algoluca.gumroad.com/l/zero2prod): Takes you on a journey to discover the world of backend development in Rust.
## Contribute

View File

@ -33,7 +33,6 @@
- [sparkpost](https://crates.io/crates/sparkpost) // Rust bindings for sparkpost email api v1.
- [uuid](https://crates.io/crates/uuid) // A library to generate and parse UUIDs.
Read the full tutorial series on [gill.net.in](https://gill.net.in)
- [Auth Web Microservice with Rust using Actix Web v2 - Complete Tutorial](https://gill.net.in/posts/auth-microservice-rust-actix-web1.0-diesel-complete-tutorial/)

View File

@ -2,9 +2,8 @@
Demonstrates how to serve static files. Inside the `./static` folder you will find 2 subfolders:
* `root`: A tree of files that will be served at the web root `/`. This includes the `css` and `js` folders, each
containing an example file.
* `images`: A list of images that will be served at `/images` path, with file listing enabled.
- `root`: A tree of files that will be served at the web root `/`. This includes the `css` and `js` folders, each containing an example file.
- `images`: A list of images that will be served at `/images` path, with file listing enabled.
## Usage

View File

@ -1,16 +1,20 @@
# Actix Web CORS example
# Cross-Origin Resource Sharing (CORS)
## Run Server
```sh
cd cors/backend
cargo run
```
## Run Frontend
In a separate terminal, also run:
```sh
cd cors/frontend
npm install
npm run serve
```
then open browser at: http://localhost:8080

View File

@ -1,23 +1,27 @@
# Usage:
This is an example demonstrating the construction of async state with `App::data_factory`
## Reason:
Use of a `data_factory` would make sense in these situations:
- When async state does not necessarily have to be shared between workers/threads.
- When an async state would spawn tasks on `actix-rt`. If state was centralized there could be a possibility the tasks get an unbalanced distribution on the workers/threads
(`actix-rt` would spawn tasks on local thread whenever it's called)
- When an async state would spawn tasks on `actix-rt`. If state was centralized there could be a possibility the tasks get an unbalanced distribution on the workers/threads (`actix-rt` would spawn tasks on local thread whenever it's called)
## Requirement:
- `rustc 1.58 stable`
- `redis` server listen on `127.0.0.1:6379`(or use `REDIS_URL` env argument when starting the example)
## Endpoints:
- use a work load generator(e.g wrk) to benchmark the end points:
http://127.0.0.1:8080/pool prebuilt shared redis pool
http://127.0.0.1:8080/pool2 data_factory redis pool
## Context:
The real world difference can be vary by the work you are doing but in general it's a good idea to
spread your *identical* async tasks evenly between threads and have as little cross threads synchronization as possible.
The real world difference can be vary by the work you are doing but in general it's a good idea to spread your _identical_ async tasks evenly between threads and have as little cross threads synchronization as possible.

View File

@ -48,11 +48,13 @@ 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",
@ -63,15 +65,18 @@ On success, a response like the following is returned:
<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)

View File

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

View File

@ -15,13 +15,9 @@
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
@ -35,8 +31,7 @@
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:
@ -68,5 +63,4 @@
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).

View File

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

View File

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

View File

@ -7,10 +7,10 @@ cd forms/multipart
cargo run
```
``` open web browser to localhost:8080 and upload file(s) ```
`open web browser to localhost:8080 and upload file(s)`
### Result
``` file(s) will show up in ./tmp in the same directory as the running process ```
`file(s) will show up in ./tmp in the same directory as the running process`
Note: this is a naive implementation and will panic on any error

View File

@ -1,7 +1,6 @@
# GraphQL using Juniper
[Juniper](https://github.com/graphql-rust/juniper) integration for Actix Web.
If you want more advanced example, see also the [juniper-advanced example].
[Juniper](https://github.com/graphql-rust/juniper) integration for Actix Web. If you want more advanced example, see also the [juniper-advanced example].
[juniper-advanced example]: https://github.com/actix/examples/tree/master/graphql/juniper-advanced
@ -37,9 +36,7 @@ _Result:_
"data": {
"human": {
"name": "Luke",
"appearsIn": [
"NEW_HOPE"
],
"appearsIn": ["NEW_HOPE"],
"homePlanet": "Mars"
}
}
@ -50,7 +47,9 @@ _Mutation example:_
```graphql
mutation {
createHuman(newHuman: {name: "Fresh Kid Ice", appearsIn: EMPIRE, homePlanet: "earth"}) {
createHuman(
newHuman: { name: "Fresh Kid Ice", appearsIn: EMPIRE, homePlanet: "earth" }
) {
id
name
appearsIn
@ -67,9 +66,7 @@ _Result:_
"createHuman": {
"id": "1234",
"name": "Fresh Kid Ice",
"appearsIn": [
"EMPIRE"
],
"appearsIn": ["EMPIRE"],
"homePlanet": "earth"
}
}

View File

@ -1,6 +1,7 @@
# guards
Shows how to set up custom routing guards.
- Routing different API versions using a header instead of path.
## Usage
@ -30,5 +31,5 @@ Using [cURL]:
curl 'localhost:8080/api/hello' -H 'accept-version: 1'
```
[HTTPie]: https://httpie.org
[cURL]: https://curl.haxx.se
[httpie]: https://httpie.org
[curl]: https://curl.haxx.se

View File

@ -1,11 +1,10 @@
## HTTP naïve proxy example
This is a relatively simple HTTP proxy, forwarding HTTP requests to another HTTP server, including
request body, headers, and streaming uploads.
This is a relatively simple HTTP proxy, forwarding HTTP requests to another HTTP server, including request body, headers, and streaming uploads.
To start:
``` shell
```shell
cargo run -- <listen addr> <listen port> <forward addr> <forward port>
# example:
cargo run -- 127.0.0.1 3333 127.0.0.1 8080

View File

@ -4,9 +4,7 @@
### Certificate
We put the self-signed certificate in this directory as an example
but your browser would complain that it isn't secure.
So we recommend to use [`mkcert`] to trust it. To use local CA, you should run:
We put the self-signed certificate in this directory as an example but your browser would complain that it isn't secure. So we recommend to use [`mkcert`] to trust it. To use local CA, you should run:
```sh
mkcert -install
@ -30,5 +28,5 @@ cargo run (or ``cargo watch -x run``)
### web client
- curl: ``curl -v https://127.0.0.1:8443/index.html --compressed -k``
- curl: `curl -v https://127.0.0.1:8443/index.html --compressed -k`
- browser: [https://127.0.0.1:8443/index.html](https://127.0.0.1:8443/index.html)

View File

@ -4,8 +4,7 @@
### Certificate
All the self-signed certificate are in the ./certs directory, including the CA certificate
generated by [`mkcert`] that was used to create the server and client certs.
All the self-signed certificate are in the ./certs directory, including the CA certificate generated by [`mkcert`] that was used to create the server and client certs.
### Server
@ -19,15 +18,17 @@ The server runs HTTP on port 8080 and HTTPS on port 8443.
### Providing Client Cert
Using [HTTPie]:
```sh
http https://127.0.0.1:8443/ --verify=certs/rootCA.pem --cert=certs/client-cert.pem --cert-key=certs/client-key.pem
```
Using [cURL]:
```sh
curl https://127.0.0.1:8443/ --cacert certs/rootCA.pem --cert certs/client-cert.pem --key certs/client-key.pem
```
[`mkcert`]: https://github.com/FiloSottile/mkcert
[cURL]: https://curl.haxx.se/
[HTTPie]: https://httpie.org/
[curl]: https://curl.haxx.se/
[httpie]: https://httpie.org/

View File

@ -4,9 +4,7 @@
### Certificate
We put the self-signed certificate in this directory as an example
but your browser would complain that it isn't secure.
So we recommend to use [`mkcert`] to trust it. To use local CA, you should run:
We put the self-signed certificate in this directory as an example but your browser would complain that it isn't secure. So we recommend to use [`mkcert`] to trust it. To use local CA, you should run:
```sh
mkcert -install
@ -19,6 +17,7 @@ mkcert 127.0.0.1 localhost
```
For `rsa` keys use `rsa_private_keys` function instead `pkcs8_private_keys`
```rs
let mut keys = pkcs8_private_keys(key_file).unwrap(); // pkcs8
let mut keys = rsa_private_keys(key_file).unwrap(); // rsa
@ -34,10 +33,9 @@ cargo run # (or ``cargo watch -x run``)
# Started http server: 127.0.0.1:8443
```
If you prefer reloading you can substitute `cargo watch -x run`.
That requires you install the `cargo-watch` crate.
If you prefer reloading you can substitute `cargo watch -x run`. That requires you install the `cargo-watch` crate.
### web client
- curl: ``curl -v https://127.0.0.1:8443/index.html --compressed -k``
- curl: `curl -v https://127.0.0.1:8443/index.html --compressed -k`
- browser: [https://127.0.0.1:8443/index.html](https://127.0.0.1:8443/index.html)

View File

@ -1,10 +1,6 @@
# JSON decode errors
This example demonstrates how to return useful error messages to the client
when the server receives a request with invalid JSON, or which cannot be
deserialized to the expected model. By configuring an `error_handler` on the
route, we can set appropriate response codes and return the string
representation of the error.
This example demonstrates how to return useful error messages to the client when the server receives a request with invalid JSON, or which cannot be deserialized to the expected model. By configuring an `error_handler` on the route, we can set appropriate response codes and return the string representation of the error.
## Usage
@ -16,9 +12,7 @@ cargo run
## Examples
The examples use `curl -i` in order to show the status line with the response
code. The response headers have been omitted for brevity, and replaced with an
ellipsis `...`.
The examples use `curl -i` in order to show the status line with the response code. The response headers have been omitted for brevity, and replaced with an ellipsis `...`.
- A well-formed request

View File

@ -1,10 +1,6 @@
This is a contrived example intended to illustrate a few important Actix Web features.
*Imagine* that you have a process that involves 3 steps. The steps here
are dumb in that they do nothing other than call an HTTP endpoint that
returns the json that was posted to it. The intent here is to illustrate
how to chain these steps together as futures and return a final result
in a response.
_Imagine_ that you have a process that involves 3 steps. The steps here are dumb in that they do nothing other than call an HTTP endpoint that returns the json that was posted to it. The intent here is to illustrate how to chain these steps together as futures and return a final result in a response.
Actix Web features illustrated here include:
@ -14,7 +10,6 @@ Actix Web features illustrated here include:
- POSTing json body
3. chaining futures into a single response used by an asynch endpoint
### server
```sh
@ -23,5 +18,4 @@ cargo run
# Started http server: 127.0.0.1:8080
```
Example query from the command line using httpie:
```echo '{"id":"1", "name": "JohnDoe"}' | http 127.0.0.1:8080/something```
Example query from the command line using httpie: `echo '{"id":"1", "name": "JohnDoe"}' | http 127.0.0.1:8080/something`

View File

@ -18,31 +18,31 @@ With [Postman](https://www.getpostman.com/) or [Rested](moz-extension://60daeb1c
- POST / (embed serde-json):
- method : ``POST``
- url : ``http://127.0.0.1:8080/``
- header : ``Content-Type`` = ``application/json``
- body (raw) : ``{"name": "Test user", "number": 100}``
- method : `POST`
- url : `http://127.0.0.1:8080/`
- header : `Content-Type` = `application/json`
- body (raw) : `{"name": "Test user", "number": 100}`
- POST /manual (manual serde-json):
- method : ``POST``
- url : ``http://127.0.0.1:8080/manual``
- header : ``Content-Type`` = ``application/json``
- body (raw) : ``{"name": "Test user", "number": 100}``
- method : `POST`
- url : `http://127.0.0.1:8080/manual`
- header : `Content-Type` = `application/json`
- body (raw) : `{"name": "Test user", "number": 100}`
- POST /mjsonrust (manual json-rust):
- method : ``POST``
- url : ``http://127.0.0.1:8080/mjsonrust``
- header : ``Content-Type`` = ``application/json``
- body (raw) : ``{"name": "Test user", "number": 100}`` (you can also test ``{notjson}``)
- method : `POST`
- url : `http://127.0.0.1:8080/mjsonrust`
- header : `Content-Type` = `application/json`
- body (raw) : `{"name": "Test user", "number": 100}` (you can also test `{notjson}`)
### python client
- ``pip install aiohttp``
- ``python client.py``
- `pip install aiohttp`
- `python client.py`
if ubuntu :
- ``pip3 install aiohttp``
- ``python3 client.py``
- `pip3 install aiohttp`
- `python3 client.py`

View File

@ -17,7 +17,6 @@ $ curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "metho
# {"jsonrpc":"2.0","result":"pong","error":null,"id":1}
```
**python**
```sh

View File

@ -28,8 +28,7 @@ A middleware demonstrating how to read out the outgoing response body.
### `simple::SayHi`
A minimal middleware demonstrating the sequence of operations in an actix middleware.
There is a second version of the same middleware using `wrap_fn` which shows how easily a middleware can be implemented in actix.
A minimal middleware demonstrating the sequence of operations in an actix middleware. There is a second version of the same middleware using `wrap_fn` which shows how easily a middleware can be implemented in actix.
## See Also

View File

@ -1,4 +1,5 @@
# actix-sse
Example of server-sent events, aka `EventSource`, with actix web.
```sh
@ -12,9 +13,10 @@ Open http://127.0.0.1:8080/ with a browser, then send events with another HTTP c
curl 127.0.0.1:8080/broadcast/my_message
```
*my_message* should appear in the browser with a timestamp.
_my_message_ should appear in the browser with a timestamp.
## Performance
This implementation can serve thousands of clients on a 2021 MacBook with no problems.
Run `node ./benchmark.js` to benchmark your own system:
@ -24,7 +26,8 @@ $ node benchmark.js
Connected: 1000, connection time: 201 ms, total broadcast time: 20 ms^C⏎
```
### Error *Too many open files*
### Error _Too many open files_
You may be limited to a maximal number of connections (open file descriptors). Setting maximum number of open file descriptors to 2048:
```sh

View File

@ -7,4 +7,5 @@ cd templating/yarte
cargo test
cargo run
```
> open `localhost:8080`

View File

@ -9,9 +9,6 @@ curl --unix-socket /tmp/actix-uds.socket http://localhost/
Hello world!
```
Although this will only one thread for handling incoming connections
according to the [documentation](https://actix.github.io/actix-web/actix_web/struct.HttpServer.html#method.bind_uds).
Although this will only one thread for handling incoming connections according to the [documentation](https://actix.github.io/actix-web/actix_web/struct.HttpServer.html#method.bind_uds).
And it does not delete the socket file (`/tmp/actix-uds.socket`) when stopping
the server, so it will fail to start next time you run it unless you delete
the socket file manually.
And it does not delete the socket file (`/tmp/actix-uds.socket`) when stopping the server, so it will fail to start next time you run it unless you delete the socket file manually.

View File

@ -1,26 +1,25 @@
# Websocket chat broker example
This is a different implementation of the
[websocket chat example](https://github.com/actix/examples/tree/master/websockets/chat)
This is a different implementation of the [websocket chat example](https://github.com/actix/examples/tree/master/websockets/chat)
Differences:
* Chat Server Actor is a System Service that runs in the same thread as the HttpServer/WS Listener.
* The [actix-broker](https://github.com/Chris-Ricketts/actix-broker) crate is used to facilitate the sending of some messages between the Chat Session and Server Actors where the session does not require a response.
* The Client is not required to send Ping messages. The Chat Server Actor auto-clears dead sessions.
- Chat Server Actor is a System Service that runs in the same thread as the HttpServer/WS Listener.
- The [actix-broker](https://github.com/Chris-Ricketts/actix-broker) crate is used to facilitate the sending of some messages between the Chat Session and Server Actors where the session does not require a response.
- The Client is not required to send Ping messages. The Chat Server Actor auto-clears dead sessions.
Possible Improvements:
* Could the Chat Server Actor be simultaneously a System Service (accessible to the Chat Session via the System Registry) and also run in a separate thread?
- Could the Chat Server Actor be simultaneously a System Service (accessible to the Chat Session via the System Registry) and also run in a separate thread?
## Server
Chat server listens for incoming tcp connections. Server can access several types of message:
* `/list` - list all available rooms
* `/join name` - join room, if room does not exist, create new one
* `/name name` - set session name
* `some message` - just string, send message to all peers in same room
- `/list` - list all available rooms
- `/join name` - join room, if room does not exist, create new one
- `/name name` - set session name
- `some message` - just string, send message to all peers in same room
To start server use command: `cargo run`

View File

@ -1,7 +1,6 @@
# Websocket chat example
This is extension of the
[actix chat example](https://github.com/actix/actix/tree/master/examples/chat)
This is extension of the [actix chat example](https://github.com/actix/actix/tree/master/examples/chat)
Added features:

View File

@ -1,27 +1,25 @@
# Websocket chat example
This is extension of the
[actix chat example](https://github.com/actix/actix/tree/master/examples/chat)
This is extension of the [actix chat example](https://github.com/actix/actix/tree/master/examples/chat)
Added features:
* Browser WebSocket client
* Chat server runs in separate thread
* Tcp listener runs in separate thread
* Application state is shared with the websocket server and a resource at `/count/`
- Browser WebSocket client
- Chat server runs in separate thread
- Tcp listener runs in separate thread
- Application state is shared with the websocket server and a resource at `/count/`
## Server
1. Chat server listens for incoming tcp connections. Server can access several types of message:
* `/list` - list all available rooms
* `/join name` - join room, if room does not exist, create new one
* `/name name` - set session name
* `some message` - just string, send message to all peers in same room
* client has to send heartbeat `Ping` messages, if server does not receive a heartbeat message for 10 seconds connection gets dropped
- `/list` - list all available rooms
- `/join name` - join room, if room does not exist, create new one
- `/name name` - set session name
- `some message` - just string, send message to all peers in same room
- client has to send heartbeat `Ping` messages, if server does not receive a heartbeat message for 10 seconds connection gets dropped
2. [http://localhost:8080/count/](http://localhost:8080/count/) is a
non-websocket endpoint and will affect and display state.
2. [http://localhost:8080/count/](http://localhost:8080/count/) is a non-websocket endpoint and will affect and display state.
To start server use command: `cargo run --bin websocket-chat-server`

View File

@ -25,10 +25,10 @@ cargo run --bin websocket-client
### python client
- ``pip install aiohttp``
- ``python websocket-client.py``
- `pip install aiohttp`
- `python websocket-client.py`
if ubuntu :
- ``pip3 install aiohttp``
- ``python3 websocket-client.py``
- `pip3 install aiohttp`
- `python3 websocket-client.py`