80 lines
2.3 KiB
Rust
80 lines
2.3 KiB
Rust
mod db;
|
|
mod sbot;
|
|
mod utils;
|
|
|
|
use std::path::Path;
|
|
|
|
use log::{debug, info, warn};
|
|
use rocket::{form::Form, get, launch, post, response::Redirect, routes, uri, FromForm, State};
|
|
use rocket_dyn_templates::{tera::Context, Template};
|
|
|
|
use crate::db::Database;
|
|
|
|
#[derive(FromForm)]
|
|
struct Peer {
|
|
public_key: String,
|
|
}
|
|
|
|
#[get("/")]
|
|
fn index(db: &State<Database>) -> Template {
|
|
let mut context = Context::new();
|
|
let feeds = db.get_feeds();
|
|
context.insert("feeds", &feeds);
|
|
|
|
Template::render("index", &context.into_json())
|
|
}
|
|
|
|
#[post("/subscribe", data = "<peer>")]
|
|
fn subscribe_form(db: &State<Database>, peer: Form<Peer>) -> Redirect {
|
|
// validate the public key
|
|
if let Ok(_) = utils::validate_public_key(&peer.public_key) {
|
|
debug!("public key {} is valid", &peer.public_key);
|
|
match db.add_feed(&peer.public_key) {
|
|
Ok(_) => {
|
|
debug!("added {} to feed tree in database", &peer.public_key);
|
|
// check if we already follow the peer
|
|
// - if not, follow the peer and create a tree for the peer
|
|
}
|
|
Err(_e) => warn!(
|
|
"failed to add {} to feed tree in database",
|
|
&peer.public_key
|
|
),
|
|
}
|
|
} else {
|
|
warn!("{} is invalid", &peer.public_key);
|
|
}
|
|
|
|
Redirect::to(uri!(index))
|
|
}
|
|
|
|
#[post("/unsubscribe", data = "<peer>")]
|
|
fn unsubscribe_form(db: &State<Database>, peer: Form<Peer>) -> Redirect {
|
|
// validate the public key
|
|
match utils::validate_public_key(&peer.public_key) {
|
|
Ok(_) => {
|
|
debug!("public key {} is valid", &peer.public_key);
|
|
match db.remove_feed(&peer.public_key) {
|
|
Ok(_) => debug!("removed {} from feed tree in database", &peer.public_key),
|
|
Err(_e) => warn!(
|
|
"failed to remove {} from feed tree in database",
|
|
&peer.public_key
|
|
),
|
|
}
|
|
}
|
|
Err(e) => warn!("{} is invalid: {}", &peer.public_key, e),
|
|
}
|
|
|
|
Redirect::to(uri!(index))
|
|
}
|
|
|
|
#[launch]
|
|
fn rocket() -> _ {
|
|
env_logger::init();
|
|
|
|
info!("launching the web server");
|
|
rocket::build()
|
|
.manage(Database::init(Path::new("lykin")))
|
|
.mount("/", routes![index, subscribe_form, unsubscribe_form])
|
|
.attach(Template::fairing())
|
|
}
|