add CORS
This commit is contained in:
parent
414fa7bffb
commit
1ee9c93230
2 changed files with 24 additions and 3 deletions
|
@ -2,5 +2,6 @@
|
|||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -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
|
||||
])
|
||||
|
|
Loading…
Reference in a new issue