Skip to content
This repository was archived by the owner on Mar 25, 2023. It is now read-only.

Commit d4ef463

Browse files
author
Shobhit
committed
Allow bind to an address(for fpm-controller hosting purposes)
1 parent 6c063a1 commit d4ef463

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/commands/serve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ async fn serve_static(req: actix_web::HttpRequest) -> actix_web::HttpResponse {
144144
}
145145

146146
#[actix_web::main]
147-
pub async fn serve(port: &str) -> std::io::Result<()> {
147+
pub async fn serve(bind_address: &str, port: &str) -> std::io::Result<()> {
148148
println!("### Server Started ###");
149-
println!("Go to: http://127.0.0.1:{}", port);
149+
println!("Go to: http://{}:{}", bind_address, port);
150150
actix_web::HttpServer::new(|| {
151151
actix_web::App::new().route("/{path:.*}", actix_web::web::get().to(serve_static))
152152
})
153-
.bind(format!("127.0.0.1:{}", port))?
153+
.bind(format!("{}:{}", bind_address, port))?
154154
.run()
155155
.await
156156
}

src/main.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ async fn main() -> fpm::Result<()> {
7373
fpm::stop_tracking(&config, source, target).await?;
7474
}
7575
if let Some(mark) = matches.subcommand_matches("serve") {
76-
let port = mark.value_of("port").unwrap_or("8000").to_string();
76+
let port = mark
77+
.value_of("port")
78+
.unwrap_or(mark.value_of("positional_port").unwrap_or("8000"))
79+
.to_string();
80+
let bind = mark.value_of("bind").unwrap_or("127.0.0.1").to_string();
7781
tokio::task::spawn_blocking(move || {
78-
fpm::serve(port.as_str()).expect("http service error");
82+
fpm::serve(bind.as_str(), port.as_str()).expect("http service error");
7983
})
8084
.await
8185
.expect("Thread spawn error");
@@ -222,7 +226,24 @@ fn app(authors: &'static str, version: &'static str) -> clap::App<'static, 'stat
222226
)
223227
.subcommand(
224228
clap::SubCommand::with_name("serve")
225-
.arg(clap::Arg::with_name("port").required(false))
229+
.arg(clap::Arg::with_name("port")
230+
.required_unless("positional_port")
231+
.required(false)
232+
.help("Specify the port to serve on")
233+
)
234+
.arg(clap::Arg::with_name("positional_port")
235+
.long("--port")
236+
.required_unless("bind")
237+
.takes_value(true)
238+
.required(false)
239+
.help("Specify the port to serve on")
240+
)
241+
.arg(clap::Arg::with_name("bind")
242+
.long("--bind")
243+
.takes_value(true)
244+
.required(false)
245+
.help("Specify the bind address to serve on")
246+
)
226247
.about("Create an http server and serves static files")
227248
.version(env!("CARGO_PKG_VERSION")),
228249
)

0 commit comments

Comments
 (0)