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;
#[derive(Clone)]
pub struct Data {
pub struct Ctx {
pub client: Client,
pub settings: Settings,
}
impl Data {
impl Ctx {
pub async fn new(settings: Settings) -> Arc<Self> {
let timeout = Duration::new(CLIENT_TIMEOUT, 0);
let client = ClientBuilder::new()

View file

@ -15,7 +15,7 @@
* 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/>.
*/
pub mod data;
pub mod ctx;
pub mod db;
pub mod forge;
pub mod settings;

View file

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

View file

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