Skip to content

Commit

Permalink
Merge branch 'main' into shared-db-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaro00 committed Nov 12, 2024
2 parents 5c91d26 + 8b079d3 commit f4a6af4
Show file tree
Hide file tree
Showing 30 changed files with 640 additions and 115 deletions.
6 changes: 3 additions & 3 deletions docs/local-run.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ Here are some small tips and tricks to boost your productivity when developing l
To live reload your Shuttle app when you save a file, you can use [cargo-watch](https://github.com/watchexec/cargo-watch):

```bash
# This will e(x)ecute `shuttle run` when you save a file.
cargo watch -x 'shuttle run'
# This will execute `shuttle run` when you save a file.
cargo watch -s 'shuttle run'
# This will also (q)uietly (c)lear the console between runs.
cargo watch -qcx 'shuttle run'
cargo watch -qcs 'shuttle run'

# There are many other helpful options, see `cargo watch --help`
```
Expand Down
17 changes: 8 additions & 9 deletions examples/actix-cookie-authentication.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ description: "Explore how you can secure your Actix Web application by using coo
This example shows how to use authentication within actix-web with cookies, assisted by actix-identity and actix-session.
The idea is that all requests authenticate first at the login route to get a cookie, then the cookie is sent with all requests requiring authentication using the HTTP cookie header.

You can clone the example below by running the following (you'll need `cargo-shuttle` installed):
You can clone the example below by running the following (you'll need `shuttle` CLI installed):
```bash
shuttle init --from shuttle-hq/shuttle-examples \
--subfolder actix-web/cookie-authentication
shuttle init --from shuttle-hq/shuttle-examples --subfolder actix-web/cookie-authentication
```

Three Actix Web routes are registered in this file:
Expand All @@ -33,8 +32,8 @@ edition = "2021"
actix-identity = "0.6.0"
actix-session = { version = "0.8.0", features = ["cookie-session"] }
actix-web = "4.3.1"
shuttle-actix-web = "0.27.0"
shuttle-runtime = "0.27.0"
shuttle-actix-web = "0.48.0"
shuttle-runtime = "0.48.0"
tokio = "1.26.0"
```
Your `main.rs` should look like this:
Expand Down Expand Up @@ -127,25 +126,25 @@ Once you've cloned this example, launch it locally by using `shuttle run`. Once
First, we should be able to access the public endpoint without any authentication using:

```sh
$ curl http://localhost:8000/public
curl http://localhost:8000/public
```

But trying to access the private endpoint will return "Hello anonymous":

```sh
$ curl http://localhost:8000/private
curl http://localhost:8000/private
```

So let's get a cookie from the login route first:

```sh
$ curl http://localhost:8000/login
curl http://localhost:8000/login
```

Accessing the private endpoint with the token will now succeed:

```sh
$ curl --header "Authorization: Bearer <token>" http://localhost:8000/private
curl --header "Authorization: Bearer <token>" http://localhost:8000/private
```

The token is set to expire in 5 minutes, so wait a while and try to access the private endpoint again. Once the token has expired, a user will need to get a new token from login.
Expand Down
5 changes: 2 additions & 3 deletions examples/actix-postgres.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ The following routes are provided:
- GET `/todos/<id>` - Get a to-do item by ID.
- POST `/todos` - Create a to-do item. Takes "note" as a JSON body parameter.

You can clone the example below by running the following (you'll need `cargo-shuttle` installed):
You can clone the example below by running the following (you'll need `shuttle` CLI installed):
```bash
shuttle init --from shuttle-hq/shuttle-examples \
--subfolder actix-web/postgres
shuttle init --from shuttle-hq/shuttle-examples --subfolder actix-web/postgres
```

## Code
Expand Down
14 changes: 8 additions & 6 deletions examples/actix-static-files.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ This example has one route at `/` where the homepage is served and shows you how

Note that static assets are declared in the `Shuttle.toml` file.

You can clone the example below by running the following (you'll need `cargo-shuttle` installed):
You can clone the example below by running the following (you'll need `shuttle` CLI installed):
```bash
shuttle init --from shuttle-hq/shuttle-examples \
--subfolder actix-web/static-files
shuttle init --from shuttle-hq/shuttle-examples --subfolder actix-web/static-files
```

## Code
Expand Down Expand Up @@ -59,13 +58,16 @@ edition = "2021"
[dependencies]
actix-web = "4.3.1"
actix-files = "0.6.2"
shuttle-actix-web = "0.27.0"
shuttle-runtime = "0.27.0"
shuttle-actix-web = "0.48.0"
shuttle-runtime = "0.48.0"
tokio = "1.26.0"
```

```toml Shuttle.toml
assets = ["assets/*"]
[build]
assets = [
"assets/*",
]
```

</CodeGroup>
Expand Down
21 changes: 11 additions & 10 deletions examples/actix-websocket-actorless.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ description: "Learn how websockets can upgrade your web service by providing liv

This example shows how to use a WebSocket to show the live status of the Shuttle API on a web page. The app also provides an echo service and notifies when the number of connected users change.

You can clone the example below by running the following (you'll need `cargo-shuttle` installed):
You can clone the example below by running the following (you'll need `shuttle` CLI installed):
```bash
shuttle init --from shuttle-hq/shuttle-examples \
--subfolder actix-web/websocket-actorless
shuttle init --from shuttle-hq/shuttle-examples --subfolder actix-web/websocket-actorless
```

## Code
Expand All @@ -34,7 +33,7 @@ use std::{
use tokio::sync::{mpsc, watch};

const PAUSE_SECS: u64 = 15;
const STATUS_URI: &str = "https://api.shuttle.dev";
const STATUS_URI: &str = "https://api.shuttle.dev/.healthz";

type AppState = (
mpsc::UnboundedSender<WsState>,
Expand Down Expand Up @@ -120,12 +119,11 @@ async fn websocket(
async fn index() -> impl Responder {
NamedFile::open_async("./static/index.html")
.await
.map_err(|e| actix_web::error::ErrorInternalServerError(e))
.map_err(actix_web::error::ErrorInternalServerError)
}

#[shuttle_runtime::main]
async fn actix_web(
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Send + Clone + 'static> {
async fn main() -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Send + Clone + 'static> {
// We're going to use channels to communicate between threads.
// api state channel
let (tx_api_state, rx_api_state) = watch::channel(ApiStateMessage::default());
Expand Down Expand Up @@ -187,7 +185,7 @@ async fn actix_web(

if let Err(e) = shared_tx_api_state2.send(ApiStateMessage {
client_count,
origin: format!("ws_update"),
origin: "ws_update".to_string(),
date_time: Utc::now(),
is_up,
}) {
Expand All @@ -211,7 +209,7 @@ async fn actix_web(

async fn get_api_status(client: &reqwest::Client) -> bool {
let response = client.get(STATUS_URI).send().await;
response.is_ok()
response.is_ok_and(|r| r.status().is_success())
}
```

Expand Down Expand Up @@ -414,7 +412,10 @@ tracing = "0.1"
```

```toml Shuttle.toml
assets = ["static/*"]
[build]
assets = [
"static/*",
]
```
</CodeGroup>

Expand Down
2 changes: 1 addition & 1 deletion examples/actix.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This section revolves around simple Actix examples you can get quickly started w

<Tip>If you are looking for step-by-step guides, check out our [Tutorials](/templates/tutorials) section.</Tip>

You can clone the example below by running the following (you'll need `cargo-shuttle` installed):
You can clone the example below by running the following (you'll need `shuttle` CLI installed):
```bash
shuttle init --template actix-web
```
Expand Down
15 changes: 6 additions & 9 deletions examples/axum-jwt-authentication.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ Three Axum routes are registered in this file:
- `/login`: a route for posting a JSON object with a username and password to get a JWT.
- `/private`: a route that can only be accessed with a valid JWT.

You can clone the example below by running the following (you'll need `cargo-shuttle` installed):
You can clone the example below by running the following (you'll need `shuttle` CLI installed):

```bash
shuttle init --from shuttle-hq/shuttle-examples \
--subfolder axum/jwt-authentication
shuttle init --from shuttle-hq/shuttle-examples --subfolder axum/jwt-authentication
```

## Code
Expand Down Expand Up @@ -235,28 +234,26 @@ Once you've cloned this example, launch it locally by using `shuttle run`. Once
First, we should be able to access the public endpoint without any authentication using:

```sh
$ curl http://localhost:8000/public
curl http://localhost:8000/public
```

But trying to access the private endpoint will fail with a 403 forbidden:

```sh
$ curl http://localhost:8000/private
curl http://localhost:8000/private
```

So let's get a JWT from the login route first:


```sh
$ curl --header "Content-Type: application/json" --request POST \
--data '{"client_id": "foo", "client_secret": "bar"}' \
http://localhost:8000/login
curl -X POST --header "Content-Type: application/json" --data '{"client_id": "foo", "client_secret": "bar"}' http://localhost:8000/login
```

Accessing the private endpoint with the token will now succeed:

```sh
$ curl --header "Authorization: Bearer <token>" http://localhost:8000/private
curl --header "Authorization: Bearer <token>" http://localhost:8000/private
```

The token is set to expire in 5 minutes, so wait a while and try to access the private endpoint again. Once the token has expired, a user will need to get a new token from login.
Expand Down
5 changes: 2 additions & 3 deletions examples/axum-postgres.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ The following routes are provided:
- GET `/todos/<id>` - Get a to-do item by ID.
- POST `/todos` - Create a to-do item. Takes "note" as a JSON body parameter.

You can clone the example below by running the following (you'll need `cargo-shuttle` installed):
You can clone the example below by running the following (you'll need `shuttle` CLI installed):

```bash
shuttle init --from shuttle-hq/shuttle-examples \
--subfolder axum/postgres
shuttle init --from shuttle-hq/shuttle-examples --subfolder axum/postgres
```

## Code
Expand Down
12 changes: 7 additions & 5 deletions examples/axum-static-files.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ description: "This article walks you through setting up static files with Axum,

This example has one route at `/` where the homepage is served and shows you how you can serve HTML or other types of files with Actix Web.

Note that static assets are declared in the `Shuttle.toml` file.
Note that build assets are declared in `Shuttle.toml`.

You can clone the example below by running the following (you'll need `cargo-shuttle` installed):
You can clone the example below by running the following (you'll need `shuttle` CLI installed):

```bash
shuttle init --from shuttle-hq/shuttle-examples \
--subfolder axum/static-files
shuttle init --from shuttle-hq/shuttle-examples --subfolder axum/static-files
```

## Code
Expand Down Expand Up @@ -65,7 +64,10 @@ tower-http = { version = "0.5.0", features = ["fs"] }
```

```toml Shuttle.toml
assets = ["assets/*"]
[build]
assets = [
"assets",
]
```
</CodeGroup>

Expand Down
19 changes: 11 additions & 8 deletions examples/axum-websockets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ There are a few routes available:
- `/` - the homepage route where you can find the `index.html` page.
- `/ws` - the route that handles websockets.

You can clone the example below by running the following (you'll need `cargo-shuttle` installed):
You can clone the example below by running the following (you'll need `shuttle` CLI installed):

```bash
shuttle init --from shuttle-hq/shuttle-examples \
--subfolder axum/websocket
shuttle init --from shuttle-hq/shuttle-examples --subfolder axum/websocket
```

## Code
Expand Down Expand Up @@ -49,7 +48,7 @@ struct State {
}

const PAUSE_SECS: u64 = 15;
const STATUS_URI: &str = "https://api.shuttle.dev";
const STATUS_URI: &str = "https://api.shuttle.dev/.healthz";

#[derive(Serialize)]
struct Response {
Expand All @@ -60,7 +59,7 @@ struct Response {
}

#[shuttle_runtime::main]
async fn axum() -> ShuttleAxum {
async fn main() -> ShuttleAxum {
let (tx, rx) = watch::channel(Message::Text("{}".to_string()));

let state = Arc::new(Mutex::new(State {
Expand All @@ -74,8 +73,9 @@ async fn axum() -> ShuttleAxum {
let duration = Duration::from_secs(PAUSE_SECS);

loop {
let is_up = reqwest::get(STATUS_URI).await;
let is_up = is_up.is_ok();
let is_up = reqwest::get(STATUS_URI)
.await
.is_ok_and(|r| r.status().is_success());

let response = Response {
clients_count: state_send.lock().await.clients_count,
Expand Down Expand Up @@ -273,7 +273,10 @@ tower-http = { version = "0.5.0", features = ["fs"] }
```

```toml Shuttle.toml
assets = ["static/*"]
[build]
assets = [
"static",
]
```
</CodeGroup>

Expand Down
4 changes: 2 additions & 2 deletions examples/axum.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This section revolves around simple Axum examples you can get quickly started wi

<Tip>If you are looking for step-by-step guides, check out our [Tutorials](/templates/tutorials) section.</Tip>

You can clone the example below by running the following (you'll need `cargo-shuttle` installed):
You can clone the example below by running the following (you'll need `shuttle` CLI installed):

```bash
shuttle init --template axum
Expand Down Expand Up @@ -42,7 +42,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.7.3"
axum = "0.7.4"
shuttle-axum = "0.48.0"
shuttle-runtime = "0.48.0"
tokio = "1.28.2"
Expand Down
13 changes: 6 additions & 7 deletions examples/rocket-jwt-authentication.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ Then the JWT is sent with all requests requiring authentication using the HTTP h
This example uses the [`jsonwebtoken`](https://github.com/Keats/jsonwebtoken) which supports symmetric and asymmetric secret encoding, built-in validations, and most JWT algorithms.
However, this example only makes use of symmetric encoding and validation on the expiration claim.

You can clone the example below by running the following (you'll need `cargo-shuttle` installed):
You can clone the example below by running the following (you'll need `shuttle` CLI installed):
```bash
shuttle init --from shuttle-hq/shuttle-examples \
--subfolder rocket/jwt-authentication
shuttle init --from shuttle-hq/shuttle-examples --subfolder rocket/jwt-authentication
```

Three Rocket routes are registered in this file:
Expand Down Expand Up @@ -275,26 +274,26 @@ Once you've cloned this example, launch it locally by using `shuttle run`. Once
First, we should be able to access the public endpoint without any authentication using:

```sh
$ curl https://<host>/public
curl https://<host>/public
```

But trying to access the private endpoint will fail with a 403 forbidden:

```sh
$ curl https://<host>/private
curl https://<host>/private
```

So let's get a JWT from the login route first:


```sh
$ curl --request POST --data '{"username": "username", "password": "password"}' https://<host>/login
curl -X POST --data '{"username": "username", "password": "password"}' https://<host>/login
```

Accessing the private endpoint with the token will now succeed:

```sh
$ curl --header "Authorization: Bearer <token>" https://<host>/private
curl --header "Authorization: Bearer <token>" https://<host>/private
```

The token is set to expire in 5 minutes, so wait a while and try to access the private endpoint again. Once the token has expired, a user will need to get a new token from login.
Expand Down
Loading

0 comments on commit f4a6af4

Please sign in to comment.