2022-12-04 15:02:07 +01:00
|
|
|
#[macro_use]
|
|
|
|
|
extern crate rocket;
|
|
|
|
|
|
2022-12-08 13:06:19 +01:00
|
|
|
mod parser;
|
|
|
|
|
#[cfg(feature = "static")]
|
|
|
|
|
mod embed;
|
|
|
|
|
|
2022-12-09 15:44:27 +01:00
|
|
|
use rocket::error::ErrorKind;
|
2022-12-04 15:02:07 +01:00
|
|
|
use rocket::serde::json::Json;
|
2022-12-09 23:18:13 +01:00
|
|
|
use rocket::config::{Config};
|
2022-12-08 13:06:19 +01:00
|
|
|
#[cfg(feature = "static")]
|
|
|
|
|
use crate::embed::CustomHandler;
|
2022-12-05 23:18:32 +01:00
|
|
|
use crate::parser::lsblk_parser::{Disk, parse_lsblk};
|
2022-12-05 00:03:22 +01:00
|
|
|
use crate::parser::mdstat_parser::{MdRaidSystem, parse_mdstat};
|
2022-12-04 15:02:07 +01:00
|
|
|
|
|
|
|
|
#[get("/raiddevices")]
|
|
|
|
|
fn get_raid_devices() -> Json<MdRaidSystem> {
|
|
|
|
|
Json(parse_mdstat())
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 23:18:32 +01:00
|
|
|
#[get("/disks")]
|
|
|
|
|
fn get_disks() -> Json<Vec<Disk>> {
|
|
|
|
|
Json(parse_lsblk())
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 15:02:07 +01:00
|
|
|
#[rocket::main]
|
|
|
|
|
async fn main() -> Result<(), rocket::Error> {
|
2022-12-09 15:44:27 +01:00
|
|
|
println!("init server");
|
|
|
|
|
println!("access at: http://127.0.0.1:8000/");
|
2022-12-09 23:18:13 +01:00
|
|
|
|
|
|
|
|
let mut cfg = Config::default();
|
|
|
|
|
cfg.address = "0.0.0.0".parse().unwrap();
|
|
|
|
|
|
|
|
|
|
let b = rocket::custom(cfg);
|
2022-12-08 13:06:19 +01:00
|
|
|
let b = b.mount("/api", routes![get_raid_devices, get_disks]);
|
|
|
|
|
#[cfg(feature = "static")]
|
|
|
|
|
let b = b.mount("/", CustomHandler{});
|
2022-12-09 15:44:27 +01:00
|
|
|
if let Err(_rocket) = b.launch().await {
|
|
|
|
|
match _rocket.kind() {
|
|
|
|
|
ErrorKind::Bind(_) => println!("Bind address already in use!"),
|
|
|
|
|
e => println!("{}", e.to_string())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 15:02:07 +01:00
|
|
|
Ok(())
|
2022-12-06 22:22:24 +01:00
|
|
|
}
|