aboutsummaryrefslogtreecommitdiff
path: root/src/modules
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-04-06 01:53:22 -0700
committerFuwn <[email protected]>2023-04-06 01:53:22 -0700
commit9235be2eedc2c4f3a78cb704b0a107f33607e530 (patch)
treefc927ec6d5a58d2e995cf6ff80ae2804a2265531 /src/modules
parentdeps(windmark): bump 0.2.5 -> 0.3.1 (diff)
downloadlocus-9235be2eedc2c4f3a78cb704b0a107f33607e530.tar.xz
locus-9235be2eedc2c4f3a78cb704b0a107f33607e530.zip
feat: native async reqwest calls
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/api/sydney.rs51
-rw-r--r--src/modules/router/module.rs10
-rw-r--r--src/modules/router/ticker.rs2
-rw-r--r--src/modules/router/translate/module.rs2
-rw-r--r--src/modules/stocks.rs90
-rw-r--r--src/modules/uptime.rs2
6 files changed, 82 insertions, 75 deletions
diff --git a/src/modules/api/sydney.rs b/src/modules/api/sydney.rs
index 20f8faa..e66f91d 100644
--- a/src/modules/api/sydney.rs
+++ b/src/modules/api/sydney.rs
@@ -27,34 +27,37 @@ pub fn module(router: &mut windmark::Router) {
"/api/sydney/version",
"Sydney's version",
move |context| {
- let mut content = "0.0.0".to_string();
-
- if let Ok(response) = reqwest::blocking::Client::new()
- .get("https://api.github.com/repos/gemrest/sydney/tags")
- .header(
- "User-Agent",
- format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
- )
- .send()
- {
- if let Ok(response_content) = response.json::<Vec<Tags>>() {
- let response_content: Vec<Tags> = response_content;
-
- if let Some(first_tag) = response_content.get(0) {
- content = first_tag.name().clone();
- }
-
- if let Some(just_tag) = content.get(1..) {
- content = just_tag.to_string();
+ async move {
+ let mut content = "0.0.0".to_string();
+
+ if let Ok(response) = reqwest::Client::new()
+ .get("https://api.github.com/repos/gemrest/sydney/tags")
+ .header(
+ "User-Agent",
+ format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
+ )
+ .send()
+ .await
+ {
+ if let Ok(response_content) = response.json::<Vec<Tags>>().await {
+ let response_content: Vec<Tags> = response_content;
+
+ if let Some(first_tag) = response_content.get(0) {
+ content = first_tag.name().clone();
+ }
+
+ if let Some(just_tag) = content.get(1..) {
+ content = just_tag.to_string();
+ }
}
}
- }
- crate::route::cache(&context, &content);
+ crate::route::cache(&context, &content);
- windmark::Response::success(content)
- .with_mime("text/plain")
- .clone()
+ windmark::Response::success(content)
+ .with_mime("text/plain")
+ .clone()
+ }
},
);
}
diff --git a/src/modules/router/module.rs b/src/modules/router/module.rs
index 34d20db..cd7c862 100644
--- a/src/modules/router/module.rs
+++ b/src/modules/router/module.rs
@@ -19,25 +19,25 @@
use windmark::context::HookContext;
pub fn module(router: &mut windmark::Router) {
- router.set_pre_route_callback(move |context: HookContext<'_>| {
+ router.set_pre_route_callback(move |context: HookContext| {
info!(
"accepted connection from {} to {}",
- context.tcp.peer_addr().unwrap().ip(),
+ context.peer_address.unwrap().ip(),
context.url.to_string(),
);
super::ticker::module(&context);
});
router.set_post_route_callback({
- |context: HookContext<'_>, response: &mut windmark::Response| {
+ |context: HookContext, response: &mut windmark::Response| {
info!(
"closed connection from {} to {}",
- context.tcp.peer_addr().unwrap().ip(),
+ context.peer_address.unwrap().ip(),
context.url.to_string(),
);
if let Some(language) =
- windmark::utilities::queries_from_url(context.url).get("translate")
+ windmark::utilities::queries_from_url(&context.url).get("translate")
{
if super::translate::module(&context, response, language).is_err() {
response.content = format!(
diff --git a/src/modules/router/ticker.rs b/src/modules/router/ticker.rs
index d193c9b..a5384db 100644
--- a/src/modules/router/ticker.rs
+++ b/src/modules/router/ticker.rs
@@ -18,7 +18,7 @@
use crate::DATABASE;
-pub fn module(context: &windmark::context::HookContext<'_>) {
+pub fn module(context: &windmark::context::HookContext) {
let url_path = if context.url.path().is_empty() {
"/"
} else {
diff --git a/src/modules/router/translate/module.rs b/src/modules/router/translate/module.rs
index 4c105e1..70e4829 100644
--- a/src/modules/router/translate/module.rs
+++ b/src/modules/router/translate/module.rs
@@ -20,7 +20,7 @@ use super::deepl::translate;
use crate::modules::router::translate::deepl::language_code_to_language_name;
pub fn module<S>(
- context: &windmark::context::HookContext<'_>,
+ context: &windmark::context::HookContext,
response: &mut windmark::Response,
language: S,
) -> Result<(), serde_json::Error>
diff --git a/src/modules/stocks.rs b/src/modules/stocks.rs
index 108e792..ff1a77d 100644
--- a/src/modules/stocks.rs
+++ b/src/modules/stocks.rs
@@ -72,19 +72,19 @@ impl Quote {
}
}
-fn symbol_to_string(symbol: &str) -> String {
+async fn symbol_to_string(symbol: &str) -> String {
let mut quote = None;
// https://github.com/seanmonstar/reqwest/issues/1017#issuecomment-1157260218
- if let Ok(response) = tokio::task::block_in_place(|| {
- reqwest::blocking::get(&format!(
- "https://finnhub.io/api/v1/quote?symbol={}&token={}",
- symbol,
- std::env::var("FINNHUB_TOKEN")
- .expect("could not locate FINNHUB_TOKEN environment variable")
- ))
- }) {
- if let Ok(response_content) = response.json::<Quote>() {
+ if let Ok(response) = reqwest::get(&format!(
+ "https://finnhub.io/api/v1/quote?symbol={}&token={}",
+ symbol,
+ std::env::var("FINNHUB_TOKEN")
+ .expect("could not locate FINNHUB_TOKEN environment variable")
+ ))
+ .await
+ {
+ if let Ok(response_content) = response.json::<Quote>().await {
if response_content.dp.is_some() {
quote = Some(response_content);
} else {
@@ -136,15 +136,17 @@ pub fn module(router: &mut windmark::Router) {
"/stocks",
"Explore and search the stock market using Gemini!",
|context| {
- success!(
+ async move {
+ success!(
format!(
"# The Stock Market\n\n=> /stocks/search Symbol Search\n=> /stocks/referrals Referrals\n=> /cryptocurrency Cryptocurrency Dashboard\n=> /stocks/telegram Telegram Groups\n\n## Popular \
Symbols\n\n### AAPL\n\n{}\n\n### TSLA\n\n{}\n\n## Credits\n\nFinancial data provided by\n\n=> https://finnhub.io/ Finnhub",
- symbol_to_string("AAPL"),
- symbol_to_string("TSLA")
+ symbol_to_string("AAPL").await,
+ symbol_to_string("TSLA").await
),
context
)
+ }
},
);
@@ -153,47 +155,49 @@ pub fn module(router: &mut windmark::Router) {
"/stocks/search",
"Search for a specific symbol",
|context| {
- let mut symbol = context.url.query().unwrap_or("Symbol Search");
+ async move {
+ let mut symbol = context.url.query().unwrap_or("Symbol Search");
- if symbol.is_empty() {
- symbol = "Symbol Search";
- }
-
- let mut response = format!(
- "# {}\n\n=> /stocks Dashboard\n=> /cryptocurrency Cryptocurrency \
- Dashboard\n=> /stocks/telegram Telegram Groups\n=> /stocks/search \
- Search",
- symbol
- );
-
- if symbol != "Symbol Search" {
- if let Some(query) = context.url.query_pairs().next() {
- if query.0 == "action" && query.1 == "go" {
- return windmark::Response::input(
- "Which symbol would you like to explore?",
- );
- }
-
- let symbol = query.0;
+ if symbol.is_empty() {
+ symbol = "Symbol Search";
+ }
- if symbol != "Symbol Search" {
- response = format!(
- "{}\n\nYou searched for \"{}\"!\n\n## Key Statistics\n\n{}",
- response,
- symbol,
- symbol_to_string(&symbol)
- );
+ let mut response = format!(
+ "# {}\n\n=> /stocks Dashboard\n=> /cryptocurrency Cryptocurrency \
+ Dashboard\n=> /stocks/telegram Telegram Groups\n=> /stocks/search \
+ Search",
+ symbol
+ );
+
+ if symbol != "Symbol Search" {
+ if let Some(query) = context.url.query_pairs().next() {
+ if query.0 == "action" && query.1 == "go" {
+ return windmark::Response::input(
+ "Which symbol would you like to explore?",
+ );
+ }
+
+ let symbol = query.0;
+
+ if symbol != "Symbol Search" {
+ response = format!(
+ "{}\n\nYou searched for \"{}\"!\n\n## Key Statistics\n\n{}",
+ response,
+ symbol,
+ symbol_to_string(&symbol).await
+ );
+ }
}
}
- }
- success!(
+ success!(
format!(
"{}\n\n## Credits\n\nFinancial data provided by\n\n=> https://finnhub.io/ Finnhub",
response
),
context
)
+ }
},
);
}
diff --git a/src/modules/uptime.rs b/src/modules/uptime.rs
index 9b3d2b5..67161c5 100644
--- a/src/modules/uptime.rs
+++ b/src/modules/uptime.rs
@@ -29,7 +29,7 @@ pub fn module(router: &mut windmark::Router) {
"/uptime",
"The uptime of Locus (A.K.A., The Locus Epoch). (\\?[s|ms|mu|ns]?)?",
move |context| {
- let response = windmark::utilities::queries_from_url(context.url)
+ let response = windmark::utilities::queries_from_url(&context.url)
.get("unit")
.map_or_else(
|| UPTIME.elapsed().as_nanos().to_string(),