You've already forked AstralRinth
forked from didirus/AstralRinth
Rewrite the app (#23)
* chore: Removed everything not needed, and added base for rewrite feat(error_handling): Added 404 general cache feat(index): Added informations about the app in the / route. * feat(indexing): Brought back the indexing, with conditions to make it easier * fix: Fixed build error with a forgotten call * feat: Add Docker development enviroment (#19) * ci: add a *lot* of new actions * fix: rename linting action * fix: invalid yaml begone(?) * ci: Added cache to speed up build times * fix(ci): 🦀ed the yaml errors * fix(ci): fixed a missing hyphen * ci: Added matrix of rust versions, and changed way to install rust toolchain * fix(ci): Added names to build with the matrix so it's easier to find the source of the problem * style(ci): Added eof lines * refactor: Finished moving the search.rs file to a separate module. * Search Endpoint * refactor: Moved around functions and struct for a better understanding of what it does. * chore: Change env default settings to resolve conversation * refactor: Removed #[use_macros] fix: Fixed meilisearch address from env * chore: Added email to Aeledfyr * fix: Brought back the dotenv variables * style: Ran `cargo fmt` Co-authored-by: Charalampos Fanoulis <charalampos.fanoulis@gmail.com> Co-authored-by: Jai A <jai.a@tuta.io>
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
use mongodb::options::ClientOptions;
|
||||
use mongodb::Client;
|
||||
use mongodb::error::Error;
|
||||
|
||||
pub async fn connect() -> Result<Client, Error> {
|
||||
info!("Initializing database connection");
|
||||
|
||||
let mut client_options = ClientOptions::parse("mongodb://localhost:27017").await?;
|
||||
client_options.app_name = Some("Actix Web Server".to_string());
|
||||
|
||||
Client::with_options(client_options)
|
||||
}
|
||||
@@ -1,6 +1,19 @@
|
||||
mod database;
|
||||
mod models;
|
||||
pub mod models;
|
||||
mod mongo_database;
|
||||
|
||||
pub use database::connect;
|
||||
pub use models::Mod;
|
||||
pub use models::Version;
|
||||
pub use mongo_database::connect;
|
||||
use thiserror::Error;
|
||||
|
||||
type Result<T> = std::result::Result<T, DatabaseError>;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum DatabaseError {
|
||||
#[error("Impossible to find document")]
|
||||
NotFound(),
|
||||
#[error("BSON deserialization error")]
|
||||
BsonError(#[from] bson::de::Error),
|
||||
#[error("Local database error")]
|
||||
LocalDatabaseError(#[from] mongodb::error::Error),
|
||||
}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Mod {
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub published: String,
|
||||
pub author: String,
|
||||
pub downloads: i32,
|
||||
pub categories: Vec<String>,
|
||||
pub body_path: String,
|
||||
pub icon_path: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Version {
|
||||
pub version_id: i32,
|
||||
pub mod_id: i32,
|
||||
pub title: String,
|
||||
pub changelog_path: String,
|
||||
pub files_path: Vec<String>,
|
||||
pub date_published: String,
|
||||
pub author: String,
|
||||
pub downloads: i32,
|
||||
pub dependencies: Vec<String>,
|
||||
pub game_versions: Vec<String>,
|
||||
}
|
||||
27
src/database/models/mod.rs
Normal file
27
src/database/models/mod.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
mod mod_item;
|
||||
mod version_item;
|
||||
|
||||
use crate::database::DatabaseError::NotFound;
|
||||
use crate::database::Result;
|
||||
use async_trait::async_trait;
|
||||
use bson::doc;
|
||||
use bson::Document;
|
||||
pub use mod_item::Mod;
|
||||
use mongodb::Database;
|
||||
pub use version_item::Version;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Item {
|
||||
fn get_collection() -> &'static str;
|
||||
async fn get_by_id(client: Database, id: &str) -> Result<Box<Self>> {
|
||||
let filter = doc! { "_id": id };
|
||||
let collection = client.collection(Self::get_collection());
|
||||
let doc: Document = match collection.find_one(filter, None).await? {
|
||||
Some(e) => e,
|
||||
None => return Err(NotFound()),
|
||||
};
|
||||
let elem: Box<Self> = Self::from_doc(doc)?;
|
||||
Ok(elem)
|
||||
}
|
||||
fn from_doc(elem: Document) -> Result<Box<Self>>;
|
||||
}
|
||||
27
src/database/models/mod_item.rs
Normal file
27
src/database/models/mod_item.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use crate::database::models::Item;
|
||||
use crate::database::Result;
|
||||
use bson::{Bson, Document};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Mod {
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub published: String,
|
||||
pub author: String,
|
||||
pub downloads: i32,
|
||||
pub categories: Vec<String>,
|
||||
pub body_path: String,
|
||||
pub icon_path: String,
|
||||
}
|
||||
impl Item for Mod {
|
||||
fn get_collection() -> &'static str {
|
||||
"mods"
|
||||
}
|
||||
|
||||
fn from_doc(elem: Document) -> Result<Box<Mod>> {
|
||||
let result: Mod = bson::from_bson(Bson::from(elem))?;
|
||||
Ok(Box::from(result))
|
||||
}
|
||||
}
|
||||
28
src/database/models/version_item.rs
Normal file
28
src/database/models/version_item.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use crate::database::models::Item;
|
||||
use crate::database::Result;
|
||||
use bson::{Bson, Document};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Version {
|
||||
pub version_id: i32,
|
||||
pub mod_id: i32,
|
||||
pub title: String,
|
||||
pub changelog_path: String,
|
||||
pub files_path: Vec<String>,
|
||||
pub date_published: String,
|
||||
pub author: String,
|
||||
pub downloads: i32,
|
||||
pub dependencies: Vec<String>,
|
||||
pub game_versions: Vec<String>,
|
||||
}
|
||||
impl Item for Version {
|
||||
fn get_collection() -> &'static str {
|
||||
"versions"
|
||||
}
|
||||
|
||||
fn from_doc(elem: Document) -> Result<Box<Version>> {
|
||||
let version: Version = bson::from_bson(Bson::from(elem))?;
|
||||
Ok(Box::from(version))
|
||||
}
|
||||
}
|
||||
13
src/database/mongo_database.rs
Normal file
13
src/database/mongo_database.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use log::info;
|
||||
use mongodb::error::Error;
|
||||
use mongodb::options::ClientOptions;
|
||||
use mongodb::Client;
|
||||
|
||||
pub async fn connect() -> Result<Client, Error> {
|
||||
info!("Initializing database connection");
|
||||
|
||||
let mut client_options = ClientOptions::parse(&dotenv::var("PORT").unwrap()).await?;
|
||||
client_options.app_name = Some("labrinth".to_string());
|
||||
|
||||
Client::with_options(client_options)
|
||||
}
|
||||
Reference in New Issue
Block a user