Skip to content

Commit

Permalink
docs(backend): shuttle
Browse files Browse the repository at this point in the history
  • Loading branch information
robertohuertasm committed Jun 29, 2023
1 parent ca12a9e commit 37651d7
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 3 deletions.
6 changes: 3 additions & 3 deletions docs/src/02_01_workspace_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ The `vsc` flag is used in this case to tell `cargo` to not initialize a new git
Don't worry if you receive the error message below, it is expected. We will fix it later.
```

![Workspace Error](./assets/workspace_error.png)
![Workspace Error](./assets/21/workspace_error.png)

### Creating the `shuttle` crate

Expand Down Expand Up @@ -109,7 +109,7 @@ cargo build

You should see something like this:

![Cargo Build](./assets/cargo_build.png)
![Cargo Build](./assets/21/cargo_build.png)

```admonish warning
Don't commit yet as we still have some work to do!
Expand All @@ -130,7 +130,7 @@ Aside from that, **remove** all the `.gitignore` files from the crates as they a

This is how it should look like:

![.gitignore](./assets/gitignore.png)
![.gitignore](./assets/21/gitignore.png)

## Committing the changes

Expand Down
75 changes: 75 additions & 0 deletions docs/src/02_02_shuttle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Exploring Shuttle

Open the `api/shuttle` folder and look for the `src/main.rs` file. This is the entry point of our application.

You'll see something like this:

```rust
use actix_web::{get, web::ServiceConfig};
use shuttle_actix_web::ShuttleActixWeb;

#[get("/")]
async fn hello_world() -> &'static str {
"Hello World!"
}

#[shuttle_runtime::main]
async fn actix_web(
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Send + Clone + 'static> {
let config = move |cfg: &mut ServiceConfig| {
cfg.service(hello_world);
};

Ok(config.into())
}
```

Shuttle has generated a simple `hello-world` [Actix-web](https://actix.rs) application for us.

As you can see, it's pretty straight-forward.

The `actix_web` function is the entry point of our application. It returns a `ShuttleActixWeb` instance that will be used by Shuttle to run our application.

In this function, we're going to configure our different routes. In this case, we only have one route: `/`, which is mapped to the `hello_world` function.

## Let's run it!

In the **root of the project**, run the following command:

```bash
cargo shuttle run
```

You should see something like this:

![shuttle_run](./assets/22/cargo_shuttle_run.png)

Now *curl* the `/` route:

```bash
curl localhost:8000
```

Or *open* it in your browser.

Hopefully, **you should see a greeting** in your screen!

And that's how easy it is to create a simple API with [Shuttle](https://www.shuttle.rs)!

```admonish
We're using [Actix-web](https://actix.rs) as our web framework, but you can any other framework supported by Shuttle.
Check out the [Shuttle documentation](https://docs.shuttle.rs/introduction/welcome) to learn more. Browse through the `Examples` section to see how to use Shuttle with other frameworks.
[Shuttle](https://www.shuttle.rs/) supports the following frameworks:
- [Actix-web](https://actix.rs)
- [Axum](https://github.com/tokio-rs/axum)
- [Salvo](https://next.salvo.rs/)
- [Poem](https://github.com/poem-web/poem)
- [Thruster](https://github.com/thruster-rs/Thruster)
- [Tower](https://github.com/tower-rs/tower)
- [Warp](https://github.com/seanmonstar/warp)
- [Rocket](https://rocket.rs)
- [Tide](https://github.com/http-rs/tide)
```
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# Backend
- [Backend](./02_backend.md)
- [Workspace Setup](./02_01_workspace_setup.md)
- [Exploring Shuttle](./02_02_shuttle.md)

# Frontend
- [Frontend](./04_frontend.md)
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added docs/src/assets/22/cargo_shuttle_run.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 37651d7

Please sign in to comment.