vertigo_cli/serve/
vertigo_install.rs

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