|
@@ -2,7 +2,7 @@ mod datasource;
|
|
|
mod api;
|
|
mod api;
|
|
|
use std::{collections::HashMap, process::exit, str::FromStr};
|
|
use std::{collections::HashMap, process::exit, str::FromStr};
|
|
|
|
|
|
|
|
-use axum::{extract::{Request, State}, middleware::Next, routing::any};
|
|
|
|
|
|
|
+use axum::{extract::State, routing::any};
|
|
|
use datasource::sqlite;
|
|
use datasource::sqlite;
|
|
|
use tokio::sync::Mutex;
|
|
use tokio::sync::Mutex;
|
|
|
use tower_http::cors::{Any, CorsLayer}; // 添加Any和CorsLayer的引用
|
|
use tower_http::cors::{Any, CorsLayer}; // 添加Any和CorsLayer的引用
|
|
@@ -42,7 +42,7 @@ pub fn log(level: LogLevel, msg: String) {
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
#[derive(Clone)]
|
|
|
struct AppState{
|
|
struct AppState{
|
|
|
- db_lite: sqlite::SqlitePool
|
|
|
|
|
|
|
+ db_lite: sqlite::SqlitePool,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
#[derive(Clone)]
|
|
@@ -50,6 +50,7 @@ struct Counter {
|
|
|
counts: std::sync::Arc<Mutex<HashMap<String, [u64;2]>>>,
|
|
counts: std::sync::Arc<Mutex<HashMap<String, [u64;2]>>>,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+#[allow(dead_code)]
|
|
|
impl Counter {
|
|
impl Counter {
|
|
|
fn new() -> Self {
|
|
fn new() -> Self {
|
|
|
Counter {
|
|
Counter {
|
|
@@ -76,9 +77,11 @@ const CFGPATH: &'static str = "config.json";
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
#[derive(serde::Deserialize,serde::Serialize, Debug)]
|
|
#[derive(serde::Deserialize,serde::Serialize, Debug)]
|
|
|
pub struct Conf{
|
|
pub struct Conf{
|
|
|
- host: String,
|
|
|
|
|
|
|
+ #[serde()]
|
|
|
|
|
+ host: Option<String>,
|
|
|
ssl: Option<String>,
|
|
ssl: Option<String>,
|
|
|
db: String,
|
|
db: String,
|
|
|
|
|
+ // mqtt: String,
|
|
|
ssl_cert: Option<String>,
|
|
ssl_cert: Option<String>,
|
|
|
ssl_key: Option<String>,
|
|
ssl_key: Option<String>,
|
|
|
}
|
|
}
|
|
@@ -87,15 +90,24 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
Err(_) => {let f=std::fs::File::create_new(CFGPATH).unwrap();
|
|
Err(_) => {let f=std::fs::File::create_new(CFGPATH).unwrap();
|
|
|
serde_json::to_writer(&f, &Conf{
|
|
serde_json::to_writer(&f, &Conf{
|
|
|
ssl: None,
|
|
ssl: None,
|
|
|
- host: "0.0.0.0:3000".to_string(),
|
|
|
|
|
|
|
+ host: Some("0.0.0.0:3000".to_string()),
|
|
|
db: "db.sqlite".to_string(),
|
|
db: "db.sqlite".to_string(),
|
|
|
|
|
+ // mqtt: "/vat/lib/mosquitto.db".to_string(),
|
|
|
ssl_cert: None,
|
|
ssl_cert: None,
|
|
|
ssl_key: None,
|
|
ssl_key: None,
|
|
|
}).unwrap();
|
|
}).unwrap();
|
|
|
f}
|
|
f}
|
|
|
}){
|
|
}){
|
|
|
Ok(conf) => conf,
|
|
Ok(conf) => conf,
|
|
|
- Err(_) => {log(Warning, "config file phrase fail, overwriting ...\n\t\tdone, running with default configuration".to_string());Conf { host: "0.0.0.0:3000".to_string(), db: "db.sqlite".to_string(),ssl_cert:None, ssl_key:None,ssl:None }},
|
|
|
|
|
|
|
+ Err(_) => {log(Warning, "config file phrase fail, overwriting ...\n\t\tdone, running with default configuration".to_string());
|
|
|
|
|
+ Conf {
|
|
|
|
|
+ host: Some("0.0.0.0:3000".to_string()),
|
|
|
|
|
+ db: "db.sqlite".to_string(),
|
|
|
|
|
+ // mqtt: " /var/lib/mosquitto/data.db".to_string(),
|
|
|
|
|
+ ssl_cert:None,
|
|
|
|
|
+ ssl_key:None,
|
|
|
|
|
+ ssl:None
|
|
|
|
|
+ }},
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
let appstat = AppState{db_lite: sqlite::init_sqlite_pool(&conf.db, 10).await?};
|
|
let appstat = AppState{db_lite: sqlite::init_sqlite_pool(&conf.db, 10).await?};
|
|
@@ -110,7 +122,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
.nest("/apilite", axum::Router::new()
|
|
.nest("/apilite", axum::Router::new()
|
|
|
// .nest("/api", axum::Router::new()
|
|
// .nest("/api", axum::Router::new()
|
|
|
// .route("/wx/auth", get(api::auth::auth))
|
|
// .route("/wx/auth", get(api::auth::auth))
|
|
|
-
|
|
|
|
|
|
|
+ .route("/mq", post(api::ability::get_mqtt))
|
|
|
.route("/loggin", post(api::user::u_loggin))
|
|
.route("/loggin", post(api::user::u_loggin))
|
|
|
|
|
|
|
|
.route("/device", post(api::device::d_all))
|
|
.route("/device", post(api::device::d_all))
|
|
@@ -138,18 +150,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
.route("/flow/share/new", post(api::flow_task::new_flow_task_share_device))
|
|
.route("/flow/share/new", post(api::flow_task::new_flow_task_share_device))
|
|
|
.route("/flow/share/checkout", post(api::flow_task::checkout_flow_task_of_share_device))
|
|
.route("/flow/share/checkout", post(api::flow_task::checkout_flow_task_of_share_device))
|
|
|
|
|
|
|
|
- .route_layer(
|
|
|
|
|
- axum::middleware::from_fn_with_state(
|
|
|
|
|
- monitor.clone(),
|
|
|
|
|
- |a:State<Counter>,req: Request,c:Next | async move{
|
|
|
|
|
- log(Debuging, format!("in url {} {:?}",req.uri().path(),req.uri().query()));
|
|
|
|
|
- let url = format!("{} {}",req.method().as_str(),req.uri().path());
|
|
|
|
|
- let resp = c.run(req).await;
|
|
|
|
|
- a.increment(url.as_str(),resp.status().is_success()).await;
|
|
|
|
|
- resp
|
|
|
|
|
- }
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ // .route_layer(
|
|
|
|
|
+ // axum::middleware::from_fn_with_state(
|
|
|
|
|
+ // monitor.clone(),
|
|
|
|
|
+ // |a:State<Counter>,req: Request,c:Next | async move{
|
|
|
|
|
+ // log(Debuging, format!("in url {} {:?}",req.uri().path(),req.uri().query()));
|
|
|
|
|
+ // let url = format!("{} {}",req.method().as_str(),req.uri().path());
|
|
|
|
|
+ // let resp = c.run(req).await;
|
|
|
|
|
+ // a.increment(url.as_str(),resp.status().is_success()).await;
|
|
|
|
|
+ // resp
|
|
|
|
|
+ // }
|
|
|
|
|
+ // )
|
|
|
|
|
+ // )
|
|
|
.with_state(appstat)
|
|
.with_state(appstat)
|
|
|
|
|
|
|
|
// layer需要在最后添加,添加视为入栈,生效顺序为出栈顺序
|
|
// layer需要在最后添加,添加视为入栈,生效顺序为出栈顺序
|
|
@@ -169,15 +181,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
if let (Some(cert),Some(key),Some(ssl)) = (conf.ssl_cert,conf.ssl_key,conf.ssl) {
|
|
if let (Some(cert),Some(key),Some(ssl)) = (conf.ssl_cert,conf.ssl_key,conf.ssl) {
|
|
|
use axum_server::tls_rustls::RustlsConfig;
|
|
use axum_server::tls_rustls::RustlsConfig;
|
|
|
use std::{path::PathBuf,net::SocketAddr};
|
|
use std::{path::PathBuf,net::SocketAddr};
|
|
|
- log(Info, format!("rederect for http2https at {}==>{}",conf.host,ssl));
|
|
|
|
|
- let (listener, app2) = (
|
|
|
|
|
- tokio::net::TcpListener::bind(conf.host).await?,
|
|
|
|
|
- axum::Router::new()
|
|
|
|
|
- .route("/", any(redirect_http_to_https)));
|
|
|
|
|
- tokio::spawn(async move{
|
|
|
|
|
- log(Info, format!("http2https start"));
|
|
|
|
|
- axum::serve(listener,app2).await
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ if let Some(host)=conf.host{
|
|
|
|
|
+ log(Info, format!("rederect for http2https at {}==>{}",host,ssl));
|
|
|
|
|
+ let (listener, app2) = (
|
|
|
|
|
+ tokio::net::TcpListener::bind(host).await?,
|
|
|
|
|
+ axum::Router::new()
|
|
|
|
|
+ .route("/", any(redirect_http_to_https)));
|
|
|
|
|
+ tokio::spawn(async move{
|
|
|
|
|
+ log(Info, format!("http2https start"));
|
|
|
|
|
+ axum::serve(listener,app2).await
|
|
|
|
|
+ });
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
log(Info, format!("host https at {}",ssl));
|
|
log(Info, format!("host https at {}",ssl));
|
|
|
log(Info, "starting with ssl-https".to_string());
|
|
log(Info, "starting with ssl-https".to_string());
|
|
@@ -194,9 +208,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
Err(e) => {log(Error, format!("{e}"));exit(1)}
|
|
Err(e) => {log(Error, format!("{e}"));exit(1)}
|
|
|
}
|
|
}
|
|
|
).serve(app.into_make_service()).await?;
|
|
).serve(app.into_make_service()).await?;
|
|
|
- } else {
|
|
|
|
|
- log(Info, format!("hosting at {}",conf.host));
|
|
|
|
|
- let listener = tokio::net::TcpListener::bind(conf.host).await?;
|
|
|
|
|
|
|
+ } else if let Some(host)=conf.host{
|
|
|
|
|
+ log(Info, format!("hosting at {}",host));
|
|
|
|
|
+ let listener = tokio::net::TcpListener::bind(host).await?;
|
|
|
axum::serve(listener, app).await?;
|
|
axum::serve(listener, app).await?;
|
|
|
}
|
|
}
|
|
|
log(Info, format!("bye!"));
|
|
log(Info, format!("bye!"));
|