You've already forked AstralRinth
forked from didirus/AstralRinth
Migrate to MongoDB
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::prelude::*;
|
||||
use dotenv::dotenv;
|
||||
use std::env;
|
||||
use mongodb::options::ClientOptions;
|
||||
use mongodb::Client;
|
||||
use mongodb::error::Error;
|
||||
|
||||
pub fn connect() -> PgConnection {
|
||||
dotenv().ok();
|
||||
pub async fn connect() -> Result<Client, Error> {
|
||||
info!("Initializing database connection");
|
||||
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set!");
|
||||
PgConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
||||
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,11 +1,11 @@
|
||||
use chrono::NaiveDate;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Queryable)]
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Mod {
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub published: NaiveDate,
|
||||
pub published: String,
|
||||
pub author: String,
|
||||
pub downloads: i32,
|
||||
pub categories: Vec<String>,
|
||||
@@ -13,14 +13,14 @@ pub struct Mod {
|
||||
pub icon_path: String,
|
||||
}
|
||||
|
||||
#[derive(Queryable)]
|
||||
#[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: NaiveDate,
|
||||
pub date_published: String,
|
||||
pub author: String,
|
||||
pub downloads: i32,
|
||||
pub dependencies: Vec<String>,
|
||||
|
||||
18
src/main.rs
18
src/main.rs
@@ -2,19 +2,25 @@
|
||||
extern crate serde_json;
|
||||
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
extern crate bson;
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
use actix_files as fs;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use handlebars::*;
|
||||
use actix_web::middleware::Logger;
|
||||
use env_logger::Env;
|
||||
|
||||
mod database;
|
||||
mod helpers;
|
||||
mod routes;
|
||||
mod schema;
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
env_logger::from_env(Env::default().default_filter_or("info")).init();
|
||||
|
||||
//Handlebars
|
||||
let mut handlebars = Handlebars::new();
|
||||
|
||||
@@ -26,13 +32,17 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
let handlebars_ref = web::Data::new(handlebars);
|
||||
|
||||
let database = database::connect();
|
||||
routes::index_mods(database).await;
|
||||
let client = database::connect().await.unwrap();
|
||||
routes::index_mods(client.clone()).await;
|
||||
|
||||
info!("Starting Actix HTTP server!");
|
||||
//Init App
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(Logger::default())
|
||||
.wrap(Logger::new("%a %{User-Agent}i"))
|
||||
.app_data(handlebars_ref.clone())
|
||||
.app_data(client.clone())
|
||||
.service(fs::Files::new("/static", "./static").show_files_listing())
|
||||
.service(routes::index_get)
|
||||
.service(routes::search_post)
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
extern crate diesel;
|
||||
|
||||
use actix_web::{get, post, web, web::Data, HttpResponse};
|
||||
use handlebars::*;
|
||||
use meilisearch_sdk::{client::*, document::*, search::*};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::database::*;
|
||||
use diesel::prelude::*;
|
||||
|
||||
use futures::stream::StreamExt;
|
||||
use meilisearch_sdk::settings::Settings;
|
||||
use mongodb::error::Error;
|
||||
use bson::Bson;
|
||||
use mongodb::Cursor;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -158,45 +159,16 @@ fn search(web::Query(info): web::Query<SearchRequest>) -> Vec<SearchMod> {
|
||||
.hits
|
||||
}
|
||||
|
||||
pub async fn index_mods(conn: PgConnection) {
|
||||
use crate::schema::mods::dsl::*;
|
||||
use crate::schema::versions::dsl::*;
|
||||
|
||||
pub async fn index_mods(conn: mongodb::Client) -> Result<(), Error>{
|
||||
let client = Client::new("http://localhost:7700", "");
|
||||
let mut mods_index = client.get_or_create("mods").unwrap();
|
||||
|
||||
let results = mods.load::<Mod>(&conn).expect("Error loading mods!");
|
||||
let mut docs_to_add: Vec<SearchMod> = vec![];
|
||||
|
||||
for result in results {
|
||||
let mod_versions = versions
|
||||
.filter(mod_id.eq(result.id))
|
||||
.load::<Version>(&conn)
|
||||
.expect("Error loading versions!");
|
||||
info!("Indexing database mods!");
|
||||
|
||||
let mut mod_game_versions = vec![];
|
||||
|
||||
for version in mod_versions {
|
||||
mod_game_versions.extend(version.game_versions.clone())
|
||||
}
|
||||
|
||||
docs_to_add.push(SearchMod {
|
||||
mod_id: result.id,
|
||||
author: result.author,
|
||||
title: result.title,
|
||||
description: result.description,
|
||||
keywords: result.categories,
|
||||
versions: mod_game_versions,
|
||||
downloads: result.downloads,
|
||||
page_url: "".to_string(),
|
||||
icon_url: "".to_string(),
|
||||
author_url: "".to_string(),
|
||||
date_created: "".to_string(),
|
||||
date_modified: "".to_string(),
|
||||
latest_version: "".to_string(),
|
||||
empty: String::from("{}{}{}"),
|
||||
});
|
||||
}
|
||||
info!("Indexing curseforge mods!");
|
||||
|
||||
let body = reqwest::get("https://addons-ecs.forgesvc.net/api/v2/addon/search?categoryId=0&gameId=432&index=0&pageSize=10000§ionId=6&sort=5")
|
||||
.await.unwrap()
|
||||
@@ -237,9 +209,7 @@ pub async fn index_mods(conn: PgConnection) {
|
||||
"Technology" => mod_categories.push(String::from("technology")),
|
||||
"Processing" => mod_categories.push(String::from("technology")),
|
||||
"Player Transport" => mod_categories.push(String::from("technology")),
|
||||
"Energy, Fluid, and Item Transport" => {
|
||||
mod_categories.push(String::from("technology"))
|
||||
}
|
||||
"Energy, Fluid, and Item Transport" => { mod_categories.push(String::from("technology")) }
|
||||
"Food" => mod_categories.push(String::from("food")),
|
||||
"Farming" => mod_categories.push(String::from("food")),
|
||||
"Energy" => mod_categories.push(String::from("technology")),
|
||||
@@ -364,4 +334,6 @@ pub async fn index_mods(conn: PgConnection) {
|
||||
.with_ranking_rules(ranking_rules);
|
||||
|
||||
mods_index.set_settings(&write_settings).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
table! {
|
||||
mods (id) {
|
||||
id -> Int4,
|
||||
title -> Varchar,
|
||||
description -> Varchar,
|
||||
datepublished -> Date,
|
||||
author -> Varchar,
|
||||
downloads -> Int4,
|
||||
categories -> Array<Text>,
|
||||
body_path -> Varchar,
|
||||
icon_path -> Varchar,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
versions (version_id) {
|
||||
version_id -> Int4,
|
||||
mod_id -> Int4,
|
||||
title -> Varchar,
|
||||
changelog_path -> Varchar,
|
||||
files_path -> Array<Text>,
|
||||
date_published -> Date,
|
||||
author -> Varchar,
|
||||
downloads -> Int4,
|
||||
dependencies -> Array<Text>,
|
||||
game_versions -> Array<Text>,
|
||||
}
|
||||
}
|
||||
|
||||
allow_tables_to_appear_in_same_query!(mods, versions,);
|
||||
Reference in New Issue
Block a user