From b5e03278e1a1fc75c532b74292a58258a86f2090 Mon Sep 17 00:00:00 2001 From: veypi Date: Fri, 13 Oct 2023 17:22:59 +0800 Subject: [PATCH] add service manager --- oab/Cargo.lock | 55 ++++++++++++++++++++++++++++++++++++++++++++ oab/Cargo.toml | 1 + oab/src/api/token.rs | 2 -- oab/src/cfg.rs | 45 ++++++++++++++++++++++++++++++++---- oab/src/main.rs | 3 +++ 5 files changed, 100 insertions(+), 6 deletions(-) diff --git a/oab/Cargo.lock b/oab/Cargo.lock index 23a65ac..736313b 100644 --- a/oab/Cargo.lock +++ b/oab/Cargo.lock @@ -1009,6 +1009,26 @@ dependencies = [ "subtle", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "dotenv" version = "0.15.0" @@ -1924,6 +1944,7 @@ dependencies = [ "serde_json", "serde_repr", "serde_yaml", + "service-manager", "sqlx 0.5.13", "thiserror", "tokio", @@ -2324,6 +2345,17 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall 0.2.16", + "thiserror", +] + [[package]] name = "regex" version = "1.9.6" @@ -2794,6 +2826,17 @@ dependencies = [ "unsafe-libyaml", ] +[[package]] +name = "service-manager" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4969d3299cee0b77d9c37273997bc1e04e2b0a132eff738631a79301120defb7" +dependencies = [ + "cfg-if", + "dirs", + "which", +] + [[package]] name = "sha-1" version = "0.10.1" @@ -3790,6 +3833,18 @@ dependencies = [ "rustls-webpki", ] +[[package]] +name = "which" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" +dependencies = [ + "either", + "home", + "once_cell", + "rustix", +] + [[package]] name = "whoami" version = "1.4.1" diff --git a/oab/Cargo.toml b/oab/Cargo.toml index 39399af..459b8c5 100644 --- a/oab/Cargo.toml +++ b/oab/Cargo.toml @@ -55,4 +55,5 @@ actix-multipart = "0.6.1" actix-cors = "0.6.4" rust-embed = "8.0.0" mime_guess = "2.0.4" +service-manager = "0.3.0" diff --git a/oab/src/api/token.rs b/oab/src/api/token.rs index b18ba69..d3c451b 100644 --- a/oab/src/api/token.rs +++ b/oab/src/api/token.rs @@ -8,8 +8,6 @@ use actix_web::{get, web, Responder}; use proc::access_read; use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; -use std::time::{Duration, Instant}; -use tokio::{self}; use crate::{ models::{self, AUStatus, Token, UserPlugin}, diff --git a/oab/src/cfg.rs b/oab/src/cfg.rs index 007d82d..79f6349 100644 --- a/oab/src/cfg.rs +++ b/oab/src/cfg.rs @@ -18,7 +18,7 @@ use clap::{Args, Parser, Subcommand}; use lazy_static::lazy_static; use sea_orm::{ConnectOptions, Database, DatabaseConnection}; use sqlx::{mysql::MySqlPoolOptions, Pool}; -use tracing::Level; +use tracing::{info, Level}; use crate::Result; @@ -43,6 +43,10 @@ pub struct AppCli { #[derive(Debug, Subcommand)] pub enum Clis { Init, + Install, + Uninstall, + Start, + Stop, Web, Stash(StashData), } @@ -54,9 +58,42 @@ pub struct StashData { } impl AppCli { - fn new() -> Self { + pub fn new() -> Self { AppCli::parse() } + pub fn handle_service(&self) -> Result<()> { + let label: service_manager::ServiceLabel = "v.oa".parse().unwrap(); + + // Get generic service by detecting what is available on the platform + let manager = ::native() + .expect("Failed to detect management platform"); + + if let Some(c) = &CLI.command { + match c { + Clis::Install => { + let p = std::env::current_exe()?; + info!("deploy {}", p.to_str().unwrap()); + manager.install(service_manager::ServiceInstallCtx { + label: label.clone(), + program: p, + args: vec![], + contents: None, // Optional String for system-specific service content. + })? + } + Clis::Uninstall => manager.uninstall(service_manager::ServiceUninstallCtx { + label: label.clone(), + })?, + Clis::Start => manager.start(service_manager::ServiceStartCtx { + label: label.clone(), + })?, + Clis::Stop => manager.stop(service_manager::ServiceStopCtx { + label: label.clone(), + })?, + _ => {} + } + }; + Ok(()) + } } #[derive(Debug, serde::Serialize, serde::Deserialize, Clone)] @@ -97,7 +134,7 @@ impl AppState { let mut f = match File::open(CLI.cfg.clone()) { Ok(f) => f, Err(ref e) if e.kind() == io::ErrorKind::NotFound => { - res.connect_sqlx().unwrap(); + // res.connect_sqlx().unwrap(); return res; } Err(e) => panic!("{}", e), @@ -113,7 +150,7 @@ impl AppState { } else { println!("release_mode is enable!") } - res.connect_sqlx().unwrap(); + info!("asd"); res } pub fn defaut() -> Self { diff --git a/oab/src/main.rs b/oab/src/main.rs index cef3870..02dbd6c 100644 --- a/oab/src/main.rs +++ b/oab/src/main.rs @@ -35,6 +35,9 @@ async fn main() -> Result<()> { models::init(data).await; return Ok(()); } + Clis::Install | Clis::Uninstall | Clis::Start | Clis::Stop => { + return CLI.handle_service(); + } _ => {} }; };