Add ctrl+c handler

This commit is contained in:
Andras Schmelczer 2024-12-18 22:37:42 +00:00
parent a4d9ec16a1
commit bdd8650ba7
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
3 changed files with 35 additions and 4 deletions

View file

@ -38,7 +38,8 @@ impl<'a> EditedText<'a, String> {
/// the original text, it will result in the updated text. The default
/// word tokenizer is used to tokenize the text which splits the text on
/// whitespaces.
#[must_use] pub fn from_strings(original: &'a str, updated: &str) -> Self {
#[must_use]
pub fn from_strings(original: &'a str, updated: &str) -> Self {
Self::from_strings_with_tokenizer(original, updated, &word_tokenizer)
}
}
@ -186,7 +187,8 @@ where
Self { text, operations }
}
#[must_use] pub fn merge(self, other: Self) -> Self {
#[must_use]
pub fn merge(self, other: Self) -> Self {
debug_assert_eq!(
self.text, other.text,
"EditedTexts must be derived from the same text to be mergable"
@ -235,7 +237,8 @@ where
///
/// Returns an `SyncLibError::OperationError` if the operations cannot be
/// applied to the text.
#[must_use] pub fn apply(&self) -> String {
#[must_use]
pub fn apply(&self) -> String {
let mut builder: StringBuilder<'_> = StringBuilder::new(self.text);
for OrderedOperation { operation, .. } in &self.operations {

View file

@ -110,7 +110,8 @@ where
#[cfg(debug_assertions)]
debug_assert!(
deleted_text
.as_ref().is_none_or(|text| builder.get_slice(self.range()) == *text),
.as_ref()
.is_none_or(|text| builder.get_slice(self.range()) == *text),
"Text to delete does not match the text in the rope"
);

View file

@ -14,6 +14,7 @@ use axum::{
Extension, Json,
};
use log::info;
use tokio::signal;
use tower_http::cors::CorsLayer;
use crate::app_state::AppState;
@ -91,8 +92,34 @@ pub async fn create_server(app_state: AppState) -> Result<()> {
);
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.tcp_nodelay(true)
.await
.context("Failed to start server")
}
async fn serve_api(Extension(api): Extension<OpenApi>) -> impl IntoResponse { Json(api) }
async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
}