diff options
author | ache <ache@ache.one> | 2023-04-22 00:06:39 +0200 |
---|---|---|
committer | ache <ache@ache.one> | 2023-04-22 00:06:39 +0200 |
commit | d545006de8ab228e75afffd5cb1f3d7f71026062 (patch) | |
tree | c50ac8c7dcc27934c1fa0c8a31145db209277a52 | |
parent | Fix responce (diff) |
Suport for aliases
-rw-r--r-- | src/main.rs | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 395b853..73b4cb3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::convert::Infallible; use std::net::{IpAddr, SocketAddr, Ipv4Addr}; +use std::collections::HashMap; use std::str::FromStr; use std::sync::mpsc::{Sender, channel}; use std::{thread, time::Duration}; @@ -81,7 +82,7 @@ fn handle(req: Request<Body>, addr: SocketAddr, tx: Sender<VustMessage>, config: Method::PUT => VustQuery::Commit, Method::GET => VustQuery::Get, Method::OPTIONS => { - return respond(StatusCode::OK, "".to_string()); + return respond(StatusCode::OK, "WHAT ?".to_string()); }, _ => { return respond(StatusCode::METHOD_NOT_ALLOWED, "Tu t'attends à quoi au juste ?".to_string()); @@ -98,11 +99,17 @@ fn handle(req: Request<Body>, addr: SocketAddr, tx: Sender<VustMessage>, config: return respond(StatusCode::BAD_REQUEST, "Tu t'attends à quoi au juste ?".to_string()); } + let article_key_db = if config.aliases.contains_key(&path) { + config.aliases.get(&path).unwrap().to_owned() + } else { + path + }; + let (ttx , rx) = channel(); let message = VustMessage{ req_type: query_type, ip: addr, - path, + path: article_key_db, res: ttx, }; @@ -114,7 +121,7 @@ fn handle(req: Request<Body>, addr: SocketAddr, tx: Sender<VustMessage>, config: } } -fn do_get(tr: &Transaction, ip : SocketAddr, path : String) -> Response<Body> { +fn do_get(tr: &Transaction, path : String) -> Response<Body> { for _ in [1..3] { let mut req_prepared = tr.prepare("SELECT cast(SUM(number) as text) FROM likes WHERE path = ?").unwrap(); @@ -211,6 +218,7 @@ struct Config { port: u16, cors_hosts: String, list_articles: Vec<String>, + aliases: HashMap<String, String>, } fn get_config() -> String { @@ -230,7 +238,10 @@ fn get_config() -> String { 'retour-sur-laoc-2021-semaine-1', '2FA-discord-sur-pc', 'duckduckgo-google-en-mieux', + 'c-language-quirks', ] + + aliases.c-language-quirks='bizarreries-du-langage-c' "#.to_string(); }; @@ -286,7 +297,7 @@ async fn main() { continue; }, VustQuery::Get => { - let res = recv.res.send(do_get(&tr, recv.ip, recv.path)); + let res = recv.res.send(do_get(&tr, recv.path)); if res.is_ok() || res.is_err() { continue; } |