Starting to work

This commit is contained in:
Thomas Forgione 2018-09-25 11:27:28 +02:00
parent 6f1d1eabb9
commit 7a216fabaf
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
3 changed files with 84 additions and 34 deletions

View File

@ -5,6 +5,7 @@ authors = ["Thomas Forgione <thomas@forgione.fr>"]
[dependencies]
lazy_static = "1.1.0"
colored = "1.6.1"
hyper = "0.12.10"
[[bin]]

View File

@ -1,13 +1,38 @@
extern crate mars;
extern crate hyper;
use std::env::current_dir;
use std::env;
use std::process::exit;
use mars::{Builder, MakeBuilder, CargoBuilder};
use hyper::Client;
use hyper::rt::{self, Future};
fn main() {
let mut current_dir = current_dir()
.expect("Cannot read current directory");
let current_dir = match env::current_dir() {
Err(e) => {
eprintln!("Couldn't find current directory: {:?}", e);
exit(1);
},
Ok(path) => {
let p = path.to_str();
p.unwrap().to_owned()
},
};
let url = format!("http://localhost:1500{}", current_dir);
println!("{:?}", url);
let url = url.parse::<hyper::Uri>().unwrap();
rt::run(fetch_url(url));
}
fn fetch_url(url: hyper::Uri) -> impl Future<Item=(), Error=()> {
let client = Client::new();
client.get(url)
.map(|_| ())
// If there was an error, let the user know...
.map_err(|err| {
eprintln!("Error {}", err);
})
}

View File

@ -1,42 +1,66 @@
#[macro_use]
extern crate lazy_static;
extern crate colored;
extern crate hyper;
extern crate mars;
use hyper::{Body, Request, Response, Server};
use std::thread;
use std::sync::{Mutex, Arc, mpsc};
use colored::*;
use hyper::{Body, Response, Server};
use hyper::service::service_fn_ok;
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();
let builder = GeneralBuilder::new();
let server = Server::bind(&addr)
.serve(move || {
service_fn_ok(|r| manage_request(r))
})
.map_err(|e| eprintln!("server error: {}", e));
let (tx, rx) = mpsc::channel();
hyper::rt::run(server);
let tasks = Arc::new(Mutex::new(vec![]));
let clone = tasks.clone();
thread::spawn(move || {
let addr = ([127, 0, 0, 1], 1500).into();
let server = Server::bind(&addr)
.serve(move || {
let clone = clone.clone();
let tx_clone = tx.clone();
let r = service_fn_ok(move |r| {
clone.lock().unwrap().push(String::from(r.uri().path()));
tx_clone.send(()).unwrap();
Response::new(Body::from("Ok"))
});
r
})
.map_err(|e| eprintln!("server error: {}", e));
println!("{}", format!("Server listening to {}", addr).bold().green());
hyper::rt::run(server);
});
for _ in rx {
let mut tasks = tasks.lock().unwrap();
while let Some(path) = tasks.pop() {
println!("{}", format!("---- STARTING BUILD ---- from {}", path).bold().green());
match builder.build(&path) {
Err(_) => {
println!("{}", "--------- FAIL ---------".bold().red());
},
Ok(_) => {
println!("{}", "----- SUCCESSFUL -----".bold().green());
},
};
println!();
}
}
}