47 lines
1.1 KiB
Rust
47 lines
1.1 KiB
Rust
#[macro_use]
|
|
extern crate rocket;
|
|
|
|
mod parser;
|
|
#[cfg(feature = "static")]
|
|
mod embed;
|
|
|
|
use rocket::error::ErrorKind;
|
|
use rocket::serde::json::Json;
|
|
use rocket::config::{Config};
|
|
#[cfg(feature = "static")]
|
|
use crate::embed::CustomHandler;
|
|
use crate::parser::lsblk_parser::{Disk, parse_lsblk};
|
|
use crate::parser::mdstat_parser::{MdRaidSystem, parse_mdstat};
|
|
|
|
#[get("/raiddevices")]
|
|
fn get_raid_devices() -> Json<MdRaidSystem> {
|
|
Json(parse_mdstat())
|
|
}
|
|
|
|
#[get("/disks")]
|
|
fn get_disks() -> Json<Vec<Disk>> {
|
|
Json(parse_lsblk())
|
|
}
|
|
|
|
#[rocket::main]
|
|
async fn main() -> Result<(), rocket::Error> {
|
|
println!("init server");
|
|
println!("access at: http://127.0.0.1:8000/");
|
|
|
|
let mut cfg = Config::default();
|
|
cfg.address = "0.0.0.0".parse().unwrap();
|
|
|
|
let b = rocket::custom(cfg);
|
|
let b = b.mount("/api", routes![get_raid_devices, get_disks]);
|
|
#[cfg(feature = "static")]
|
|
let b = b.mount("/", CustomHandler{});
|
|
if let Err(_rocket) = b.launch().await {
|
|
match _rocket.kind() {
|
|
ErrorKind::Bind(_) => println!("Bind address already in use!"),
|
|
e => println!("{}", e.to_string())
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|