This commit is contained in:
JMARyA 2024-01-14 03:57:13 +01:00
commit e8e8d9d960
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
7 changed files with 3217 additions and 0 deletions

50
src/item.rs Normal file
View file

@ -0,0 +1,50 @@
use mongodb::{Collection, bson::doc};
#[derive(serde::Deserialize, serde::Serialize)]
pub struct ItemEntry {
pub name: String,
pub category: String
}
impl ItemEntry {
pub fn new(doc: mdq::Document) -> Self {
let name = std::path::Path::new(&doc.path).file_stem().unwrap().to_str().unwrap().to_string();
let category = doc.frontmatter.as_mapping().unwrap().get("category").unwrap().as_str().unwrap().to_string();
Self { name, category }
}
}
pub struct ItemDB {
index: mdq::Index,
mongodb: mongodb::Client
}
impl ItemDB {
pub async fn new(dir: &str, mongodb: &str) -> Self {
// scan for markdown item entries
let index = mdq::Index::new(dir, true);
let mongodb = mongodb::Client::with_uri_str(mongodb).await.unwrap();
for item in &index.documents {
let item = ItemEntry::new(item.clone());
log::info!("Adding item {} to DB", item.name);
let items: Collection<ItemEntry> = mongodb.database("cdb").collection("items");
items.insert_one(item, None).await.unwrap();
}
Self { index, mongodb }
}
pub fn get_item(&self, item: &str) -> Option<ItemEntry> {
self.index.documents.iter().map(|x| ItemEntry::new(x.clone())).find(|x| x.name == item)
}
pub fn items(&self) -> Vec<String> {
let mut ret = vec![];
for item in &self.index.documents {
let item = ItemEntry::new(item.clone());
ret.push(item.name);
}
ret
}
}

66
src/main.rs Normal file
View file

@ -0,0 +1,66 @@
use actix_web::{get, HttpRequest, Responder};
use maud::html;
mod item;
// ░░░░░░░░░░▀▀▀██████▄▄▄░░░░░░░░░░
// ░░░░░░░░░░░░░░░░░▀▀▀████▄░░░░░░░
// ░░░░░░░░░░▄███████▀░░░▀███▄░░░░░
// ░░░░░░░░▄███████▀░░░░░░░▀███▄░░░
// ░░░░░░▄████████░░░░░░░░░░░███▄░░
// ░░░░░██████████▄░░░░░░░░░░░███▌░ ▒█▀▀█ █▀▀█ █▀▄▀█ █▀▄▀█ ▒█▀▀▄ ▒█▀▀█
// ░░░░░▀█████▀░▀███▄░░░░░░░░░▐███░ ▒█░░░ █░░█ █░▀░█ █░▀░█ ▒█░▒█ ▒█▀▀▄
// ░░░░░░░▀█▀░░░░░▀███▄░░░░░░░▐███░ ▒█▄▄█ ▀▀▀▀ ▀░░░▀ ▀░░░▀ ▒█▄▄▀ ▒█▄▄█
// ░░░░░░░░░░░░░░░░░▀███▄░░░░░███▌░
// ░░░░▄██▄░░░░░░░░░░░▀███▄░░▐███░░
// ░░▄██████▄░░░░░░░░░░░▀███▄███░░░
// ░█████▀▀████▄▄░░░░░░░░▄█████░░░░
// ░████▀░░░▀▀█████▄▄▄▄█████████▄░░
// ░░▀▀░░░░░░░░░▀▀██████▀▀░░░▀▀██░░
#[get("/item/{item_id}")]
pub async fn item_page(r: HttpRequest) -> impl Responder {
let id = r.match_info().query("item_id");
println!("{}", id);
let itemdb: &actix_web::web::Data<item::ItemDB> = r.app_data().unwrap();
let item = itemdb.get_item(id).unwrap();
let content = html!(
p { "Item" };
p { (format!("Category: {}", item.category))}
).into_string();
web_base::build_site(&r, "Item", &content)
}
#[get("/")]
pub(crate) async fn index(r: HttpRequest) -> impl Responder {
let itemdb: &actix_web::web::Data<item::ItemDB> = r.app_data().unwrap();
let content = html!(
p class="text-xl font-bold" { "Hello World" };
@for item in itemdb.items() {
a href=(format!("/item/{}", item)) { (item) };
}
)
.into_string();
web_base::func::build_site(&r, "Index", &content)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init();
let itemdb = item::ItemDB::new("./itemdb", "mongodb://user:pass@mongodb:27017").await;
let itemdb = actix_web::web::Data::new(itemdb);
web_base::map!(
web_base::Site::new()
.head_content("<script src=\"https://cdn.tailwindcss.com\"></script>".to_string()),
|app: actix_web::App<_>| { app.app_data(itemdb.clone()).service(index).service(item_page) }
)
.bind(("0.0.0.0".to_string(), 8080))?
.run()
.await
}