Skip to content

Commit 1877c13

Browse files
committed
docs: 📝 added documentation for actix-web integration
1 parent 5e7a867 commit 1877c13

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

docs/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
- [Incremental generation](./strategies/incremental.md)
1616
- [Building](./building.md)
1717
- [Serving](./serving.md)
18+
- [Actix Web Integration](./integrations/actix-web.md)
1819
- [Config Managers](./config_managers.md)

docs/src/integrations/actix-web.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Actix Web Integration
2+
3+
If you're using [Actix Web](https://actix.rs), then Perseus can automate nearly all the boilerplate of serving your app for you!
4+
5+
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!
6+
7+
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).
8+
9+
## Installation
10+
11+
You can install the Actix Web integration by adding the following to your `Cargo.toml` under the `dependencies` section:
12+
13+
```toml
14+
perseus-actix-web = "0.1"
15+
```
16+
17+
Note that you will still need `actix-web`, `futures`, and `perseus` itself, even in a server repository.
18+
19+
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.
20+
21+
## Usage
22+
23+
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.
24+
25+
```rust,no_run
26+
use perseus::{FsConfigManager, SsrNode};
27+
use perseus_actix_web::{configurer, Options};
28+
use perseus_showcase_app::pages;
29+
use actix_web::{HttpServer, App};
30+
use futures::executor::block_on;
31+
32+
#[actix_web::main]
33+
async fn main() -> std::io::Result<()> {
34+
HttpServer::new(|| {
35+
App::new()
36+
// Other server logic here
37+
.configure(
38+
block_on(configurer(
39+
Options {
40+
index: "../app/index.html".to_string(),
41+
js_bundle: "../app/pkg/bundle.js".to_string(),
42+
wasm_bundle: "../app/pkg/perseus_showcase_app_bg.wasm".to_string(),
43+
templates_map: pages::get_templates_map::<SsrNode>()
44+
},
45+
FsConfigManager::new()
46+
))
47+
)
48+
})
49+
.bind(("localhost", 8080))?
50+
.run()
51+
.await
52+
}
53+
```
54+
55+
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).
56+
57+
Also, because this plugs into an existing server, you have full control over hosting options, like the port to be used!
58+
59+
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)!

docs/src/serving.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ Having generated a large number of static files, you'll need a system to host yo
44

55
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).
66

7-
In future, Perseus will provide separate integration libraries for common frameworks that manage this side of things for you!
7+
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!
8+
9+
- [Actix Web](./integrations/actix-web.md)
10+
- *More coming soon...*
811

912
## Endpoints
1013

0 commit comments

Comments
 (0)