chore: rename data::Data to ctx::Ctx

This commit is contained in:
Aravinth Manivannan 2022-05-17 19:57:57 +05:30
parent fd0b2f5d6d
commit 65eab4e488
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
4 changed files with 21 additions and 18 deletions

View file

@ -31,12 +31,12 @@ lazy_static! {
const CLIENT_TIMEOUT: u64 = 60; const CLIENT_TIMEOUT: u64 = 60;
#[derive(Clone)] #[derive(Clone)]
pub struct Data { pub struct Ctx {
pub client: Client, pub client: Client,
pub settings: Settings, pub settings: Settings,
} }
impl Data { impl Ctx {
pub async fn new(settings: Settings) -> Arc<Self> { pub async fn new(settings: Settings) -> Arc<Self> {
let timeout = Duration::new(CLIENT_TIMEOUT, 0); let timeout = Duration::new(CLIENT_TIMEOUT, 0);
let client = ClientBuilder::new() let client = ClientBuilder::new()

View file

@ -15,7 +15,7 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
pub mod data; pub mod ctx;
pub mod db; pub mod db;
pub mod forge; pub mod forge;
pub mod settings; pub mod settings;

View file

@ -24,10 +24,10 @@ use db_core::prelude::*;
use forge_core::prelude::*; use forge_core::prelude::*;
use gitea::Gitea; use gitea::Gitea;
use crate::data::Data; use crate::ctx::Ctx;
use crate::db::BoxDB; use crate::db::BoxDB;
impl Data { impl Ctx {
pub async fn crawl(&self, instance_url: &str, db: &BoxDB) { pub async fn crawl(&self, instance_url: &str, db: &BoxDB) {
let gitea = Gitea::new(Url::parse(instance_url).unwrap(), self.client.clone()); let gitea = Gitea::new(Url::parse(instance_url).unwrap(), self.client.clone());
let mut page = 1; let mut page = 1;
@ -56,7 +56,7 @@ impl Data {
for (username, u) in res.users.iter() { for (username, u) in res.users.iter() {
if !db if !db
.user_exists(&username, Some(&gitea.get_hostname())) .user_exists(username, Some(gitea.get_hostname()))
.await .await
.unwrap() .unwrap()
{ {
@ -66,9 +66,15 @@ impl Data {
} }
for r in res.repos.iter() { for r in res.repos.iter() {
if !db
.repository_exists(&r.name, &r.owner.username, r.hostname)
.await
.unwrap()
{
let msg = r.into(); let msg = r.into();
db.create_repository(&msg).await.unwrap(); db.create_repository(&msg).await.unwrap();
} }
}
sleep_fut.await.unwrap(); sleep_fut.await.unwrap();
page += 1; page += 1;
@ -88,8 +94,8 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn crawl_gitea() { async fn crawl_gitea() {
let (db, data) = sqlx_sqlite::get_data().await; let (db, ctx) = sqlx_sqlite::get_ctx().await;
let res = data.crawl(GITEA_HOST, &db).await; ctx.crawl(GITEA_HOST, &db).await;
let hostname = get_hostname(&Url::parse(GITEA_HOST).unwrap()); let hostname = get_hostname(&Url::parse(GITEA_HOST).unwrap());
assert!(db.forge_exists(&hostname).await.unwrap()); assert!(db.forge_exists(&hostname).await.unwrap());
assert!(db assert!(db

View file

@ -19,30 +19,27 @@
use std::env; use std::env;
pub use std::sync::Arc; pub use std::sync::Arc;
use serde::Serialize; use crate::ctx::Ctx;
use crate::data::Data;
pub use crate::db::BoxDB; pub use crate::db::BoxDB;
use crate::settings::{DBType, Settings}; use crate::settings::{DBType, Settings};
//pub mod sqlx_postgres { //pub mod sqlx_postgres {
// use super::*; // use super::*;
// //
// pub async fn get_data() -> (BoxDB, Arc<Data>) { // pub async fn get_ctx() -> (BoxDB, Arc<Ctx>) {
// let url = env::var("POSTGRES_DATABASE_URL").unwrap(); // let url = env::var("POSTGRES_DATABASE_URL").unwrap();
// let mut settings = Settings::new().unwrap(); // let mut settings = Settings::new().unwrap();
// settings.database.url = url.clone(); // settings.database.url = url.clone();
// settings.database.database_type = DBType::Postgres; // settings.database.database_type = DBType::Postgres;
// let db = pg::get_data(Some(settings.clone())).await; // let db = pg::get_data(Some(settings.clone())).await;
// (db, Data::new(Some(settings))) // (db, Ctx::new(Some(settings)))
// }
// } // }
pub mod sqlx_sqlite { pub mod sqlx_sqlite {
use super::*; use super::*;
use crate::db::sqlite; use crate::db::sqlite;
pub async fn get_data() -> (BoxDB, Arc<Data>) { pub async fn get_ctx() -> (BoxDB, Arc<Ctx>) {
let url = env::var("SQLITE_DATABASE_URL").unwrap(); let url = env::var("SQLITE_DATABASE_URL").unwrap();
env::set_var("DATABASE_URL", &url); env::set_var("DATABASE_URL", &url);
println!("found db url: {url}"); println!("found db url: {url}");
@ -50,6 +47,6 @@ pub mod sqlx_sqlite {
settings.database.url = url.clone(); settings.database.url = url.clone();
settings.database.database_type = DBType::Sqlite; settings.database.database_type = DBType::Sqlite;
let db = sqlite::get_data(Some(settings.clone())).await; let db = sqlite::get_data(Some(settings.clone())).await;
(db, Data::new(settings).await) (db, Ctx::new(settings).await)
} }
} }