API
This commit is contained in:
parent
24431f2167
commit
25ee0246de
5 changed files with 173 additions and 43 deletions
|
|
@ -1,3 +1,6 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use crate::communities::community::Community;
|
||||
use crate::data::communication::{CommunicationType, CommunicationValue, DataTypes};
|
||||
use crate::gui::log_panel::log_message;
|
||||
use axum::http::HeaderValue;
|
||||
|
|
@ -27,7 +30,16 @@ pub async fn handle(
|
|||
let (status, content, body_text) = if path_parts.len() >= 3 {
|
||||
match path_parts[2] {
|
||||
"app_state" => (StatusCode::OK, "application/json", {
|
||||
let json = APP_STATE.lock().unwrap().clone();
|
||||
let with = headers
|
||||
.get("size")
|
||||
.unwrap_or(&HeaderValue::from_static("50"))
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.to_string();
|
||||
let json = APP_STATE
|
||||
.lock()
|
||||
.unwrap()
|
||||
.with_width(with.parse::<u16>().unwrap_or(50));
|
||||
json.to_json().to_string()
|
||||
}),
|
||||
"users" => (StatusCode::OK, "application/json", {
|
||||
|
|
@ -44,7 +56,7 @@ pub async fn handle(
|
|||
.add_data(DataTypes::user, user.to_json());
|
||||
cv.to_json().to_string()
|
||||
} else {
|
||||
"{}".to_string()
|
||||
"{\"type\":\"error\"}".to_string()
|
||||
}
|
||||
}
|
||||
"remove" => {
|
||||
|
|
@ -62,7 +74,7 @@ pub async fn handle(
|
|||
}
|
||||
json.to_string()
|
||||
}
|
||||
_ => "{}".to_string(),
|
||||
_ => "{\"type\":\"error\"}".to_string(),
|
||||
}
|
||||
} else {
|
||||
let users = user_manager::get_users();
|
||||
|
|
@ -74,15 +86,59 @@ pub async fn handle(
|
|||
}
|
||||
}),
|
||||
"communities" => (StatusCode::OK, "application/json", {
|
||||
let communities = community_manager::get_communities().await;
|
||||
let mut json = JsonValue::new_array();
|
||||
for community in communities {
|
||||
let _ = json.push(community.to_json().await);
|
||||
if path_parts.len() >= 4 {
|
||||
match path_parts[3] {
|
||||
"add" => {
|
||||
let name = headers.get("name").unwrap().to_str().unwrap();
|
||||
let community = Arc::new(Community::create(name.to_string()).await);
|
||||
community_manager::add_community(community).await;
|
||||
"{\"type\":\"success\"}".to_string()
|
||||
}
|
||||
"remove" => {
|
||||
let name = headers.get("name").unwrap().to_str().unwrap();
|
||||
community_manager::remove_community(name).await;
|
||||
"{\"type\":\"success\"}".to_string()
|
||||
}
|
||||
"get" => {
|
||||
let communities = community_manager::get_communities().await;
|
||||
let mut json = JsonValue::new_array();
|
||||
for community in communities {
|
||||
let _ = json.push(community.to_json().await);
|
||||
}
|
||||
json.to_string()
|
||||
}
|
||||
_ => "{\"type\":\"error\"}".to_string(),
|
||||
}
|
||||
} else {
|
||||
let communities = community_manager::get_communities().await;
|
||||
let mut json = JsonValue::new_array();
|
||||
for community in communities {
|
||||
let _ = json.push(community.to_json().await);
|
||||
}
|
||||
json.to_string()
|
||||
}
|
||||
json.to_string()
|
||||
}),
|
||||
"settings" => (StatusCode::OK, "application/json", {
|
||||
CONFIG.lock().await.config.to_string()
|
||||
if path_parts.len() >= 4 {
|
||||
match path_parts[3] {
|
||||
"set" => {
|
||||
if let Some(key) = headers.get("key") {
|
||||
if let Some(value) = headers.get("value") {
|
||||
CONFIG
|
||||
.lock()
|
||||
.await
|
||||
.config
|
||||
.insert(key.to_str().unwrap(), value);
|
||||
"{\"type\":\"success\"}".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
"get" => CONFIG.lock().await.config.to_string(),
|
||||
_ => "{\"type\":\"error\"}".to_string(),
|
||||
}
|
||||
} else {
|
||||
CONFIG.lock().await.config.to_string()
|
||||
}
|
||||
}),
|
||||
|
||||
_ => {
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
use crate::gui::log_panel::log_message;
|
||||
use crate::server::api;
|
||||
use crate::server::socket::handle;
|
||||
use crate::util::file_util::{load_file, load_file_buf};
|
||||
use crate::util::file_util::{load_file_buf, load_file_vec};
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use futures::{StreamExt, TryFutureExt};
|
||||
use http_body_util::Full;
|
||||
use hyper::body::Bytes;
|
||||
use hyper::header::CONTENT_LENGTH;
|
||||
use hyper::{
|
||||
Request as HttpRequest, Response as HttpResponse, StatusCode, body::Incoming,
|
||||
server::conn::http1, upgrade,
|
||||
|
|
@ -113,43 +112,49 @@ impl Service<HttpRequest<Incoming>> for HttpService {
|
|||
} else {
|
||||
let mut path_parts: Vec<&str> = path.split("/").collect();
|
||||
let name = path_parts.remove(path_parts.len() - 1);
|
||||
|
||||
let (status, content, body_text): (StatusCode, &str, String) =
|
||||
if name.is_empty() {
|
||||
let content = load_file("web", "index.html");
|
||||
let name = if name.is_empty() {
|
||||
"index.html"
|
||||
} else if name.contains(".") && name.contains("?") {
|
||||
name.split("?").next().unwrap()
|
||||
} else if name.contains(".") {
|
||||
name
|
||||
} else {
|
||||
&format!("{}.html", name)
|
||||
};
|
||||
let code = if let Some(ext) = name.split(".").last() {
|
||||
match ext {
|
||||
"html" => "text/html",
|
||||
"css" => "text/css",
|
||||
"ico" => "image/x-icon",
|
||||
"png" => "image/png",
|
||||
"js" => "application/javascript",
|
||||
"json" => "application/json",
|
||||
_ => "application/octet-stream",
|
||||
}
|
||||
} else {
|
||||
"application/octet-stream"
|
||||
};
|
||||
let (status, content, body_text): (StatusCode, &str, Vec<u8>) = {
|
||||
let content = load_file_vec(&format!("web{}/", path_parts.join("/")), name);
|
||||
if content.is_empty() {
|
||||
let content = load_file_vec("web", "404.html");
|
||||
if content.is_empty() {
|
||||
let content = load_file("web", "404.html");
|
||||
if content.is_empty() {
|
||||
(
|
||||
StatusCode::NOT_FOUND,
|
||||
"text/html",
|
||||
include_str!("../../static/web/404.html").to_string(),
|
||||
)
|
||||
} else {
|
||||
(StatusCode::OK, "text/html", content)
|
||||
}
|
||||
(
|
||||
StatusCode::NOT_FOUND,
|
||||
code,
|
||||
include_str!("../../static/web/404.html")
|
||||
.as_bytes()
|
||||
.to_vec(),
|
||||
)
|
||||
} else {
|
||||
(StatusCode::OK, "text/html", content)
|
||||
(StatusCode::OK, code, content)
|
||||
}
|
||||
} else {
|
||||
let content = load_file(&format!("web{}/", path_parts.join("/")), name);
|
||||
if content.is_empty() {
|
||||
let content = load_file("web", "404.html");
|
||||
if content.is_empty() {
|
||||
(
|
||||
StatusCode::NOT_FOUND,
|
||||
"text/html",
|
||||
include_str!("../../static/web/404.html").to_string(),
|
||||
)
|
||||
} else {
|
||||
(StatusCode::OK, "text/html", content)
|
||||
}
|
||||
} else {
|
||||
(StatusCode::OK, "text/html", content)
|
||||
}
|
||||
};
|
||||
(StatusCode::OK, code, content)
|
||||
}
|
||||
};
|
||||
|
||||
let body = Full::new(Bytes::from(body_text.to_string()));
|
||||
let body = Full::new(Bytes::from(body_text.to_vec()));
|
||||
let response = HttpResponse::builder()
|
||||
.header("Content-Type", content)
|
||||
.status(status)
|
||||
|
|
|
|||
Loading…
Reference in a new issue