use crate::handlers;
use dahua_camera_rtsp::CameraService;
use axum::routing::{get, post};
use axum::Router;
use std::path::Path;
use std::sync::Arc;
use tower_http::cors::CorsLayer;
use tower_http::services::{ServeDir, ServeFile};
const DEFAULT_ASSETS: &str = "static";
pub fn router(service: Arc<CameraService>) -> Router {
router_with_assets(service, DEFAULT_ASSETS)
}
pub fn router_with_assets(service: Arc<CameraService>, assets: impl AsRef<Path>) -> Router {
let state = crate::AppStateInner::new(service);
let assets = assets.as_ref();
let statics = ServeDir::new(assets).fallback(ServeFile::new(assets.join("index.html")));
let api = Router::new()
.route("/api/cameras", get(handlers::status::list_cameras))
.route("/api/cameras/{id}", get(handlers::status::get_camera))
.route("/api/cameras/{id}/control", post(handlers::control::control_camera))
.route("/api/cameras/{id}/cgi_snapshot", get(handlers::control::cgi_snapshot))
.route("/stream/{id}", get(handlers::stream_ws::stream_handler))
.route("/ws/events", get(handlers::events_ws::events_handler))
.route("/api/events", get(handlers::events_sse::events_sse))
.route("/api/cameras/{id}/capabilities", get(handlers::capabilities::get_capabilities));
#[cfg(feature = "alarms")]
let api = api
.route("/api/alarms", get(handlers::alarms::list_alarms))
.route("/api/alarms/ack", post(handlers::alarms::acknowledge_all))
.route("/api/alarms/{id}/ack", post(handlers::alarms::acknowledge));
#[cfg(feature = "decode")]
let api = api
.route("/mjpeg/{id}", get(handlers::mjpeg::mjpeg_handler))
.route("/snapshot/{id}", get(handlers::mjpeg::snapshot_handler));
api.layer(CorsLayer::permissive()).fallback_service(statics).with_state(state)
}