Take a listen address

This commit is contained in:
Jeremy Wall 2022-02-21 19:17:32 -05:00
parent 8b3be6260b
commit d31e234e44
2 changed files with 16 additions and 5 deletions

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::env; use std::env;
use std::net::SocketAddr;
use std::path::PathBuf; use std::path::PathBuf;
use clap; use clap;
@ -42,6 +43,7 @@ where
(@subcommand serve => (@subcommand serve =>
(about: "Serve the interface via the web") (about: "Serve the interface via the web")
(@arg recipe_dir: -d --dir +takes_value "Directory containing recipe files to use") (@arg recipe_dir: -d --dir +takes_value "Directory containing recipe files to use")
(@arg listen: --listen +takes_value "address and port to listen on 0.0.0.0:3030")
) )
) )
.setting(clap::AppSettings::SubcommandRequiredElseHelp) .setting(clap::AppSettings::SubcommandRequiredElseHelp)
@ -76,12 +78,21 @@ fn main() {
} }
} }
} else if let Some(matches) = matches.subcommand_matches("serve") { } else if let Some(matches) = matches.subcommand_matches("serve") {
println!("Launching web interface...");
let recipe_dir_path = if let Some(dir) = matches.value_of("recipe_dir") { let recipe_dir_path = if let Some(dir) = matches.value_of("recipe_dir") {
PathBuf::from(dir) PathBuf::from(dir)
} else { } else {
std::env::current_dir().expect("Unable to get current directory. Bailing out.") std::env::current_dir().expect("Unable to get current directory. Bailing out.")
}; };
async_std::task::block_on(async { web::ui_main(recipe_dir_path).await }); let listen_socket: SocketAddr = if let Some(listen_socket) = matches.value_of("listen") {
listen_socket.parse().expect(&format!(
"--listen must be of the form <addr>:<port> but got {}",
listen_socket
))
} else {
"127.0.0.1:3030".parse().unwrap()
};
println!("Launching web interface...");
println!("listening on {}", listen_socket);
async_std::task::block_on(async { web::ui_main(recipe_dir_path, listen_socket).await });
} }
} }

View File

@ -1,3 +1,4 @@
use std::net::SocketAddr;
// Copyright 2022 Jeremy Wall // Copyright 2022 Jeremy Wall
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
@ -34,7 +35,7 @@ pub async fn get_recipes(recipe_dir_path: PathBuf) -> Result<Vec<String>, ParseE
Ok(entry_vec) Ok(entry_vec)
} }
pub async fn ui_main(recipe_dir_path: PathBuf) { pub async fn ui_main(recipe_dir_path: PathBuf, listen_socket: SocketAddr) {
let root = warp::path::end().map(|| warp::redirect::found(Uri::from_static("/ui"))); let root = warp::path::end().map(|| warp::redirect::found(Uri::from_static("/ui")));
let ui = warp::path("ui").and(static_dir!("../web/dist")); let ui = warp::path("ui").and(static_dir!("../web/dist"));
let api = warp::path("api") let api = warp::path("api")
@ -58,6 +59,5 @@ pub async fn ui_main(recipe_dir_path: PathBuf) {
let routes = root.or(ui).or(api).with(warp::log("access log")); let routes = root.or(ui).or(api).with(warp::log("access log"));
// TODO(jwall): Take listen address as an argument to this function instead. warp::serve(routes).run(listen_socket).await;
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
} }