From 35aa155c0e6f85c3ecf61000fff6810fe7471ae8 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 6 Apr 2022 10:08:15 +0530 Subject: [PATCH] feat: initialize app data and apply clippy lints --- src/data.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/settings.rs | 10 +++++----- 3 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 src/data.rs diff --git a/src/data.rs b/src/data.rs new file mode 100644 index 0000000..4953349 --- /dev/null +++ b/src/data.rs @@ -0,0 +1,53 @@ +/* + * ForgeFlux StarChart - A federated software forge spider + * Copyright © 2022 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +use std::sync::Arc; +use std::time::Duration; + +use lazy_static::lazy_static; +use reqwest::{Client, ClientBuilder}; + +use crate::settings::Settings; +use crate::{DOMAIN, PKG_NAME, VERSION}; + +lazy_static! { + pub static ref USER_AGENT: String = format!("{VERSION}---{PKG_NAME}---{DOMAIN}"); +} +/// in seconds +const CLIENT_TIMEOUT: u64 = 60; + +#[derive(Clone)] +pub struct Data { + pub client: Client, + pub settings: Settings, +} + +impl Data { + pub async fn new(settings: Settings) -> Arc { + let timeout = Duration::new(CLIENT_TIMEOUT, 0); + let client = ClientBuilder::new() + .user_agent(&*USER_AGENT) + .use_rustls_tls() + .timeout(timeout) + .connect_timeout(timeout) + .tcp_keepalive(timeout) + .build() + .unwrap(); + + Arc::new(Self { client, settings }) + } +} diff --git a/src/main.rs b/src/main.rs index 9aec316..7a96248 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +pub mod data; pub mod gitea; pub mod settings; pub mod utils; diff --git a/src/settings.rs b/src/settings.rs index 2a5cfdf..923647a 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -121,12 +121,12 @@ impl DatabaseBuilder { let name = path.next().expect("no database name").to_string(); let database_type = DBType::from_url(url).unwrap(); - let port; - if database_type == DBType::Sqlite { - port = 0; + let port = if database_type == DBType::Sqlite { + 0 } else { - port = url.port().expect("Enter database port").into(); - } + url.port().expect("Enter database port").into() + }; + DatabaseBuilder { port, hostname: url.host().expect("Enter database host").to_string(),