add service manager

master
veypi 1 year ago
parent 25e6eb135c
commit b5e03278e1

55
oab/Cargo.lock generated

@ -1009,6 +1009,26 @@ dependencies = [
"subtle", "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]] [[package]]
name = "dotenv" name = "dotenv"
version = "0.15.0" version = "0.15.0"
@ -1924,6 +1944,7 @@ dependencies = [
"serde_json", "serde_json",
"serde_repr", "serde_repr",
"serde_yaml", "serde_yaml",
"service-manager",
"sqlx 0.5.13", "sqlx 0.5.13",
"thiserror", "thiserror",
"tokio", "tokio",
@ -2324,6 +2345,17 @@ dependencies = [
"bitflags 1.3.2", "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]] [[package]]
name = "regex" name = "regex"
version = "1.9.6" version = "1.9.6"
@ -2794,6 +2826,17 @@ dependencies = [
"unsafe-libyaml", "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]] [[package]]
name = "sha-1" name = "sha-1"
version = "0.10.1" version = "0.10.1"
@ -3790,6 +3833,18 @@ dependencies = [
"rustls-webpki", "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]] [[package]]
name = "whoami" name = "whoami"
version = "1.4.1" version = "1.4.1"

@ -55,4 +55,5 @@ actix-multipart = "0.6.1"
actix-cors = "0.6.4" actix-cors = "0.6.4"
rust-embed = "8.0.0" rust-embed = "8.0.0"
mime_guess = "2.0.4" mime_guess = "2.0.4"
service-manager = "0.3.0"

@ -8,8 +8,6 @@
use actix_web::{get, web, Responder}; use actix_web::{get, web, Responder};
use proc::access_read; use proc::access_read;
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
use std::time::{Duration, Instant};
use tokio::{self};
use crate::{ use crate::{
models::{self, AUStatus, Token, UserPlugin}, models::{self, AUStatus, Token, UserPlugin},

@ -18,7 +18,7 @@ use clap::{Args, Parser, Subcommand};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use sea_orm::{ConnectOptions, Database, DatabaseConnection}; use sea_orm::{ConnectOptions, Database, DatabaseConnection};
use sqlx::{mysql::MySqlPoolOptions, Pool}; use sqlx::{mysql::MySqlPoolOptions, Pool};
use tracing::Level; use tracing::{info, Level};
use crate::Result; use crate::Result;
@ -43,6 +43,10 @@ pub struct AppCli {
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
pub enum Clis { pub enum Clis {
Init, Init,
Install,
Uninstall,
Start,
Stop,
Web, Web,
Stash(StashData), Stash(StashData),
} }
@ -54,9 +58,42 @@ pub struct StashData {
} }
impl AppCli { impl AppCli {
fn new() -> Self { pub fn new() -> Self {
AppCli::parse() 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 = <dyn service_manager::ServiceManager>::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)] #[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
@ -97,7 +134,7 @@ impl AppState {
let mut f = match File::open(CLI.cfg.clone()) { let mut f = match File::open(CLI.cfg.clone()) {
Ok(f) => f, Ok(f) => f,
Err(ref e) if e.kind() == io::ErrorKind::NotFound => { Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
res.connect_sqlx().unwrap(); // res.connect_sqlx().unwrap();
return res; return res;
} }
Err(e) => panic!("{}", e), Err(e) => panic!("{}", e),
@ -113,7 +150,7 @@ impl AppState {
} else { } else {
println!("release_mode is enable!") println!("release_mode is enable!")
} }
res.connect_sqlx().unwrap(); info!("asd");
res res
} }
pub fn defaut() -> Self { pub fn defaut() -> Self {

@ -35,6 +35,9 @@ async fn main() -> Result<()> {
models::init(data).await; models::init(data).await;
return Ok(()); return Ok(());
} }
Clis::Install | Clis::Uninstall | Clis::Start | Clis::Stop => {
return CLI.handle_service();
}
_ => {} _ => {}
}; };
}; };

Loading…
Cancel
Save