Frontend events are being returned from the API (yey!)

This commit is contained in:
rzuasti
2026-02-27 12:28:16 -05:00
parent 874a70969f
commit 2d64efa44b
14 changed files with 232 additions and 126 deletions
+1
View File
@@ -1551,6 +1551,7 @@ dependencies = [
"serde_derive",
"serde_json",
"tokio 1.49.0",
"tower",
"tower-http",
]
+2 -1
View File
@@ -21,7 +21,8 @@ lazy_static = "1.5.0"
duration-string = { version="0.5.3", features = ["serde"] }
clap = {version="4.5.54", features=["derive"]}
axum = "0.8.8"
tower-http = { version = "0.6.8", features = ["fs"] }
tower-http = { version = "0.6.8", features = ["fs", "cors"] }
r2d2 = "0.8.10"
r2d2_sqlite = "0.32.0"
once_cell = "1.21.3"
tower = "0.5.3"
+3 -3
View File
@@ -131,7 +131,7 @@ mod tests {
}
#[tokio::test]
async fn test_insert_default() {
async fn test_insert() {
tests_common::setup().await;
// Insert notification and read with returned ID
@@ -216,7 +216,7 @@ mod tests {
}
#[tokio::test]
async fn test_read_default() {
async fn test_read() {
tests_common::setup().await;
// Read existing notification
@@ -300,7 +300,7 @@ mod tests {
}
#[tokio::test]
async fn test_list_default() {
async fn test_list() {
tests_common::setup().await;
let notifications = list().unwrap();
+3 -2
View File
@@ -1,4 +1,5 @@
use chrono::{DateTime, Utc, serde::ts_seconds};
use crate::utils::date_serializer;
use chrono::{DateTime, Utc};
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
use serde::{Deserialize, Serialize};
use std::{error::Error, fmt, str::FromStr};
@@ -6,7 +7,7 @@ use std::{error::Error, fmt, str::FromStr};
#[derive(Clone, Serialize, Deserialize)]
pub struct Notification {
pub id: i64,
#[serde(with = "ts_seconds")]
#[serde(with = "date_serializer")]
pub created_on: DateTime<Utc>,
pub notification_type: NotificationType,
pub title: String,
+9 -1
View File
@@ -1,13 +1,15 @@
use std::error::Error;
use crate::settings::get_settings;
use axum::Router;
use axum::extract::Request;
use axum::http::StatusCode;
use axum::middleware::Next;
use axum::response::Response;
use axum::routing::{delete, get, put};
use axum::{Router, http};
use log::{debug, error, info};
use tower::ServiceBuilder;
use tower_http::cors::{Any, CorsLayer};
use tower_http::services::ServeDir;
pub mod devices;
@@ -18,6 +20,11 @@ pub async fn serve() -> Result<(), Box<dyn Error>> {
info!("Starting web server");
let static_files = ServeDir::new("./web");
// Allow all origins and headers for API
let cors_layer = CorsLayer::new()
.allow_origin(Any)
.allow_headers([http::header::AUTHORIZATION, http::header::CONTENT_TYPE]);
let router = Router::new()
.route("/api/devices", get(devices::list))
.route("/api/devices", put(devices::register))
@@ -30,6 +37,7 @@ pub async fn serve() -> Result<(), Box<dyn Error>> {
get(notifications::read_without_flagging),
)
.route_layer(axum::middleware::from_fn(auth))
.layer(ServiceBuilder::new().layer(cors_layer))
.route(
"/",
get(|| async { "Go to /web for the UI or to /api for the better UI." }),