use actix_web::web;
use actix_web::{dev::Payload, FromRequest, HttpRequest};
use std::future::{ready, Ready};
pub mod alerts;
pub mod auth;
pub mod behavior;
pub mod dashboard;
pub mod device;
pub mod geo;
pub mod network;
pub mod sensors;
pub mod smart_access;
pub mod weather;
pub struct BearerToken(pub String);
impl FromRequest for BearerToken {
type Error = actix_web::Error;
type Future = Ready<Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
let token = req
.headers()
.get("Authorization")
.and_then(|hv| hv.to_str().ok())
.map(|s| s.trim_start_matches("Bearer ").to_string())
.unwrap_or_default();
ready(Ok(Self(token)))
}
}
pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("/api")
.service(auth::get_user)
.service(geo::resolve_geo)
.service(device::resolve_device)
.service(behavior::analyze_behavior)
.service(sensors::analyze_sensors)
.service(network::analyze_network)
.service(alerts::trigger_alert)
.service(dashboard::dashboard_summary)
.service(weather::weather_summary)
.service(smart_access::smart_access_verify),
);
}