From c7b6191ff189ad0b0806bb3b85312e445b55e87d Mon Sep 17 00:00:00 2001 From: "benjamin.747" Date: Mon, 21 Oct 2024 16:05:33 +0800 Subject: [PATCH] fix duplicate initialization --- Cargo.toml | 11 +++++++---- gateway/src/https_server.rs | 22 +++++++++------------- mega/src/commands/service/http.rs | 10 ++++++++-- mega/src/commands/service/https.rs | 9 ++++++++- mega/src/commands/service/multi.rs | 12 ++++++++---- mega/src/commands/service/ssh.rs | 9 ++++++++- mono/src/commands/service/http.rs | 9 ++++++++- mono/src/commands/service/https.rs | 9 ++++++++- mono/src/commands/service/multi.rs | 15 +++++++++++---- mono/src/commands/service/ssh.rs | 9 ++++++++- mono/src/server/https_server.rs | 14 ++++++-------- mono/src/server/ssh_server.rs | 5 +---- 12 files changed, 90 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9cdb44309..d77f221fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,9 +35,9 @@ mega = { path = "mega" } mono = { path = "mono" } libra = { path = "libra" } -anyhow = "1.0.89" +anyhow = "1.0.90" serde = "1.0.210" -serde_json = "1.0.128" +serde_json = "1.0.132" tracing = "0.1.40" tracing-subscriber = "0.3.18" tracing-appender = "0.2" @@ -65,7 +65,7 @@ axum-server = "0.7.1" tower-http = "0.6.1" tower = "0.5.1" hex = "0.4.3" -sea-orm = "1.0.1" +sea-orm = "1.1.0" flate2 = "1.0.34" bstr = "1.10.0" colored = "2.1.0" @@ -75,7 +75,7 @@ config = "0.14.0" shadow-rs = "0.35.1" reqwest = "0.12.8" lazy_static = "1.5.0" -uuid = "1.10.0" +uuid = "1.11.0" regex = "1.11.0" ed25519-dalek = "2.1.1" ctrlc = "3.4.4" @@ -85,3 +85,6 @@ home = "0.5.9" ring = "0.17.8" cedar-policy = "4.2.1" secp256k1 = "0.30.0" + +[profile.release] +debug = true \ No newline at end of file diff --git a/gateway/src/https_server.rs b/gateway/src/https_server.rs index e4f72d8dd..54addf2e1 100644 --- a/gateway/src/https_server.rs +++ b/gateway/src/https_server.rs @@ -14,7 +14,6 @@ use tower_http::cors::{Any, CorsLayer}; use tower_http::decompression::RequestDecompressionLayer; use tower_http::trace::TraceLayer; -use common::config::Config; use common::model::{CommonOptions, ZtmOptions}; use gemini::ztm::agent::{run_ztm_client, LocalZTMAgent}; use jupiter::context::Context; @@ -54,7 +53,7 @@ pub struct HttpsOptions { pub https_cert_path: PathBuf, } -pub async fn https_server(config: Config, options: HttpsOptions) { +pub async fn https_server(context: Context, options: HttpsOptions) { let HttpsOptions { common: CommonOptions { host, .. }, https_key_path, @@ -63,10 +62,10 @@ pub async fn https_server(config: Config, options: HttpsOptions) { ztm, } = options.clone(); - check_run_with_ztm(config.clone(), options.ztm.clone(), https_port); + check_run_with_ztm(context.clone(), options.ztm.clone(), https_port); let app = app( - config, + context, host.clone(), https_port, options.common.clone(), @@ -85,17 +84,17 @@ pub async fn https_server(config: Config, options: HttpsOptions) { .unwrap(); } -pub async fn http_server(config: Config, options: HttpOptions) { +pub async fn http_server(context: Context, options: HttpOptions) { let HttpOptions { common: CommonOptions { host, .. }, http_port, ztm, } = options.clone(); - check_run_with_ztm(config.clone(), options.ztm.clone(), http_port); + check_run_with_ztm(context.clone(), options.ztm.clone(), http_port); let app = app( - config, + context, host.clone(), http_port, options.common.clone(), @@ -113,14 +112,12 @@ pub async fn http_server(config: Config, options: HttpOptions) { } pub async fn app( - config: Config, + context: Context, host: String, port: u16, common: CommonOptions, ztm: ZtmOptions, ) -> Router { - let context = Context::new(config.clone()).await; - context.services.mono_storage.init_monorepo(&config.monorepo).await; let state = AppState { host, port, @@ -182,7 +179,7 @@ pub async fn app( .with_state(state) } -pub fn check_run_with_ztm(config: Config, ztm: ZtmOptions, http_port: u16) { +pub fn check_run_with_ztm(context: Context, ztm: ZtmOptions, http_port: u16) { //Mega server join a ztm mesh match ztm.bootstrap_node { Some(bootstrap_node) => { @@ -198,7 +195,7 @@ pub fn check_run_with_ztm(config: Config, ztm: ZtmOptions, http_port: u16) { thread::sleep(time::Duration::from_secs(3)); let bootstrap_node_clone = bootstrap_node.clone(); - let config_clone = config.clone(); + let config_clone = context.config.clone(); let ztm_agent_clone = ztm_agent.clone(); tokio::spawn(async move { run_ztm_client( @@ -214,7 +211,6 @@ pub fn check_run_with_ztm(config: Config, ztm: ZtmOptions, http_port: u16) { if ztm.cache_repo { thread::sleep(time::Duration::from_secs(3)); tokio::spawn(async move { - let context = Context::new(config.clone()).await; cache_public_repository(bootstrap_node, context, ztm_agent).await }); } diff --git a/mega/src/commands/service/http.rs b/mega/src/commands/service/http.rs index 367f6afee..d8d07980b 100644 --- a/mega/src/commands/service/http.rs +++ b/mega/src/commands/service/http.rs @@ -2,7 +2,7 @@ use clap::{ArgMatches, Args, Command, FromArgMatches}; use common::{config::Config, errors::MegaResult}; use gateway::https_server::{self, HttpOptions}; - +use jupiter::context::Context; pub fn cli() -> Command { HttpOptions::augment_args_for_update(Command::new("http").about("Start Mega HTTP server")) @@ -14,7 +14,13 @@ pub(crate) async fn exec(config: Config, args: &ArgMatches) -> MegaResult { .unwrap(); tracing::info!("{server_matchers:#?}"); - https_server::http_server(config, server_matchers).await; + let context = Context::new(config.clone()).await; + context + .services + .mono_storage + .init_monorepo(&config.monorepo) + .await; + https_server::http_server(context, server_matchers).await; Ok(()) } diff --git a/mega/src/commands/service/https.rs b/mega/src/commands/service/https.rs index 1e4001a88..f6709c680 100644 --- a/mega/src/commands/service/https.rs +++ b/mega/src/commands/service/https.rs @@ -2,6 +2,7 @@ use clap::{ArgMatches, Args, Command, FromArgMatches}; use common::{config::Config, errors::MegaResult}; use gateway::https_server::{self, HttpsOptions}; +use jupiter::context::Context; pub fn cli() -> Command { @@ -14,7 +15,13 @@ pub(crate) async fn exec(config: Config, args: &ArgMatches) -> MegaResult { .unwrap(); tracing::info!("{server_matchers:#?}"); - https_server::https_server(config, server_matchers).await; + let context = Context::new(config.clone()).await; + context + .services + .mono_storage + .init_monorepo(&config.monorepo) + .await; + https_server::https_server(context, server_matchers).await; Ok(()) } diff --git a/mega/src/commands/service/multi.rs b/mega/src/commands/service/multi.rs index 1c40a3a55..0116c3540 100644 --- a/mega/src/commands/service/multi.rs +++ b/mega/src/commands/service/multi.rs @@ -8,6 +8,7 @@ use common::{ model::{CommonOptions, ZtmOptions}, }; use gateway::https_server::{self, HttpOptions, HttpsOptions}; +use jupiter::context::Context; use mono::server::ssh_server::{self, SshCustom, SshOptions}; #[derive(Debug, PartialEq, Clone, ValueEnum)] @@ -58,14 +59,17 @@ pub(crate) async fn exec(config: Config, args: &ArgMatches) -> MegaResult { let service_type = server_matchers.service; - let config_clone = config.clone(); + let context = Context::new(config.clone()).await; + context.services.mono_storage.init_monorepo(&config.monorepo).await; + + let context_clone = context.clone(); let http_server = if service_type.contains(&StartCommand::Http) { let http = HttpOptions { common: server_matchers.common.clone(), http_port: server_matchers.http_port, ztm: server_matchers.ztm, }; - tokio::spawn(async move { https_server::http_server(config_clone, http).await }) + tokio::spawn(async move { https_server::http_server(context_clone, http).await }) } else if service_type.contains(&StartCommand::Https) { let https = HttpsOptions { common: server_matchers.common.clone(), @@ -74,7 +78,7 @@ pub(crate) async fn exec(config: Config, args: &ArgMatches) -> MegaResult { https_cert_path: server_matchers.https_cert_path.unwrap(), ztm: server_matchers.ztm, }; - tokio::spawn(async move { https_server::https_server(config_clone, https).await }) + tokio::spawn(async move { https_server::https_server(context_clone, https).await }) } else { tokio::task::spawn(async {}) }; @@ -84,7 +88,7 @@ pub(crate) async fn exec(config: Config, args: &ArgMatches) -> MegaResult { common: server_matchers.common.clone(), custom: server_matchers.ssh, }; - tokio::spawn(async move { ssh_server::start_server(config, &ssh).await }) + tokio::spawn(async move { ssh_server::start_server(context, &ssh).await }) } else { tokio::task::spawn(async {}) }; diff --git a/mega/src/commands/service/ssh.rs b/mega/src/commands/service/ssh.rs index a13853b52..09c85905a 100644 --- a/mega/src/commands/service/ssh.rs +++ b/mega/src/commands/service/ssh.rs @@ -2,6 +2,7 @@ use clap::{ArgMatches, Args, Command, FromArgMatches}; use common::config::Config; use common::errors::MegaResult; +use jupiter::context::Context; use mono::server::ssh_server::start_server; use mono::server::ssh_server::SshOptions; @@ -14,7 +15,13 @@ pub(crate) async fn exec(config: Config, args: &ArgMatches) -> MegaResult { .map_err(|err| err.exit()) .unwrap(); tracing::info!("{server_matchers:#?}"); - start_server(config, &server_matchers).await; + let context = Context::new(config.clone()).await; + context + .services + .mono_storage + .init_monorepo(&config.monorepo) + .await; + start_server(context, &server_matchers).await; Ok(()) } diff --git a/mono/src/commands/service/http.rs b/mono/src/commands/service/http.rs index da05550f1..094e54df6 100644 --- a/mono/src/commands/service/http.rs +++ b/mono/src/commands/service/http.rs @@ -1,6 +1,7 @@ use clap::{ArgMatches, Args, Command, FromArgMatches}; use common::{config::Config, errors::MegaResult}; +use jupiter::context::Context; use crate::server::https_server::{self, HttpOptions}; @@ -15,7 +16,13 @@ pub(crate) async fn exec(config: Config, args: &ArgMatches) -> MegaResult { .unwrap(); tracing::info!("{server_matchers:#?}"); - https_server::start_http(config, server_matchers).await; + let context = Context::new(config.clone()).await; + context + .services + .mono_storage + .init_monorepo(&config.monorepo) + .await; + https_server::start_http(context, server_matchers).await; Ok(()) } diff --git a/mono/src/commands/service/https.rs b/mono/src/commands/service/https.rs index f10b48938..15a60de65 100644 --- a/mono/src/commands/service/https.rs +++ b/mono/src/commands/service/https.rs @@ -1,6 +1,7 @@ use clap::{ArgMatches, Args, Command, FromArgMatches}; use common::{config::Config, errors::MegaResult}; +use jupiter::context::Context; use crate::server::https_server::{start_https, HttpsOptions}; @@ -14,7 +15,13 @@ pub(crate) async fn exec(config: Config, args: &ArgMatches) -> MegaResult { .unwrap(); tracing::info!("{server_matchers:#?}"); - start_https(config, server_matchers).await; + let context = Context::new(config.clone()).await; + context + .services + .mono_storage + .init_monorepo(&config.monorepo) + .await; + start_https(context, server_matchers).await; Ok(()) } diff --git a/mono/src/commands/service/multi.rs b/mono/src/commands/service/multi.rs index 33a28a2f7..ab2439d16 100644 --- a/mono/src/commands/service/multi.rs +++ b/mono/src/commands/service/multi.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use clap::{ArgMatches, Args, Command, FromArgMatches, ValueEnum}; +use jupiter::context::Context; use crate::server::{ https_server::{self, HttpOptions, HttpsOptions}, @@ -53,13 +54,19 @@ pub(crate) async fn exec(config: Config, args: &ArgMatches) -> MegaResult { let service_type = server_matchers.service; - let config_clone = config.clone(); + let context = Context::new(config.clone()).await; + context + .services + .mono_storage + .init_monorepo(&config.monorepo) + .await; + let context_clone = context.clone(); let http_server = if service_type.contains(&StartCommand::Http) { let http = HttpOptions { common: server_matchers.common.clone(), http_port: server_matchers.http_port, }; - tokio::spawn(async move { https_server::start_http(config_clone, http).await }) + tokio::spawn(async move { https_server::start_http(context_clone, http).await }) } else if service_type.contains(&StartCommand::Https) { let https = HttpsOptions { common: server_matchers.common.clone(), @@ -67,7 +74,7 @@ pub(crate) async fn exec(config: Config, args: &ArgMatches) -> MegaResult { https_key_path: server_matchers.https_key_path.unwrap(), https_cert_path: server_matchers.https_cert_path.unwrap(), }; - tokio::spawn(async move { https_server::start_https(config_clone, https).await }) + tokio::spawn(async move { https_server::start_https(context_clone, https).await }) } else { panic!("start params should provide! run like 'mega service multi http https'") }; @@ -77,7 +84,7 @@ pub(crate) async fn exec(config: Config, args: &ArgMatches) -> MegaResult { common: server_matchers.common.clone(), custom: server_matchers.ssh, }; - tokio::spawn(async move { ssh_server::start_server(config, &ssh).await }) + tokio::spawn(async move { ssh_server::start_server(context, &ssh).await }) } else { tokio::task::spawn(async {}) }; diff --git a/mono/src/commands/service/ssh.rs b/mono/src/commands/service/ssh.rs index 8263c2f87..7048a9367 100644 --- a/mono/src/commands/service/ssh.rs +++ b/mono/src/commands/service/ssh.rs @@ -2,6 +2,7 @@ use clap::{ArgMatches, Args, Command, FromArgMatches}; use common::config::Config; use common::errors::MegaResult; +use jupiter::context::Context; use crate::server::ssh_server::{start_server, SshOptions}; @@ -14,7 +15,13 @@ pub(crate) async fn exec(config: Config, args: &ArgMatches) -> MegaResult { .map_err(|err| err.exit()) .unwrap(); tracing::info!("{server_matchers:#?}"); - start_server(config, &server_matchers).await; + let context = Context::new(config.clone()).await; + context + .services + .mono_storage + .init_monorepo(&config.monorepo) + .await; + start_server(context, &server_matchers).await; Ok(()) } diff --git a/mono/src/server/https_server.rs b/mono/src/server/https_server.rs index 8e72b7416..fd1e1f138 100644 --- a/mono/src/server/https_server.rs +++ b/mono/src/server/https_server.rs @@ -20,7 +20,6 @@ use tower_http::decompression::RequestDecompressionLayer; use tower_http::trace::TraceLayer; use ceres::protocol::{ServiceType, SmartProtocol, TransportProtocol}; -use common::config::Config; use common::errors::ProtocolError; use common::model::{CommonOptions, InfoRefsParams}; use jupiter::context::Context; @@ -66,7 +65,7 @@ pub fn remove_git_suffix(uri: Uri, git_suffix: &str) -> PathBuf { PathBuf::from(uri.path().replace(".git", "").replace(git_suffix, "")) } -pub async fn start_https(config: Config, options: HttpsOptions) { +pub async fn start_https(context: Context, options: HttpsOptions) { let HttpsOptions { common: CommonOptions { host, .. }, https_key_path, @@ -74,7 +73,7 @@ pub async fn start_https(config: Config, options: HttpsOptions) { https_port, } = options.clone(); - let app = app(config, host.clone(), https_port, options.common.clone()).await; + let app = app(context, host.clone(), https_port, options.common.clone()).await; let server_url = format!("{}:{}", host, https_port); let addr = SocketAddr::from_str(&server_url).unwrap(); @@ -87,13 +86,13 @@ pub async fn start_https(config: Config, options: HttpsOptions) { .unwrap(); } -pub async fn start_http(config: Config, options: HttpOptions) { +pub async fn start_http(context: Context, options: HttpOptions) { let HttpOptions { common: CommonOptions { host, .. }, http_port, } = options.clone(); - let app = app(config, host.clone(), http_port, options.common.clone()).await; + let app = app(context, host.clone(), http_port, options.common.clone()).await; let server_url = format!("{}:{}", host, http_port); @@ -133,9 +132,8 @@ pub async fn start_http(config: Config, options: HttpOptions) { /// - GET end of `Regex::new(r"/info/refs$")` /// - POST end of `Regex::new(r"/git-upload-pack$")` /// - POST end of `Regex::new(r"/git-receive-pack$")` -pub async fn app(config: Config, host: String, port: u16, common: CommonOptions) -> Router { - let context = Context::new(config.clone()).await; - context.services.mono_storage.init_monorepo(&config.monorepo).await; +pub async fn app(context: Context, host: String, port: u16, common: CommonOptions) -> Router { + let state = AppState { host, port, diff --git a/mono/src/server/ssh_server.rs b/mono/src/server/ssh_server.rs index 392f02f43..710dbd044 100644 --- a/mono/src/server/ssh_server.rs +++ b/mono/src/server/ssh_server.rs @@ -6,7 +6,6 @@ use std::sync::Arc; use bytes::BytesMut; use clap::Args; -use common::config::Config; use ed25519_dalek::pkcs8::spki::der::pem::LineEnding; use ed25519_dalek::pkcs8::{DecodePrivateKey, EncodePrivateKey}; use ed25519_dalek::SigningKey; @@ -36,7 +35,7 @@ pub struct SshCustom { } /// start a ssh server -pub async fn start_server(config: Config, command: &SshOptions) { +pub async fn start_server(context: Context, command: &SshOptions) { // we need to persist the key to prevent key expired after server restart. let client_key = load_key(); let client_pubkey = Arc::new(client_key.clone_public_key().unwrap()); @@ -58,8 +57,6 @@ pub async fn start_server(config: Config, command: &SshOptions) { common: CommonOptions { host, .. }, custom: SshCustom { ssh_port }, } = command; - let context = Context::new(config.clone()).await; - context.services.mono_storage.init_monorepo(&config.monorepo).await; let mut ssh_server = SshServer { client_pubkey, clients: Arc::new(Mutex::new(HashMap::new())),