🐛 Fixes CORS and creating triggers

This commit is contained in:
2025-10-30 10:45:24 +01:00
parent 0717f1694f
commit 7f03d83f72
4 changed files with 67 additions and 11 deletions

View File

@@ -52,8 +52,8 @@ pub async fn get(id: &str, meta: ReqMeta, state: &State<AppState>) -> Result<Nam
None => return Err(Status::BadRequest),
};
let mut tracker: Option<Tracker> = None;
let mut result: Option<QueryResult<usize>> = None;
let tracker: Option<Tracker>;
let result: Option<QueryResult<usize>>;
let now = Utc::now().naive_utc();
{

View File

@@ -1,6 +1,7 @@
use rocket::Request;
use rocket::http::Status;
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::{Header, Status};
use rocket::request::{FromRequest, Outcome};
use rocket::{Request, Response};
use std::env;
pub struct Authenticated;
@@ -22,3 +23,25 @@ impl<'r> FromRequest<'r> for Authenticated {
}
}
}
pub 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",
"GET, POST, DELETE, OPTIONS",
));
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
}
}

View File

@@ -7,6 +7,7 @@ mod schema;
use crate::api::hit;
use crate::api::image;
use crate::api::tracker;
use crate::auth::CORS;
use crate::models::{AppState, GotifyState};
use chrono::Local;
use diesel::{Connection, PgConnection};
@@ -68,13 +69,18 @@ fn rocket() -> _ {
.expect(&format!("Error connecting to {}", database_url));
let gotify_data = GotifyState {
url: gotify_url,
url: gotify_url.clone(),
token: gotify_token,
enabled: gotify_enabled
enabled: gotify_enabled,
};
if gotify_enabled {
info!("Gotify alerts enabled with url {}", gotify_url);
}
let app_data = AppState::new(db, static_dir, gotify_data);
rocket::build()
.manage(app_data)
.attach(CORS {})
.mount("/", routes![index])
.mount(
"/tracker",