Skip to content

Commit

Permalink
feat: query client api
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhazhazhu committed Jan 23, 2024
1 parent 6639ec7 commit d920230
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
4 changes: 2 additions & 2 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod routes;

use actix_cors::Cors;
use actix_web::{http, web, App, HttpServer};
use routes::{clients, clients_by_ip, ws_index};
use routes::{clients, ws_index};
use std::env;

#[actix_web::main]
Expand Down Expand Up @@ -38,7 +38,7 @@ async fn main() -> std::io::Result<()> {
.wrap(cors)
.app_data(shared_data.clone())
.route("/ws", web::get().to(ws_index))
.service(web::scope("/api").service(clients).service(clients_by_ip))
.service(web::scope("/api").service(clients))
})
.workers(2)
.bind(("0.0.0.0", 8080))?
Expand Down
20 changes: 10 additions & 10 deletions crates/server/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ pub fn insert_service(
websocket_key: String,
) -> mysql::error::Result<u64> {
let insert_query =
format!("INSERT INTO {SERVICES_TABLE_NAME} (ip, links, websocket_key) VALUES (:ip, :links, :websocket_key)");
format!("INSERT INTO {SERVICES_TABLE_NAME} (ip, link, websocket_key) VALUES (:ip, :link, :websocket_key)");
conn.exec_drop(
insert_query,
params! {
"ip"=> ip,
"links" => link,
"link" => link,
"websocket_key" => websocket_key
},
)
Expand All @@ -49,7 +49,7 @@ pub fn insert_service(
// websocket_key: String,
// ) -> mysql::error::Result<u64> {
// let update_query = format!(
// "UPDATE {SERVICES_TABLE_NAME} SET links = CONCAT(links, ',{}') WHERE websocket_key = :websocket_key",
// "UPDATE {SERVICES_TABLE_NAME} SET link = CONCAT(link, ',{}') WHERE websocket_key = :websocket_key",
// link
// );
// conn.exec_drop(
Expand All @@ -66,23 +66,23 @@ pub struct Service {
pub id: String,
pub ip: String,
pub websocket_key: String,
pub links: String,
pub link: String,
}

pub fn get_service(
conn: &mut mysql::PooledConn,
ip: Option<String>,
search: Option<String>,
) -> Result<Vec<Service>, PersistenceError> {
let query_fn = |(id, ip, websocket_key, links)| Service {
let query_fn = |(id, ip, link, websocket_key)| Service {
id,
ip,
link,
websocket_key,
links,
};

match ip {
Some(ip) => {
let select_query = format!("SELECT * FROM {SERVICES_TABLE_NAME} WHERE ip = '{ip}'");
match search {
Some(search) => {
let select_query = format!("SELECT * FROM {SERVICES_TABLE_NAME} WHERE CONCAT(id, ip, link, websocket_key) LIKE '%{search}%'");
let data = conn.query_map(select_query, query_fn)?;
Ok(data)
}
Expand Down
19 changes: 8 additions & 11 deletions crates/server/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,17 @@ pub async fn ws_index(
resp
}

#[get("/client/{ip}")]
pub async fn clients(
path: web::Path<String>,
data: web::Data<mysql::Pool>,
) -> actix_web::Result<impl Responder> {
let ip = path.into_inner();
let mut conn = data.get_conn().unwrap();
let services = web::block(move || get_service(&mut conn, Some(ip))).await??;
Ok(web::Json(services))
#[derive(Deserialize)]
pub struct ClientQuery {
pub search: Option<String>,
}

#[get("/client")]
pub async fn clients_by_ip(data: web::Data<mysql::Pool>) -> actix_web::Result<impl Responder> {
pub async fn clients(
query: web::Query<ClientQuery>,
data: web::Data<mysql::Pool>,
) -> actix_web::Result<impl Responder> {
let mut conn = data.get_conn().unwrap();
let services = web::block(move || get_service(&mut conn, None)).await??;
let services = web::block(move || get_service(&mut conn, query.search.clone())).await??;
Ok(web::Json(services))
}

0 comments on commit d920230

Please sign in to comment.