From 6f1d1eabb942bb4956f30553cb3e7442d5d26871 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Mon, 24 Sep 2018 17:55:05 +0200 Subject: [PATCH] Working on server --- Cargo.toml | 1 + src/lib.rs | 95 ++++++++++++++++++++++++++++----------------------- src/server.rs | 41 +++++++++++++++++----- 3 files changed, 86 insertions(+), 51 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6c31daa..6d84209 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Thomas Forgione "] [dependencies] +lazy_static = "1.1.0" hyper = "0.12.10" [[bin]] diff --git a/src/lib.rs b/src/lib.rs index 54b882f..85d9fa8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,7 @@ pub fn run_command_with_args(command: &str, path: &str, args: I) -> Result } /// Tries to build a certain directory using the specified builders. -pub fn build(path: &PathBuf, builders: Vec>) -> Result<(), ()> { +pub fn build(path: &PathBuf, builders: &Vec) -> Result<(), ()> { let mut path = path.clone(); @@ -51,7 +51,7 @@ pub fn build(path: &PathBuf, builders: Vec>) -> Result<(), ()> { return Err(()); } - for builder in &builders { + for builder in builders { if builder.is_buildable(path.to_str().unwrap()) { builder.build(path.to_str().unwrap())?; @@ -73,63 +73,74 @@ pub fn destroy(result: Result) -> Result<(), ()> { } } -/// A generic builder. It can build some projects. -pub trait Builder { - /// Checks if a directory has corresponding files so it can build it. - /// e.g.: if there is a Makefile - fn is_buildable(&self, path: &str) -> bool; +/// A general builders that contains many builders and can build any type of code. +#[derive(Clone)] +pub struct GeneralBuilder { + /// The builders contained. + builders: Vec, +} - /// Trigger all the commands to build the project. - fn build(&self, path: &str) -> Result<(), ()>; +impl GeneralBuilder { + /// Creates a new general builder with the defaults builders. + pub fn new() -> GeneralBuilder { + GeneralBuilder { + builders: vec![ + Builder::make(), + Builder::cargo(), + ], + } + } + + /// Triggers a build. + pub fn build(&self, path: &str) -> Result<(), ()> { + build(&PathBuf::from(path), &self.builders) + } } /// The builder that looks for makefiles. -pub struct MakeBuilder { +#[derive(Clone)] +pub struct Builder { + /// The file to lookup to check build. + file: String, + /// The command to launch to build. + command: String, + + /// The arguments to the command. + args: Vec, } -impl MakeBuilder { +impl Builder { /// Creates a new make builder. - pub fn new() -> MakeBuilder { - MakeBuilder { - + pub fn make() -> Builder { + Builder { + file: "Makefile".to_string(), + command: "make".to_string(), + args: vec![], } } -} -impl Builder for MakeBuilder { - fn is_buildable(&self, path: &str) -> bool { - contains_file(path, "Makefile") + /// Creates a new cargo builder. + pub fn cargo() -> Builder { + Builder { + file: "Cargo.toml".to_string(), + command: "cargo".to_string(), + args: vec!["build".to_string()], + } } + /// Checks if a directory has corresponding files so it can build it. + /// e.g.: if there is a Makefile + fn is_buildable(&self, path: &str) -> bool { + contains_file(path, &self.file) + } + + /// Trigger all the commands to build the project. fn build(&self, path: &str) -> Result<(), ()> { - match run_command("make", path) { + match run_command_with_args(&self.command, path, &self.args) { Ok(_) => Ok(()), Err(_) => Err(()), } } } -/// The builder that looks for Cargo.toml. -pub struct CargoBuilder { - -} - -impl CargoBuilder { - /// Creates a new cargo builder. - pub fn new() -> CargoBuilder { - CargoBuilder { - - } - } -} - -impl Builder for CargoBuilder { - fn is_buildable(&self, path: &str) -> bool { - contains_file(path, "Cargo.toml") - } - - fn build(&self, path: &str) -> Result<(), ()> { - destroy(run_command_with_args("cargo", path, ["build"].iter())) - } -} diff --git a/src/server.rs b/src/server.rs index f97b451..005f205 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,19 +1,42 @@ +#[macro_use] +extern crate lazy_static; extern crate hyper; +extern crate mars; -use hyper::{Body, Response, Server}; +use hyper::{Body, Request, Response, Server}; use hyper::service::service_fn_ok; -use hyper::rt::{self, Future}; +use hyper::rt::Future; + +use mars::GeneralBuilder; + +lazy_static! { + static ref BUILDER: GeneralBuilder = { + GeneralBuilder::new() + }; +} + +fn manage_request(request: Request) -> Response { + + match BUILDER.build(request.uri().path()) { + Err(error) => { + eprintln!("Errror while treating the following request:\n{:?}\n{:?}", + request, error); + }, + Ok(_) => (), + } + + Response::new(Body::from("Hello")) +} fn main() { let addr = ([127, 0, 0, 1], 1500).into(); - rt::run(rt::lazy(move || { - let server = Server::bind(&addr) - .serve(|| service_fn_ok(|_| Response::new(Body::from("Hello world")))) - .map_err(|e| eprintln!("server 1 error: {}", e)); + let server = Server::bind(&addr) + .serve(move || { + service_fn_ok(|r| manage_request(r)) + }) + .map_err(|e| eprintln!("server error: {}", e)); - rt::spawn(server); - Ok(()) - })); + hyper::rt::run(server); }