Skip to content

Commit

Permalink
disable http server if port == 0
Browse files Browse the repository at this point in the history
  • Loading branch information
reymondzzzz committed Jan 11, 2024
1 parent 123be36 commit 98880ab
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
47 changes: 32 additions & 15 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use axum::{Extension, http::{StatusCode, Uri}, response::IntoResponse};
use hyper::Server;
use tokio::signal;
use tokio::sync::RwLock as ARwLock;
use tracing::info;
use tokio::task::JoinHandle;
use tracing::{error, info};

use crate::global_context::GlobalContext;
use crate::http::routers::make_refact_http_server;
Expand Down Expand Up @@ -50,19 +51,35 @@ pub async fn shutdown_signal(ask_shutdown_receiver: std::sync::mpsc::Receiver<St
pub async fn start_server(
global_context: Arc<ARwLock<GlobalContext>>,
ask_shutdown_receiver: std::sync::mpsc::Receiver<String>,
) -> Result<(), String> {
) -> Option<JoinHandle<()>> {
let port = global_context.read().await.cmdline.http_port;
let addr = ([127, 0, 0, 1], port).into();
let builder = Server::try_bind(&addr).map_err(|e| {
write!(std::io::stderr(), "PORT_BUSY {}\n", e).unwrap();
std::io::stderr().flush().unwrap();
format!("port busy, address {}: {}", addr, e)
})?;
info!("HTTP server listening on {}", addr);
let router = make_refact_http_server().layer(Extension(global_context.clone()));
let server = builder
.serve(router.into_make_service())
.with_graceful_shutdown(shutdown_signal(ask_shutdown_receiver));
let resp = server.await.map_err(|e| format!("HTTP server error: {}", e));
resp
if port == 0 {
return None
}
return Some(tokio::spawn(async move {
let addr = ([127, 0, 0, 1], port).into();
let builder = Server::try_bind(&addr).map_err(|e| {
write!(std::io::stderr(), "PORT_BUSY {}\n", e).unwrap();
std::io::stderr().flush().unwrap();
format!("port busy, address {}: {}", addr, e)
});
match builder {
Ok(builder) => {
info!("HTTP server listening on {}", addr);
let router = make_refact_http_server().layer(Extension(global_context.clone()));
let server = builder
.serve(router.into_make_service())
.with_graceful_shutdown(shutdown_signal(ask_shutdown_receiver));
let resp = server.await.map_err(|e| format!("HTTP server error: {}", e));
if let Err(e) = resp {
error!("server error: {}", e);
} else {
info!("clean shutdown");
}
}
Err(e) => {
error!("server error: {}", e);
}
}
}));
}
32 changes: 19 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::Write;
use tokio::task::JoinHandle;

use tracing::{error, info, Level};
use tracing_appender;
Expand Down Expand Up @@ -57,22 +58,27 @@ async fn main() {
info!("{:>20} {}", k, v);
}
}

let mut background_tasks = start_background_tasks(gcx.clone());
let lsp_task = spawn_lsp_task(gcx.clone(), cmdline.clone()).await; // execution stays inside if stdin-stdout
if lsp_task.is_some() {
background_tasks.push_back(lsp_task.unwrap())
}

let gcx_clone = gcx.clone();
let server = http::start_server(gcx_clone, ask_shutdown_receiver);
let server_result = server.await;
if let Err(e) = server_result {
error!("server error: {}", e);
} else {
info!("clean shutdown");
}
let should_start_http = cmdline.http_port != 0;
let should_start_lsp = (cmdline.lsp_port == 0 && cmdline.lsp_stdin_stdout == 1) ||
(cmdline.lsp_port != 0 && cmdline.lsp_stdin_stdout == 0);

let mut main_handle: Option<JoinHandle<()>> = None;
if should_start_http {
main_handle = http::start_server(gcx.clone(), ask_shutdown_receiver).await;
}
if should_start_lsp {
if main_handle.is_none() {
main_handle = spawn_lsp_task(gcx.clone(), cmdline.clone()).await;
} else {
background_tasks.push_back(spawn_lsp_task(gcx.clone(), cmdline.clone()).await.unwrap())
}
}
if main_handle.is_some() {
let _ = main_handle.unwrap().await;
}

background_tasks.abort().await;
info!("saving telemetry without sending, so should be quick");
basic_transmit::basic_telemetry_compress(gcx.clone()).await;
Expand Down

0 comments on commit 98880ab

Please sign in to comment.