This commit is contained in:
Rasmus Moorats 2024-07-16 17:16:06 +03:00
parent 414fa7bffb
commit 1ee9c93230
Signed by: xx
GPG key ID: FE14255A6AE7241C
2 changed files with 24 additions and 3 deletions
api

View file

@ -2,5 +2,6 @@
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View file

@ -3,13 +3,32 @@ extern crate rocket;
use std::process::Command;
use std::sync::{Arc, Mutex};
use rocket::http::Status;
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::{Header, Status};
use rocket::response::status;
use rocket::State;
use rocket::{Request, Response, State};
struct Rebooting(bool);
struct CORS;
#[rocket::async_trait]
impl Fairing for CORS {
fn info(&self) -> Info {
Info {
name: "Add CORS headers to responses",
kind: Kind::Response
}
}
async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
response.set_header(Header::new("Access-Control-Allow-Methods", "POST, GET, PATCH, OPTIONS"));
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
}
}
#[get("/online")]
fn online(is_rebooting: &State<Arc<Mutex<Rebooting>>>) -> Result<&'static str, status::Custom<String>> {
let rebooting = is_rebooting.lock().map_err(|e| status::Custom(
@ -93,6 +112,7 @@ fn restart_pipewire() -> Result<status::NoContent, status::Custom<String>> {
fn rocket() -> _ {
rocket::build()
.manage(Arc::new(Mutex::new(Rebooting(false))))
.attach(CORS)
.mount("/api", routes![
restart_kodi, restart_pipewire, reboot, online
])