feat(conf): allow to disable playground
This commit is contained in:
parent
ec334ef78b
commit
a09ef43775
2 changed files with 50 additions and 18 deletions
|
@ -30,10 +30,12 @@ pub struct GvaConf {
|
|||
#[serde(default = "ip4_default")]
|
||||
pub ip4: Ipv4Addr,
|
||||
pub ip6: Option<Ipv6Addr>,
|
||||
#[serde(default = "port_default")]
|
||||
pub port: u16,
|
||||
#[serde(default = "path_default")]
|
||||
pub path: String,
|
||||
#[serde(default = "playground_default")]
|
||||
pub playground: bool,
|
||||
#[serde(default = "port_default")]
|
||||
pub port: u16,
|
||||
pub remote_host: Option<String>,
|
||||
pub remote_port: Option<u16>,
|
||||
pub remote_path: Option<String>,
|
||||
|
@ -54,6 +56,10 @@ fn path_default() -> String {
|
|||
"gva".to_owned()
|
||||
}
|
||||
|
||||
const fn playground_default() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
const fn port_default() -> u16 {
|
||||
30_901
|
||||
}
|
||||
|
@ -71,8 +77,9 @@ impl Default for GvaConf {
|
|||
enabled: false,
|
||||
ip4: ip4_default(),
|
||||
ip6: Some(ip6_default()),
|
||||
port: port_default(),
|
||||
path: path_default(),
|
||||
port: port_default(),
|
||||
playground: playground_default(),
|
||||
remote_host: None,
|
||||
remote_port: None,
|
||||
remote_path: None,
|
||||
|
@ -150,6 +157,9 @@ impl GvaCommand {
|
|||
.msg(format!("Path ? [{}]", conf.path))
|
||||
.default(conf.path)
|
||||
.get();
|
||||
// playground
|
||||
let res = input().msg("Enable playground ? [Y/n]").default('Y').get();
|
||||
conf.playground = res != 'n';
|
||||
// remoteHost
|
||||
if let Some(ref remote_host) = conf.remote_host {
|
||||
let new_remote_host = input()
|
||||
|
|
52
src/lib.rs
52
src/lib.rs
|
@ -280,24 +280,46 @@ impl GvaModule {
|
|||
);
|
||||
let gva_playground_route = warp_::gva_playground_route(&conf);
|
||||
let gva_subscription_route = warp_::gva_subscription_route(&conf, gva_schema.clone());
|
||||
let routes = gva_route
|
||||
.or(gva_subscription_route)
|
||||
.or(gva_playground_route)
|
||||
.recover(|err: Rejection| async move {
|
||||
if let Some(warp_::BadRequest(err)) = err.find() {
|
||||
return Ok::<_, Infallible>(warp::reply::with_status(
|
||||
err.to_string(),
|
||||
http::StatusCode::BAD_REQUEST,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(warp::reply::with_status(
|
||||
"INTERNAL_SERVER_ERROR".to_string(),
|
||||
http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
))
|
||||
});
|
||||
// Define recover function
|
||||
let recover_func = |err: Rejection| async move {
|
||||
if let Some(warp_::BadRequest(err)) = err.find() {
|
||||
return Ok::<_, Infallible>(warp::reply::with_status(
|
||||
err.to_string(),
|
||||
http::StatusCode::BAD_REQUEST,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(warp::reply::with_status(
|
||||
"INTERNAL_SERVER_ERROR".to_string(),
|
||||
http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
))
|
||||
};
|
||||
|
||||
// Start warp server
|
||||
if conf.playground {
|
||||
Self::run_warp_server(
|
||||
&conf,
|
||||
gva_route
|
||||
.or(gva_subscription_route)
|
||||
.or(gva_playground_route)
|
||||
.recover(recover_func),
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
Self::run_warp_server(
|
||||
&conf,
|
||||
gva_route.or(gva_subscription_route).recover(recover_func),
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_warp_server<F>(conf: &GvaConf, routes: F)
|
||||
where
|
||||
F: warp::Filter<Error = Infallible> + Clone + Send + Sync + 'static,
|
||||
F::Extract: warp::Reply,
|
||||
{
|
||||
log::info!(
|
||||
"GVA server listen on http://{}:{}/{}",
|
||||
conf.ip4,
|
||||
|
|
Loading…
Add table
Reference in a new issue