Working on server
This commit is contained in:
parent
4ef065318c
commit
6f1d1eabb9
|
@ -4,6 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Thomas Forgione <thomas@forgione.fr>"]
|
||||
|
||||
[dependencies]
|
||||
lazy_static = "1.1.0"
|
||||
hyper = "0.12.10"
|
||||
|
||||
[[bin]]
|
||||
|
|
93
src/lib.rs
93
src/lib.rs
|
@ -40,7 +40,7 @@ pub fn run_command_with_args<I, S>(command: &str, path: &str, args: I) -> Result
|
|||
}
|
||||
|
||||
/// Tries to build a certain directory using the specified builders.
|
||||
pub fn build(path: &PathBuf, builders: Vec<Box<Builder>>) -> Result<(), ()> {
|
||||
pub fn build(path: &PathBuf, builders: &Vec<Builder>) -> Result<(), ()> {
|
||||
|
||||
let mut path = path.clone();
|
||||
|
||||
|
@ -51,7 +51,7 @@ pub fn build(path: &PathBuf, builders: Vec<Box<Builder>>) -> 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<T, E>(result: Result<T, E>) -> 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<Builder>,
|
||||
}
|
||||
|
||||
/// 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<String>,
|
||||
}
|
||||
|
||||
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 {
|
||||
/// 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, "Makefile")
|
||||
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()))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Body>) -> Response<Body> {
|
||||
|
||||
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));
|
||||
.serve(move || {
|
||||
service_fn_ok(|r| manage_request(r))
|
||||
})
|
||||
.map_err(|e| eprintln!("server error: {}", e));
|
||||
|
||||
rt::spawn(server);
|
||||
Ok(())
|
||||
}));
|
||||
hyper::rt::run(server);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue