1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
mod cargo_mod;
mod common_mod;
mod crev_mod;
mod files_mod;
mod response_get_mod;
mod response_post_mod;
mod rpc_methods_mod;
mod stdio_input_password_mod;
pub use crev_mod::unlock_crev_id_interactively;
use lazy_static::lazy_static;
use dev_bestia_simple_server::{Method, Server};
use std::sync::Mutex;
lazy_static! {
static ref CREV_UNLOCKED: Mutex<Option<crev_data::id::UnlockedId>>=Mutex::new(None);
static ref LOCAL: Mutex<Option<crev_lib::Local>>=Mutex::new(None);
}
lazy_static! {
pub static ref GREEN: String = termion::color::Fg(termion::color::Green).to_string();
pub static ref YELLOW: String = termion::color::Fg(termion::color::Yellow).to_string();
pub static ref RED: String = termion::color::Fg(termion::color::Red).to_string();
pub static ref RESET: String = termion::color::Fg(termion::color::Reset).to_string();
pub static ref CLEAR_LINE: String = termion::clear::CurrentLine.to_string();
pub static ref CLEAR_ALL: String = termion::clear::All.to_string();
pub static ref UNHIDE_CURSOR: String = termion::cursor::Show.to_string();
}
pub fn start_web_server(host: &str, port: &str) {
println!("cargo_crev_reviews server started");
let server = Server::new(|request, response_builder| {
let path = request.uri().to_string();
if !request.uri().to_string().starts_with("/cargo_crev_reviews") {
return Ok(response_get_mod::response_404_not_found(response_builder, &path));
}
match request.method() {
&Method::GET => {
let response = response_get_mod::parse_get_uri_and_response_file(&path, response_builder);
Ok(response)
}
&Method::POST => {
let request_body: &Vec<u8> = request.body();
let response_body = response_post_mod::parse_post_data_and_match_method(request_body);
match response_body {
Ok(response_body) => Ok(response_builder.body(response_body.into_bytes())?),
Err(err) => {
let response_body = response_post_mod::response_err_message(&err);
Ok(response_builder.body(response_body.into_bytes())?)
}
}
}
_ => {
let response_body = response_post_mod::response_modal_message("Unknown request method!");
Ok(response_builder.body(response_body.into_bytes())?)
}
}
});
let x = std::process::Command::new("xdg-open")
.arg("http://127.0.0.1:8182/cargo_crev_reviews/index.html")
.spawn()
.unwrap();
drop(x);
server.listen(host, port);
}