Files
Modrinth-plus/src/main.rs

140 lines
3.5 KiB
Rust
Raw Normal View History

2020-05-09 22:11:48 -07:00
#[macro_use]
extern crate serde_json;
2020-05-10 22:30:28 -07:00
use actix_web::{web, web::Data, App, HttpRequest, HttpResponse, HttpServer, Responder, get, post};
2020-05-09 22:11:48 -07:00
use handlebars::Handlebars;
2020-05-10 22:30:28 -07:00
use meilisearch_sdk::{document::*, indexes::*, client::*, search::*};
use serde::{Serialize, Deserialize};
use actix_files as fs;
#[derive(Serialize, Deserialize, Debug)]
struct Mod {
mod_id: usize,
title: String,
description: String,
}
impl Document for Mod {
type UIDType = usize;
fn get_uid(&self) -> &Self::UIDType {
&self.mod_id
}
}
2020-05-09 22:11:48 -07:00
#[derive(Deserialize)]
pub struct SearchRequest {
q: Option<String>,
2020-05-10 22:30:28 -07:00
f: Option<String>,
2020-05-09 22:11:48 -07:00
}
#[post("search")]
2020-05-10 22:30:28 -07:00
async fn search_post(web::Query(info): web::Query<SearchRequest>) -> HttpResponse {
let results = search(web::Query(info));
2020-05-09 22:11:48 -07:00
2020-05-10 22:30:28 -07:00
let data = json!({
"results": results,
});
2020-05-09 22:11:48 -07:00
HttpResponse::Ok().body(data)
}
#[get("search")]
2020-05-10 22:30:28 -07:00
async fn search_get(web::Query(info): web::Query<SearchRequest>, hb: Data<Handlebars<'_>>) -> HttpResponse {
let results = search(web::Query(info));
2020-05-09 22:11:48 -07:00
let data = json!({
"results": results,
});
let body = hb.render("search", &data).unwrap();
HttpResponse::Ok().body(body)
}
2020-05-10 22:30:28 -07:00
fn search(web::Query(info): web::Query<SearchRequest>) -> Vec<Mod> {
let client = Client::new("http://localhost:7700", "");
let mut search_query = "".to_string();
let mut filters = "".to_string();
2020-05-09 22:11:48 -07:00
if let Some(q) = info.q {
search_query = q;
}
2020-05-10 22:30:28 -07:00
if let Some(f) = info.f {
filters = f;
}
2020-05-09 22:11:48 -07:00
2020-05-10 22:30:28 -07:00
let mut query = Query::new(&search_query).with_limit(10);
2020-05-09 22:11:48 -07:00
2020-05-10 22:30:28 -07:00
if !filters.is_empty() {
query = Query::new(&search_query).with_limit(10).with_filters(&filters);
2020-05-09 22:11:48 -07:00
}
2020-05-10 22:30:28 -07:00
client.get_index("mods").unwrap().search::<Mod>(&query).unwrap().hits
2020-05-09 22:11:48 -07:00
}
#[get("/")]
async fn index(hb: web::Data<Handlebars<'_>>) -> HttpResponse {
let data = json!({
"name": "Handlebars"
});
let body = hb.render("index", &data).unwrap();
HttpResponse::Ok().body(body)
}
#[actix_rt::main]
2020-05-10 22:30:28 -07:00
async fn main() -> std::io::Result<()> {
2020-05-09 22:11:48 -07:00
//Handlebars
let mut handlebars = Handlebars::new();
handlebars
2020-05-10 22:30:28 -07:00
.register_templates_directory(".hbs", "./templates")
2020-05-09 22:11:48 -07:00
.unwrap();
let handlebars_ref = web::Data::new(handlebars);
//Search
2020-05-10 22:30:28 -07:00
let client = Client::new("http://localhost:7700", "");
let mut mods = client.get_or_create("mods").unwrap();
mods.add_documents(vec![
Mod {
mod_id: 0,
title: String::from("Magic Mod"),
description: String::from("An illustrious magic mod for magical wizards"),
},
Mod {
mod_id: 1,
title: String::from("Tech Mod"),
description: String::from("An technological mod for complete NERDS"),
},
Mod {
mod_id: 2,
title: String::from("Hood Mod"),
description: String::from("A hood mod to roleplay as if you were a real street person. Some adventure stuff too"),
},
Mod {
mod_id: 3,
title: String::from("Adventure Mod"),
description: String::from("An epic gamer adventure mod for epic adventure gamers"),
},
], Some("mod_id")).unwrap();
2020-05-09 22:11:48 -07:00
//Init App
HttpServer::new(move || {
App::new()
.app_data(handlebars_ref.clone())
2020-05-10 22:30:28 -07:00
.service(fs::Files::new("/static", "./static").show_files_listing())
2020-05-09 22:11:48 -07:00
.service(index)
2020-05-10 22:30:28 -07:00
.service(search_get)
2020-05-09 22:11:48 -07:00
.service(search_post)
})
.bind("127.0.0.1:8000")?
.run()
2020-05-10 22:30:28 -07:00
.await
2020-05-09 22:11:48 -07:00
}