Skip to main content

vertigo_cli/serve/
vertigo_install.rs

1use actix_files::Files;
2use actix_web::web;
3
4use crate::serve::MountConfig;
5
6use super::{
7    precompress::precompressed_files, server_state::ServerState, vertigo_handler::vertigo_handler,
8};
9
10/// Install vertigo handler and static files based on mount config.
11///
12/// The handler can work only if ServerState is initialized before.
13/// This can't be done automatically because initialization compiles the WASM which takes some time
14///
15/// Example:
16///
17/// ```no_run
18/// use actix_web::{web, App};
19/// use vertigo_cli::serve::{MountConfigBuilder, ServerState, vertigo_install};
20///
21/// let mount_config = MountConfigBuilder::new("/", "./build").build().unwrap();
22/// ServerState::init(&mount_config).unwrap();
23///
24/// let app = App::new();
25///
26/// app.configure(|srvc| vertigo_install(srvc, &mount_config));
27/// ```
28///
29/// To bootstrap a project using this method, use fullstack template; run:
30///
31/// ```sh
32/// vertigo-cli new -t fullstack my_project
33/// ```
34///
35pub fn vertigo_install(cfg: &mut web::ServiceConfig, mount_config: &MountConfig) {
36    let mount_point = mount_config.mount_point();
37
38    if mount_config.precompress {
39        let files = ServerState::global(mount_point).precompressed();
40        cfg.service(precompressed_files(&mount_config.dest_http_root(), files));
41    }
42
43    cfg.service(Files::new(
44        &mount_config.dest_http_root(),
45        mount_config.dest_dir(),
46    ));
47
48    if mount_point == "/" {
49        cfg.default_service(vertigo_handler(mount_config));
50    } else {
51        cfg.service(web::scope(mount_point).default_service(vertigo_handler(mount_config)));
52    };
53}