From 16ea85cfac21bdc123f68937537de022e94db273 Mon Sep 17 00:00:00 2001 From: chee Date: Wed, 14 Aug 2019 02:06:20 +0100 Subject: [PATCH] expect application subdir --- src/main.rs | 224 +++++++++--------- test/five/{ => application}/index.js | 0 test/five/{ => application}/package-lock.json | 0 test/five/{ => application}/package.json | 0 test/four/{ => application}/index.js | 0 test/four/{ => application}/package-lock.json | 0 test/four/{ => application}/package.json | 0 test/one/{ => application}/index.js | 0 test/one/{ => application}/package-lock.json | 0 test/one/{ => application}/package.json | 0 test/three/{ => application}/index.js | 0 .../three/{ => application}/package-lock.json | 0 test/three/{ => application}/package.json | 0 test/two/{ => application}/index.js | 0 test/two/{ => application}/package-lock.json | 0 test/two/{ => application}/package.json | 0 16 files changed, 112 insertions(+), 112 deletions(-) rename test/five/{ => application}/index.js (100%) rename test/five/{ => application}/package-lock.json (100%) rename test/five/{ => application}/package.json (100%) rename test/four/{ => application}/index.js (100%) rename test/four/{ => application}/package-lock.json (100%) rename test/four/{ => application}/package.json (100%) rename test/one/{ => application}/index.js (100%) rename test/one/{ => application}/package-lock.json (100%) rename test/one/{ => application}/package.json (100%) rename test/three/{ => application}/index.js (100%) rename test/three/{ => application}/package-lock.json (100%) rename test/three/{ => application}/package.json (100%) rename test/two/{ => application}/index.js (100%) rename test/two/{ => application}/package-lock.json (100%) rename test/two/{ => application}/package.json (100%) diff --git a/src/main.rs b/src/main.rs index 0233e50..bb19989 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,131 +1,131 @@ extern crate regex; -use std::os::unix::prelude::*; +use regex::Regex; use std::fs; -use std::process::{Command, Child}; -use std::os::unix::net::UnixListener; use std::io::Read; -use regex::Regex; - -fn run_npm_command (cmd: &str, path: &str, uid: u32, gid: u32) -> Result { - Command::new("npm") - .arg(cmd) - .arg("--prefix") - .arg(path) - .uid(uid) - .gid(gid) - .spawn() +use std::os::unix::net::UnixListener; +use std::os::unix::prelude::*; +use std::process::{Child, Command}; + +fn run_npm_command(cmd: &str, path: &str, uid: u32, gid: u32) -> Result { + Command::new("npm") + .arg(cmd) + .arg("--prefix") + .arg(path) + .uid(uid) + .gid(gid) + .spawn() } struct Subprocess { - uid: u32, - gid: u32, - dir: String, - process: Option + uid: u32, + gid: u32, + dir: String, + process: Option, } impl Subprocess { - fn make_process (path: &str, uid: u32, gid: u32) -> Result { - run_npm_command("start", path, uid, gid) - } - - fn new (sub: String) -> Result { - let sub_metadata = fs::metadata(&sub)?; - let uid = sub_metadata.uid(); - let gid = sub_metadata.gid(); - - let mut s = Subprocess { - uid, - gid, - dir: sub, - process: None - }; - - s.start()?; - - Ok(s) - } - - fn get_file_path (&self, file: &str) -> std::path::PathBuf { - std::path::Path::new(&format!("{}/{}", &self.dir, file)).to_owned() - } - - fn start (&mut self) -> Result<(), std::io::Error> { - fs::remove_file(self.get_file_path("sock")).unwrap_or_default(); - if let Some(process) = &mut self.process { - process.kill()?; - } - self.process = Some(Subprocess::make_process(&self.dir, self.uid, self.gid)?); - Ok(()) - } + fn make_process(&self) -> Result { + run_npm_command( + "start", + self.get_file_path("application").to_str().unwrap(), + self.uid, + self.gid, + ) + } + + fn new(sub: String) -> Result { + let sub_metadata = fs::metadata(&sub)?; + let uid = sub_metadata.uid(); + let gid = sub_metadata.gid(); + + let mut s = Subprocess { + uid, + gid, + dir: sub, + process: None, + }; + + s.start()?; + + Ok(s) + } + + fn get_file_path(&self, file: &str) -> std::path::PathBuf { + std::path::Path::new(&format!("{}/{}", &self.dir, file)).to_owned() + } + + fn start(&mut self) -> Result<(), std::io::Error> { + fs::remove_file(self.get_file_path("application/sock")).unwrap_or_default(); + if let Some(process) = &mut self.process { + process.kill()?; + } + self.process = Some(self.make_process()?); + Ok(()) + } } struct Msg<'a> { - command: &'a str, - sub: &'a str + command: &'a str, + sub: &'a str, } -fn parse_msg (msg: &str) -> Option { - let msg_regex = Regex::new(r"(\S+) (\S+)").unwrap(); - msg_regex.captures(msg).map(|capture| { - let groups = (capture.get(1), capture.get(2)); - match groups { - (Some(command), Some(name)) => - Some(Msg { - command: command.as_str(), - sub: name.as_str() - }), - _ => None - } - })? +fn parse_msg(msg: &str) -> Option { + let msg_regex = Regex::new(r"(\S+) (\S+)").unwrap(); + msg_regex.captures(msg).map(|capture| { + let groups = (capture.get(1), capture.get(2)); + match groups { + (Some(command), Some(name)) => Some(Msg { + command: command.as_str(), + sub: name.as_str(), + }), + _ => None, + } + })? } fn main() -> Result<(), std::io::Error> { - let subdirectories = fs::read_dir(".")?; - - let mut processes = std::collections::HashMap::new(); - - for sub in subdirectories { - let sub = sub?; - let path = sub.path(); - let process = Subprocess::new(path.to_str().unwrap().to_string())?; - if let Some(name) = path.file_name() { - processes.insert(name.to_str().unwrap().to_string(), process); - } - } - - let sock_path = "./subsocket"; - fs::remove_file(sock_path).unwrap_or_default(); - let sock = UnixListener::bind(sock_path)?; - for stream in sock.incoming() { - let mut buf = vec![]; - let count = stream?.read_to_end(&mut buf)?; - println!("count: {:?}", count); - let string = String::from_utf8(buf).unwrap(); - let msg = parse_msg(string.as_str()); - match msg { - Some(msg) => { - let sub = processes.get_mut(msg.sub); - match sub { - Some(sub) => { - if msg.command == "restart" { - sub.start()?; - } else { - println!("recieved unusual command: {}", msg.command) - } - }, - None => { - println!("recieved unusual person: {}", msg.sub) - } - } - }, - None => { - println!( - "recieved unusual message. message should be . got: {}", - string - ) - } - } - } - Ok(()) + let subdirectories = fs::read_dir(".")?; + + let mut processes = std::collections::HashMap::new(); + + for sub in subdirectories { + let sub = sub?; + let path = sub.path(); + let process = Subprocess::new(path.to_str().unwrap().to_string())?; + if let Some(name) = path.file_name() { + processes.insert(name.to_str().unwrap().to_string(), process); + } + } + + let sock_path = "./subsocket"; + fs::remove_file(sock_path).unwrap_or_default(); + let sock = UnixListener::bind(sock_path)?; + for stream in sock.incoming() { + let mut buf = vec![]; + let count = stream?.read_to_end(&mut buf)?; + println!("count: {:?}", count); + let string = String::from_utf8(buf).unwrap(); + let msg = parse_msg(string.as_str()); + match msg { + Some(msg) => { + let sub = processes.get_mut(msg.sub); + match sub { + Some(sub) => { + if msg.command == "restart" { + sub.start()?; + } else { + println!("recieved unusual command: {}", msg.command) + } + } + None => println!("recieved unusual person: {}", msg.sub), + } + } + None => println!( + "recieved unusual message. message should be . got: {}", + string + ), + } + } + Ok(()) } diff --git a/test/five/index.js b/test/five/application/index.js similarity index 100% rename from test/five/index.js rename to test/five/application/index.js diff --git a/test/five/package-lock.json b/test/five/application/package-lock.json similarity index 100% rename from test/five/package-lock.json rename to test/five/application/package-lock.json diff --git a/test/five/package.json b/test/five/application/package.json similarity index 100% rename from test/five/package.json rename to test/five/application/package.json diff --git a/test/four/index.js b/test/four/application/index.js similarity index 100% rename from test/four/index.js rename to test/four/application/index.js diff --git a/test/four/package-lock.json b/test/four/application/package-lock.json similarity index 100% rename from test/four/package-lock.json rename to test/four/application/package-lock.json diff --git a/test/four/package.json b/test/four/application/package.json similarity index 100% rename from test/four/package.json rename to test/four/application/package.json diff --git a/test/one/index.js b/test/one/application/index.js similarity index 100% rename from test/one/index.js rename to test/one/application/index.js diff --git a/test/one/package-lock.json b/test/one/application/package-lock.json similarity index 100% rename from test/one/package-lock.json rename to test/one/application/package-lock.json diff --git a/test/one/package.json b/test/one/application/package.json similarity index 100% rename from test/one/package.json rename to test/one/application/package.json diff --git a/test/three/index.js b/test/three/application/index.js similarity index 100% rename from test/three/index.js rename to test/three/application/index.js diff --git a/test/three/package-lock.json b/test/three/application/package-lock.json similarity index 100% rename from test/three/package-lock.json rename to test/three/application/package-lock.json diff --git a/test/three/package.json b/test/three/application/package.json similarity index 100% rename from test/three/package.json rename to test/three/application/package.json diff --git a/test/two/index.js b/test/two/application/index.js similarity index 100% rename from test/two/index.js rename to test/two/application/index.js diff --git a/test/two/package-lock.json b/test/two/application/package-lock.json similarity index 100% rename from test/two/package-lock.json rename to test/two/application/package-lock.json diff --git a/test/two/package.json b/test/two/application/package.json similarity index 100% rename from test/two/package.json rename to test/two/application/package.json