From 993962de494d9e8baf78d1fb29bc5f113934d4f7 Mon Sep 17 00:00:00 2001 From: Fankai Liu Date: Thu, 14 Dec 2023 22:52:43 +0800 Subject: [PATCH] update salvo version --- src/template/src/main_template.hbs | 39 ++++++++++++++++++--- src/template/src/routers/demo.hbs | 4 +-- src/template/src/routers/static_routers.hbs | 4 +-- src/template/src/routers/user.hbs | 4 +-- src/utils/create_project.rs | 2 +- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/template/src/main_template.hbs b/src/template/src/main_template.hbs index d48f1a3..3e73095 100644 --- a/src/template/src/main_template.hbs +++ b/src/template/src/main_template.hbs @@ -7,9 +7,12 @@ use crate::db::init_db_conn; {{#if is_mongodb}} use crate::db::init_db_conn; {{/if}} +use tokio::signal; +use tracing::info; use crate::middleware::handle_404::handle_404; use crate::routers::router; use config::{CERT_KEY, CFG}; +use salvo::server::ServerHandle; use salvo::catcher::Catcher; use salvo::conn::rustls::{Keycert, RustlsConfig}; use salvo::prelude::*; @@ -83,10 +86,9 @@ async fn main() { .await; let server = Server::new(acceptor); let handle = server.handle(); + tokio::spawn(shutdown_signal(handle)); server.serve(service).await; - handle.stop_graceful(std::time::Duration::from_secs(5)); - - } + } false => { println!( "📖 {{swagger_api_page}}: http://{}/swagger-ui", @@ -100,9 +102,10 @@ async fn main() { let acceptor = TcpListener::new(&CFG.server.address).bind().await; let server = Server::new(acceptor); let handle = server.handle(); + tokio::spawn(shutdown_signal(handle)); server.serve(service).await; - handle.stop_graceful(std::time::Duration::from_secs(5)); } - }; + } + } } fn init_log() { @@ -116,6 +119,32 @@ fn init_log() { .init(); tracing::info!("log level: {}", &CFG.log.filter_level); } + +async fn shutdown_signal(handle: ServerHandle) { + 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 => info!("ctrl_c signal received"), + _ = terminate => info!("terminate signal received"), + } + handle.stop_graceful(std::time::Duration::from_secs(60)); +} + #[cfg(test)] mod tests { use salvo::prelude::*; diff --git a/src/template/src/routers/demo.hbs b/src/template/src/routers/demo.hbs index 4cf774d..d8db368 100644 --- a/src/template/src/routers/demo.hbs +++ b/src/template/src/routers/demo.hbs @@ -2,9 +2,9 @@ use askama::Template; {{/if}} {{#if is_web_site}} -use salvo::{endpoint, writing::Text, Request, Response}; +use salvo::{oapi::endpoint, writing::Text, Request, Response}; {{else}} -use salvo::endpoint; +use salvo::oapi::endpoint; {{/if}} use crate::app_response::AppResult; {{#if is_web_site}} diff --git a/src/template/src/routers/static_routers.hbs b/src/template/src/routers/static_routers.hbs index 24d9b77..b65929a 100644 --- a/src/template/src/routers/static_routers.hbs +++ b/src/template/src/routers/static_routers.hbs @@ -1,8 +1,8 @@ use rust_embed::RustEmbed; {{#if need_db_conn}} -use salvo::{Router, serve_static::static_embed, endpoint, Response, http::ResBody, hyper::body::Bytes}; +use salvo::{Router, serve_static::static_embed, oapi::endpoint, Response, http::ResBody, hyper::body::Bytes}; {{else}} -use salvo::{Router, endpoint, Response, http::ResBody, hyper::body::Bytes}; +use salvo::{Router, oapi::endpoint, Response, http::ResBody, hyper::body::Bytes}; {{/if}} #[derive(RustEmbed)] #[folder = "assets"] diff --git a/src/template/src/routers/user.hbs b/src/template/src/routers/user.hbs index dce23b7..365f540 100644 --- a/src/template/src/routers/user.hbs +++ b/src/template/src/routers/user.hbs @@ -9,7 +9,7 @@ use crate::{ }; use askama::Template; use salvo::{ - endpoint, + oapi::endpoint, http::cookie::Cookie, oapi::extract::{JsonBody, PathParam}, writing::{Redirect, Text}, @@ -114,7 +114,7 @@ use crate::{ services::user, }; use salvo::{ - endpoint, + oapi::endpoint, http::cookie::Cookie, oapi::extract::{JsonBody, PathParam}, Request, Response, diff --git a/src/utils/create_project.rs b/src/utils/create_project.rs index 9140568..7aa566b 100644 --- a/src/utils/create_project.rs +++ b/src/utils/create_project.rs @@ -127,7 +127,7 @@ pub fn write_project_file( "jsonwebtoken": "8.3.0", "once_cell": "1.18.0", "salvo": { - "version": "0.60", + "version": "0.63", "features": ["anyhow", "logging", "cors", "oapi", "jwt-auth", "rustls", "catch-panic","cookie","serve-static"] }, "serde": "1.0.188",