Przeglądaj źródła

为非/apilite开头下的其他接口开放跨域,移除不必要的调试接口/monitor

zii 2 tygodni temu
rodzic
commit
5ad3998805
2 zmienionych plików z 14 dodań i 10 usunięć
  1. 4 4
      src/api/ability.rs
  2. 10 6
      src/main.rs

+ 4 - 4
src/api/ability.rs

@@ -211,6 +211,10 @@ pub async fn drop_device(
     if let Err(e) = state.db_lite.execute("update device set belongto=0,name=sn where sn=?", [sn.clone()]).await{
         return Json(JsonBack{errcode: 3000, errmsg: Some(format!("数据库异常: 移除设备从属失败{e}"))});
     }
+    if let Err(e) = state.db_lite.execute("delete from map_user_device where did=?", [did]).await{log(Warning, format!("error: {e}"))}
+
+    // if let Err(e) = state.db_lite.execute("update device set belongto=0,name=sn where did=?", [did]).await{log(Warning, format!("error: {e}"))}
+
     let mut sqls=Vec::new();
     let params = if let Ok(mqids) = state.db_lite.query_rows(
         "select u.mqid from map_user_device m left join user u on u.id=m.uid where m.did=?", 
@@ -221,10 +225,6 @@ pub async fn drop_device(
             return Json(JsonBack{errcode:3000,errmsg:Some("数据库异常: 获取用户mqtt-clientid失败".to_string())})
         };
     
-    if let Err(e) = state.db_lite.execute("delete from map_user_device where did=?", [did]).await{log(Warning, format!("error: {e}"))}
-
-    if let Err(e) = state.db_lite.execute("update device set belongto=0,name=sn where did=?", [did]).await{log(Warning, format!("error: {e}"))}
-
     match state.mq_detail{
         Some(s) => {
             log(Debug, format!("正在执行删除{sn}, {:?}",params));

+ 10 - 6
src/main.rs

@@ -2,7 +2,7 @@ mod datasource;
 mod api;
 use std::{collections::HashMap, process::exit, str::FromStr};
 
-use axum::{extract::State, routing::any};
+use axum::{routing::any};
 use datasource::sqlite;
 use tokio::sync::Mutex;
 use tower_http::cors::{Any, CorsLayer}; // 添加Any和CorsLayer的引用
@@ -129,7 +129,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
              } else{None}
     };
 
-    let monitor = Counter::new();
+    // let monitor = Counter::new();
 
     use axum::routing::{post,get};
     let app =  axum::Router::new()
@@ -193,10 +193,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
         // .route("/", get(|couter:State<Counter>| async move {"hello".to_string()}))
         .route("/", any(|| async {"hello".to_string()}))
         // .route("/hello", get(api::example))
-        .nest("/monitor", axum::Router::new()
-            .route("/show", get(|c:State<Counter>|async move{ axum::Json::from(c.get_counts().await)  }))
-            .with_state(monitor)
-        )
+        // .nest("/monitor", axum::Router::new()
+        //     .route("/show", get(|c:State<Counter>|async move{ axum::Json::from(c.get_counts().await)  }))
+        //     .with_state(monitor)
+        // )
+        .layer(CorsLayer::new()
+                .allow_origin(Any)
+                .allow_methods(Any)
+                .allow_headers(Any))
     ;
     if let (Some(cert),Some(key),Some(ssl)) = (conf.ssl_cert,conf.ssl_key,conf.ssl) {
         use axum_server::tls_rustls::RustlsConfig;