Skip to content

Commit b71fa41

Browse files
committed
feat(cli): added flag to set server integration to use
Also added a warning about the Actix Web integration that it's using an unstable beta version of Actix.
1 parent 139d309 commit b71fa41

File tree

6 files changed

+73
-13
lines changed

6 files changed

+73
-13
lines changed

examples/basic/.perseus/server/src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use std::fs;
1919
#[cfg(feature = "integration-actix-web")]
2020
#[actix_web::main]
2121
async fn main() -> std::io::Result<()> {
22+
println!("WARNING: The Actix Web integration uses a beta version of Actix Web, and is considered unstable. It is not recommended for production usage.");
23+
2224
use actix_web::{App, HttpServer};
2325
use perseus_actix_web::configurer;
2426

packages/perseus-cli/src/bin/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn core(dir: PathBuf) -> Result<i32, Error> {
154154
Subcommand::Snoop(snoop_subcmd) => match snoop_subcmd {
155155
SnoopSubcommand::Build => snoop_build(dir)?,
156156
SnoopSubcommand::WasmBuild => snoop_wasm_build(dir)?,
157-
SnoopSubcommand::Serve => snoop_server(dir)?,
157+
SnoopSubcommand::Serve(snoop_serve_opts) => snoop_server(dir, snoop_serve_opts)?,
158158
},
159159
Subcommand::Prep => {
160160
// The `.perseus/` directory has already been set up in the preliminaries, so we don't need to do anything here

packages/perseus-cli/src/deploy.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::errors::*;
22
use crate::export;
3+
use crate::parse::Integration;
34
use crate::parse::{DeployOpts, ExportOpts, ServeOpts};
45
use crate::serve;
56
use fs_extra::copy_items;
@@ -15,15 +16,15 @@ pub fn deploy(dir: PathBuf, opts: DeployOpts) -> Result<i32, Error> {
1516
let exit_code = if opts.export_static {
1617
deploy_export(dir, opts.output)?
1718
} else {
18-
deploy_full(dir, opts.output)?
19+
deploy_full(dir, opts.output, opts.integration)?
1920
};
2021

2122
Ok(exit_code)
2223
}
2324

2425
/// Deploys the user's app in its entirety, with a bundled server. This can return any kind of error because deploying involves working
2526
/// with other subcommands.
26-
fn deploy_full(dir: PathBuf, output: String) -> Result<i32, Error> {
27+
fn deploy_full(dir: PathBuf, output: String, integration: Integration) -> Result<i32, Error> {
2728
// Build everything for production, not running the server
2829
let (serve_exit_code, server_path) = serve(
2930
dir.clone(),
@@ -32,6 +33,7 @@ fn deploy_full(dir: PathBuf, output: String) -> Result<i32, Error> {
3233
no_build: false,
3334
release: true,
3435
standalone: true,
36+
integration,
3537
},
3638
)?;
3739
if serve_exit_code != 0 {

packages/perseus-cli/src/parse.rs

+40-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ pub struct Opts {
1414
pub subcmd: Subcommand,
1515
}
1616

17+
#[derive(Parser, PartialEq, Eq)]
18+
pub enum Integration {
19+
ActixWeb,
20+
Warp,
21+
}
22+
// We use an `enum` for this so we don't get errors from Cargo about non-existent feature flags, overly verbose but fails quickly
23+
impl std::str::FromStr for Integration {
24+
type Err = String;
25+
26+
fn from_str(s: &str) -> Result<Self, Self::Err> {
27+
match s {
28+
"actix-web" => Ok(Self::ActixWeb),
29+
"warp" => Ok(Self::Warp),
30+
_ => Err("invalid integration name".into()),
31+
}
32+
}
33+
}
34+
impl ToString for Integration {
35+
fn to_string(&self) -> String {
36+
match self {
37+
Self::ActixWeb => "actix-web".to_string(),
38+
Self::Warp => "warp".to_string(),
39+
}
40+
}
41+
}
42+
1743
#[derive(Parser)]
1844
pub enum Subcommand {
1945
Build(BuildOpts),
@@ -61,6 +87,9 @@ pub struct ServeOpts {
6187
/// Make the final binary standalone (this is used in `perseus deploy` only, don't manually invoke it unless you have a good reason!)
6288
#[clap(long)]
6389
pub standalone: bool,
90+
/// The server integration to use
91+
#[clap(short, long, default_value = "warp")]
92+
pub integration: Integration,
6493
}
6594
/// Removes `.perseus/` entirely for updates or to fix corruptions
6695
#[derive(Parser)]
@@ -81,6 +110,9 @@ pub struct DeployOpts {
81110
/// Export you app to purely static files (see `export`)
82111
#[clap(short, long)]
83112
pub export_static: bool,
113+
/// The server integration to use (only affects non-exported deployments)
114+
#[clap(short, long, default_value = "warp")]
115+
pub integration: Integration,
84116
}
85117
/// Runs the `tinker` action of plugins, which lets them modify the Perseus engine
86118
#[derive(Parser)]
@@ -100,5 +132,12 @@ pub enum SnoopSubcommand {
100132
/// Snoops on the Wasm building process (mostly for debugging errors)
101133
WasmBuild,
102134
/// Snoops on the server process (run `perseus build` before this)
103-
Serve,
135+
Serve(SnoopServeOpts),
136+
}
137+
138+
#[derive(Parser)]
139+
pub struct SnoopServeOpts {
140+
/// The server integration to use
141+
#[clap(short, long, default_value = "warp")]
142+
pub integration: Integration,
104143
}

packages/perseus-cli/src/serve.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::build::{build_internal, finalize};
22
use crate::cmd::{cfg_spinner, run_stage};
33
use crate::errors::*;
4-
use crate::parse::ServeOpts;
4+
use crate::parse::{Integration, ServeOpts};
55
use crate::thread::{spawn_thread, ThreadHandle};
66
use console::{style, Emoji};
77
use indicatif::{MultiProgress, ProgressBar};
@@ -37,10 +37,18 @@ fn build_server(
3737
exec: Arc<Mutex<String>>,
3838
is_release: bool,
3939
is_standalone: bool,
40+
integration: Integration,
4041
) -> Result<
4142
ThreadHandle<impl FnOnce() -> Result<i32, ExecutionError>, Result<i32, ExecutionError>>,
4243
ExecutionError,
4344
> {
45+
// If we're using the Actix Web integration, warn that it's unstable
46+
// A similar warning is emitted for snooping on it
47+
// TODO Remove this once Actix Web v4.0.0 goes stable
48+
if integration == Integration::ActixWeb {
49+
println!("WARNING: The Actix Web integration uses a beta version of Actix Web, and is considered unstable. It is not recommended for production usage.")
50+
}
51+
4452
let num_steps = match did_build {
4553
true => 4,
4654
false => 2,
@@ -69,11 +77,13 @@ fn build_server(
6977
"{} build --message-format json {} {}",
7078
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string()),
7179
if is_release { "--release" } else { "" },
72-
if is_standalone {
73-
"--features standalone"
74-
} else {
75-
""
76-
}
80+
// Enable the appropriate integration
81+
format!(
82+
"--features integration-{} {} --no-default-features",
83+
integration.to_string(),
84+
// We'll also handle whether or not it's standalone because that goes under the `--features` flag
85+
if is_standalone { "standalone" } else { "" }
86+
)
7787
)],
7888
&sb_target,
7989
&sb_spinner,
@@ -198,6 +208,7 @@ pub fn serve(dir: PathBuf, opts: ServeOpts) -> Result<(i32, Option<String>), Exe
198208
Arc::clone(&exec),
199209
opts.release,
200210
opts.standalone,
211+
opts.integration,
201212
)?;
202213
// Only build if the user hasn't set `--no-build`, handling non-zero exit codes
203214
if did_build {

packages/perseus-cli/src/snoop.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::errors::*;
2+
use crate::parse::SnoopServeOpts;
23
use std::env;
34
use std::path::{Path, PathBuf};
45
use std::process::{Command, Stdio};
@@ -59,12 +60,17 @@ pub fn snoop_wasm_build(dir: PathBuf) -> Result<i32, ExecutionError> {
5960
}
6061

6162
/// Runs the commands to run the server directly so the user can see detailed logs.
62-
pub fn snoop_server(dir: PathBuf) -> Result<i32, ExecutionError> {
63+
pub fn snoop_server(dir: PathBuf, opts: SnoopServeOpts) -> Result<i32, ExecutionError> {
6364
let target = dir.join(".perseus/server");
6465
run_cmd_directly(
6566
format!(
66-
"{} run",
67-
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string())
67+
"{} run {}",
68+
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string()),
69+
// Enable the appropriate feature for a non-default server integration
70+
format!(
71+
"--features integration-{} --no-default-features",
72+
opts.integration.to_string()
73+
)
6874
),
6975
&target,
7076
)

0 commit comments

Comments
 (0)