v0.12.0-preety_code
This commit is contained in:
+176
-54
@@ -1,7 +1,27 @@
|
||||
use std::{net::IpAddr, sync::atomic::Ordering, time::{Duration, Instant}};
|
||||
use crate::{
|
||||
engine,
|
||||
error::AppError,
|
||||
home_assistant, influxdb,
|
||||
models::{
|
||||
ApiTokenInfo, ApplicationSettings, Automation, ClimateGroup, ConfigurationExport,
|
||||
DebugSettings, Device, DeviceCommand, DevicePatch, DiscoveryRequest, GreeSettings,
|
||||
GroupControlPatch, HaReading, HistorySettings, HomeAssistantSettings,
|
||||
HomeAssistantSettingsUpdate, HomeAssistantSettingsView, InfluxDbSettings,
|
||||
InfluxDbSettingsUpdate, InfluxDbSettingsView, ManualDeviceRequest, NightModeSettings,
|
||||
NotificationSettings, NotificationSettingsUpdate, NotificationSettingsView, Reading,
|
||||
RuntimeSettings, Schedule, TemporaryQuickThermostat, TemporaryQuickThermostatRequest, Zone,
|
||||
ZoneControlPatch, ZoneReading,
|
||||
},
|
||||
notifications,
|
||||
protocol::merge_discovered,
|
||||
state::AppState,
|
||||
};
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{Path, Query, Request, State, WebSocketUpgrade, ws::{Message, WebSocket}},
|
||||
extract::{
|
||||
ws::{Message, WebSocket},
|
||||
Path, Query, Request, State, WebSocketUpgrade,
|
||||
},
|
||||
http::{header, HeaderMap, HeaderValue, StatusCode},
|
||||
middleware::{self, Next},
|
||||
response::{Redirect, Response},
|
||||
@@ -13,20 +33,15 @@ use chrono::{Duration as ChronoDuration, NaiveTime, Utc};
|
||||
use futures_util::StreamExt;
|
||||
use rand::{rngs::OsRng, RngCore};
|
||||
use serde::Deserialize;
|
||||
use sha2::{Digest, Sha256};
|
||||
use serde_json::{json, Value};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{
|
||||
net::IpAddr,
|
||||
sync::atomic::Ordering,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use tower_http::{compression::CompressionLayer, trace::TraceLayer};
|
||||
use uuid::Uuid;
|
||||
use crate::{
|
||||
engine,
|
||||
error::AppError,
|
||||
home_assistant,
|
||||
influxdb,
|
||||
notifications,
|
||||
models::{ApiTokenInfo, ApplicationSettings, Automation, ClimateGroup, ConfigurationExport, DebugSettings, Device, DeviceCommand, DevicePatch, DiscoveryRequest, GreeSettings, GroupControlPatch, HaReading, HistorySettings, HomeAssistantSettings, HomeAssistantSettingsUpdate, HomeAssistantSettingsView, InfluxDbSettings, InfluxDbSettingsUpdate, InfluxDbSettingsView, ManualDeviceRequest, NightModeSettings, NotificationSettings, NotificationSettingsUpdate, NotificationSettingsView, Reading, RuntimeSettings, Schedule, TemporaryQuickThermostat, TemporaryQuickThermostatRequest, Zone, ZoneControlPatch, ZoneReading},
|
||||
protocol::merge_discovered,
|
||||
state::AppState,
|
||||
};
|
||||
|
||||
mod openapi;
|
||||
|
||||
@@ -67,65 +82,168 @@ pub fn router(state: AppState) -> Router {
|
||||
.route("/api/system/info", get(system_info))
|
||||
.route("/api/discovery", post(discover))
|
||||
.route("/api/devices", get(list_devices).post(add_device))
|
||||
.route("/api/devices/:id", get(get_device).patch(patch_device).delete(delete_device))
|
||||
.route(
|
||||
"/api/devices/:id",
|
||||
get(get_device).patch(patch_device).delete(delete_device),
|
||||
)
|
||||
.route("/api/devices/:id/bind", post(bind_device))
|
||||
.route("/api/devices/:id/poll", post(poll_device))
|
||||
.route("/api/devices/:id/probe", post(probe_device))
|
||||
.route("/api/devices/:id/command", post(command_device))
|
||||
.route("/api/zones", get(list_zones).post(create_zone))
|
||||
.route("/api/zones/:id", get(get_zone).put(update_zone).delete(delete_zone))
|
||||
.route(
|
||||
"/api/zones/:id",
|
||||
get(get_zone).put(update_zone).delete(delete_zone),
|
||||
)
|
||||
.route("/api/zones/:id/control", post(update_zone_control))
|
||||
.route("/api/zones/:id/compressor-queue/cancel", post(cancel_zone_compressor_queue))
|
||||
.route("/api/compressor-queue/cancel-all", post(cancel_all_compressor_queues))
|
||||
.route("/api/zones/:id/schedule-template", post(apply_schedule_template))
|
||||
.route(
|
||||
"/api/zones/:id/compressor-queue/cancel",
|
||||
post(cancel_zone_compressor_queue),
|
||||
)
|
||||
.route(
|
||||
"/api/compressor-queue/cancel-all",
|
||||
post(cancel_all_compressor_queues),
|
||||
)
|
||||
.route(
|
||||
"/api/zones/:id/schedule-template",
|
||||
post(apply_schedule_template),
|
||||
)
|
||||
.route("/api/groups", get(list_groups).post(create_group))
|
||||
.route("/api/groups/:id", get(get_group).put(update_group).delete(delete_group))
|
||||
.route(
|
||||
"/api/groups/:id",
|
||||
get(get_group).put(update_group).delete(delete_group),
|
||||
)
|
||||
.route("/api/groups/:id/control", post(update_group_control))
|
||||
.route("/api/house/control", post(update_house_control))
|
||||
.route("/api/house/power", post(update_house_power))
|
||||
.route("/api/house/preset", post(update_house_preset))
|
||||
.route("/api/schedules", get(list_schedules).post(create_schedule))
|
||||
.route("/api/schedules/:id", get(get_schedule).put(update_schedule).delete(delete_schedule))
|
||||
.route("/api/automations", get(list_automations).post(create_automation))
|
||||
.route("/api/automations/:id", get(get_automation).put(update_automation).delete(delete_automation))
|
||||
.route(
|
||||
"/api/schedules/:id",
|
||||
get(get_schedule)
|
||||
.put(update_schedule)
|
||||
.delete(delete_schedule),
|
||||
)
|
||||
.route(
|
||||
"/api/automations",
|
||||
get(list_automations).post(create_automation),
|
||||
)
|
||||
.route(
|
||||
"/api/automations/:id",
|
||||
get(get_automation)
|
||||
.put(update_automation)
|
||||
.delete(delete_automation),
|
||||
)
|
||||
.route("/api/flows", get(list_flows).post(create_flow))
|
||||
.route("/api/flows/import", post(import_flow))
|
||||
.route("/api/flows/simulate", post(simulate_flow))
|
||||
.route("/api/flows/:id/export", get(export_flow))
|
||||
.route("/api/flows/:id/logs", get(flow_logs))
|
||||
.route("/api/flows/:id", get(get_flow).put(update_flow).delete(delete_flow))
|
||||
.route(
|
||||
"/api/flows/:id",
|
||||
get(get_flow).put(update_flow).delete(delete_flow),
|
||||
)
|
||||
.route("/api/readings", get(readings))
|
||||
.route("/api/history", get(history))
|
||||
.route("/api/control-plan", get(control_plan))
|
||||
.route("/api/events", get(events))
|
||||
.route("/api/settings/application", get(get_application_settings).put(update_application_settings))
|
||||
.route("/api/settings/gree", get(get_gree_settings).put(update_gree_settings))
|
||||
.route("/api/settings/history", get(get_history_settings).put(update_history_settings))
|
||||
.route("/api/settings/influxdb", get(get_influxdb_settings).put(update_influxdb_settings))
|
||||
.route("/api/settings/notifications", get(get_notification_settings).put(update_notification_settings))
|
||||
.route("/api/settings/night", get(get_night_settings).put(update_night_settings))
|
||||
.route("/api/settings/home-assistant", get(get_home_assistant_settings).put(update_home_assistant_settings))
|
||||
.route("/api/settings/debug", get(get_debug_settings).put(update_debug_settings))
|
||||
.route(
|
||||
"/api/settings/application",
|
||||
get(get_application_settings).put(update_application_settings),
|
||||
)
|
||||
.route(
|
||||
"/api/settings/gree",
|
||||
get(get_gree_settings).put(update_gree_settings),
|
||||
)
|
||||
.route(
|
||||
"/api/settings/history",
|
||||
get(get_history_settings).put(update_history_settings),
|
||||
)
|
||||
.route(
|
||||
"/api/settings/influxdb",
|
||||
get(get_influxdb_settings).put(update_influxdb_settings),
|
||||
)
|
||||
.route(
|
||||
"/api/settings/notifications",
|
||||
get(get_notification_settings).put(update_notification_settings),
|
||||
)
|
||||
.route(
|
||||
"/api/settings/night",
|
||||
get(get_night_settings).put(update_night_settings),
|
||||
)
|
||||
.route(
|
||||
"/api/settings/home-assistant",
|
||||
get(get_home_assistant_settings).put(update_home_assistant_settings),
|
||||
)
|
||||
.route(
|
||||
"/api/settings/debug",
|
||||
get(get_debug_settings).put(update_debug_settings),
|
||||
)
|
||||
.route("/api/configuration/export", get(export_configuration))
|
||||
.route("/api/configuration/import", post(import_configuration))
|
||||
.route("/api/access-tokens", get(list_access_tokens).post(create_access_token))
|
||||
.route("/api/access-tokens/:id", axum::routing::delete(delete_access_token))
|
||||
.route("/api/integrations/home-assistant/test", post(test_home_assistant))
|
||||
.route("/api/integrations/home-assistant/entity", post(inspect_home_assistant_entity))
|
||||
.route("/api/integrations/notifications/test", post(test_notifications))
|
||||
.route(
|
||||
"/api/access-tokens",
|
||||
get(list_access_tokens).post(create_access_token),
|
||||
)
|
||||
.route(
|
||||
"/api/access-tokens/:id",
|
||||
axum::routing::delete(delete_access_token),
|
||||
)
|
||||
.route(
|
||||
"/api/integrations/home-assistant/test",
|
||||
post(test_home_assistant),
|
||||
)
|
||||
.route(
|
||||
"/api/integrations/home-assistant/entity",
|
||||
post(inspect_home_assistant_entity),
|
||||
)
|
||||
.route(
|
||||
"/api/integrations/notifications/test",
|
||||
post(test_notifications),
|
||||
)
|
||||
.route_layer(middleware::from_fn_with_state(state.clone(), auth));
|
||||
|
||||
let home_assistant_api = Router::new()
|
||||
.route("/api/integrations/home-assistant/devices", get(list_devices))
|
||||
.route("/api/integrations/home-assistant/devices/:id/command", post(command_home_assistant_device))
|
||||
.route("/api/integrations/home-assistant/control-plan", get(control_plan))
|
||||
.route("/api/integrations/home-assistant/groups", get(list_home_assistant_groups))
|
||||
.route("/api/integrations/home-assistant/groups/:id/control", post(update_home_assistant_group_control))
|
||||
.route("/api/integrations/home-assistant/house/control", post(update_house_control))
|
||||
.route("/api/integrations/home-assistant/house/preset", post(update_house_preset))
|
||||
.route("/api/integrations/home-assistant/house/power", post(update_house_power))
|
||||
.route("/api/integrations/home-assistant/zones/:id/control", post(update_home_assistant_zone_control))
|
||||
.route_layer(middleware::from_fn_with_state(state.clone(), home_assistant_auth));
|
||||
.route(
|
||||
"/api/integrations/home-assistant/devices",
|
||||
get(list_devices),
|
||||
)
|
||||
.route(
|
||||
"/api/integrations/home-assistant/devices/:id/command",
|
||||
post(command_home_assistant_device),
|
||||
)
|
||||
.route(
|
||||
"/api/integrations/home-assistant/control-plan",
|
||||
get(control_plan),
|
||||
)
|
||||
.route(
|
||||
"/api/integrations/home-assistant/groups",
|
||||
get(list_home_assistant_groups),
|
||||
)
|
||||
.route(
|
||||
"/api/integrations/home-assistant/groups/:id/control",
|
||||
post(update_home_assistant_group_control),
|
||||
)
|
||||
.route(
|
||||
"/api/integrations/home-assistant/house/control",
|
||||
post(update_house_control),
|
||||
)
|
||||
.route(
|
||||
"/api/integrations/home-assistant/house/preset",
|
||||
post(update_house_preset),
|
||||
)
|
||||
.route(
|
||||
"/api/integrations/home-assistant/house/power",
|
||||
post(update_house_power),
|
||||
)
|
||||
.route(
|
||||
"/api/integrations/home-assistant/zones/:id/control",
|
||||
post(update_home_assistant_zone_control),
|
||||
)
|
||||
.route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
home_assistant_auth,
|
||||
));
|
||||
|
||||
let mut app = Router::new()
|
||||
.route("/api/health", get(health))
|
||||
@@ -161,23 +279,27 @@ pub fn router(state: AppState) -> Router {
|
||||
let base = state.config.base_path.clone();
|
||||
let redirect_to = format!("{base}/");
|
||||
Router::new()
|
||||
.route(&base, get(move || {
|
||||
let redirect_to = redirect_to.clone();
|
||||
async move { Redirect::permanent(&redirect_to) }
|
||||
}))
|
||||
.route(
|
||||
&base,
|
||||
get(move || {
|
||||
let redirect_to = redirect_to.clone();
|
||||
async move { Redirect::permanent(&redirect_to) }
|
||||
}),
|
||||
)
|
||||
.nest(&base, app)
|
||||
.fallback(not_found)
|
||||
};
|
||||
|
||||
app
|
||||
.layer(CompressionLayer::new())
|
||||
app.layer(CompressionLayer::new())
|
||||
.layer(TraceLayer::new_for_http())
|
||||
.layer(middleware::from_fn(security_headers))
|
||||
.layer(middleware::from_fn_with_state(state.clone(), debug_api_requests))
|
||||
.layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
debug_api_requests,
|
||||
))
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
|
||||
// Functional source split intentionally keeps items in the existing module namespace.
|
||||
include!("api/auth.rs");
|
||||
include!("api/system.rs");
|
||||
|
||||
Reference in New Issue
Block a user