Skip to content

Commit

Permalink
docs: 📝 added documentation for actix-web integration
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Aug 23, 2021
1 parent 5e7a867 commit 1877c13
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
- [Incremental generation](./strategies/incremental.md)
- [Building](./building.md)
- [Serving](./serving.md)
- [Actix Web Integration](./integrations/actix-web.md)
- [Config Managers](./config_managers.md)
59 changes: 59 additions & 0 deletions docs/src/integrations/actix-web.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Actix Web Integration

If you're using [Actix Web](https://actix.rs), then Perseus can automate nearly all the boilerplate of serving your app for you!

This integration provides a configuration function, which you can use to configure an existing web server to support Perseus, so you could even run something like [Diana](https://github.com/arctic-hen7/diana) on the same server!

This integration should support almost every use case of Perseus, but there may be some extremely advanced things that you'll need to go back to basics for. If that's the case, please let us know by [opening an issue]() (we want these integrations to be as powerful as possible), and in the meantime you can use the guide [here](./serving.md) to see how to set up a server without using the integrations. If you need implementation details, check out the actual source code for the integration in the [repository](https://github.com/arctic-hen7/perseus).

## Installation

You can install the Actix Web integration by adding the following to your `Cargo.toml` under the `dependencies` section:

```toml
perseus-actix-web = "0.1"
```

Note that you will still need `actix-web`, `futures`, and `perseus` itself, even in a server repository.

All Perseus integrations follow the same version format as the core library, meaning they're all updated simultaneously. This makes version management much easier, and it means that you can just install the same version of every Perseus package and not have to worry about compatibility issues.

## Usage

This is an example of a web server that only uses Perseus, but you can call `.configure()` on any existing web server. Note though that **Perseus must be configured after all other logic**, because it adds a generic handler for all remaining pages, which will break other more specific logic that comes after it.

```rust,no_run
use perseus::{FsConfigManager, SsrNode};
use perseus_actix_web::{configurer, Options};
use perseus_showcase_app::pages;
use actix_web::{HttpServer, App};
use futures::executor::block_on;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
// Other server logic here
.configure(
block_on(configurer(
Options {
index: "../app/index.html".to_string(),
js_bundle: "../app/pkg/bundle.js".to_string(),
wasm_bundle: "../app/pkg/perseus_showcase_app_bg.wasm".to_string(),
templates_map: pages::get_templates_map::<SsrNode>()
},
FsConfigManager::new()
))
)
})
.bind(("localhost", 8080))?
.run()
.await
}
```

When you use the integration, you'll have to define a few options to tell it what exactly to serve. Specifically, you'll need to tell it where your `index.html` file, your JS bundle, and your WASM bundle all are. In addition, you'll need to a provide it with a template map (which you'll often define a getter function for as above).

Also, because this plugs into an existing server, you have full control over hosting options, like the port to be used!

It's worth mentioning the blocking component of this design. The function that returns the closure that actually configures your server for Perseus is asynchronous because it needs to get your render configuration and add it as data to the server (this improves performance by reducing reads), which unfortunately is an asynchronous operation. We also can't `.await` that without causing ownership errors due to Actix Web's closure structure, which means the best solution for now is to `block_on` that configuration (which won't impact performance other than in your startup times, and all that's happening is a read from a file). If you have a better solution, [PRs are welcome](https://github.com/arctic-hen7/pulls)!
5 changes: 4 additions & 1 deletion docs/src/serving.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ Having generated a large number of static files, you'll need a system to host yo

Perseus aims to be agnostic as to what framework you use to host your files, and any framework that gives you access to request headers and wildcard paths should work (in other words, any framework worth its salt).

In future, Perseus will provide separate integration libraries for common frameworks that manage this side of things for you!
If you're using one of our supported integrations, you don't have to bother with this page, nearly all of it can be done for you!

- [Actix Web](./integrations/actix-web.md)
- *More coming soon...*

## Endpoints

Expand Down

0 comments on commit 1877c13

Please sign in to comment.