added zasa :3

This commit is contained in:
MicroPanda123 2024-11-18 00:09:54 +01:00
parent 604ef98a32
commit 4a78061dba
No known key found for this signature in database
3 changed files with 50 additions and 20 deletions

9
server/Cargo.lock generated
View file

@ -804,8 +804,7 @@ dependencies = [
"ntex",
"ntex-files",
"rand",
"serde",
"serde_json",
"zasa",
]
[[package]]
@ -1893,6 +1892,12 @@ version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"
[[package]]
name = "zasa"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bd6fe2c1435fa0bd5ab924aba1f8f18f70e87735c142ede178a4fb14f9e85ed"
[[package]]
name = "zerocopy"
version = "0.7.35"

View file

@ -7,6 +7,5 @@ edition = "2021"
env_logger = "0.11.3"
ntex = { version = "2.0.0", features = [ "tokio", "rustls" ] }
ntex-files = "2.0.0"
serde = "1.0.210"
serde_json = "1.0.128"
rand = "0.8.5"
zasa = "0.3.0"

View file

@ -1,22 +1,48 @@
use std::fs::{self};
use ntex::{http::{header, Response}, web::{self, middleware}};
use ntex_files as nfs;
use ::rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
use ntex::{
http::{header, Response},
web::{self, middleware},
};
use ntex_files as nfs;
use std::collections::HashMap;
use zasa::{
parser::Parser,
value::{normalize, Normalize, Value, ValueError},
};
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone)]
struct WebringMember {
name: String,
site: String,
}
impl Normalize for WebringMember {
fn normalize(value: Value) -> Result<Self, ValueError> {
match value {
Value::Object(a) => {
let member = WebringMember {
name: a["name"].to_string(),
site: a["site"].to_string(),
};
Ok(member)
}
_ => Err(ValueError::ValueNotArray),
}
}
}
#[web::get("/next/{name}")]
async fn next(
members: web::types::State<Vec<WebringMember>>,
name: web::types::Path<String>,
) -> impl web::Responder {
if let Some((i, _)) = members.iter().enumerate().find(|(_, member)| member.name == *name) {
if let Some((i, _)) = members
.iter()
.enumerate()
.find(|(_, member)| member.name == *name)
{
let next_index = (i + 1) % members.len();
let next_site = &members[next_index].site;
@ -37,7 +63,11 @@ async fn prev(
members: web::types::State<Vec<WebringMember>>,
name: web::types::Path<String>,
) -> impl web::Responder {
if let Some((i, _)) = members.iter().enumerate().find(|(_, member)| member.name == *name) {
if let Some((i, _)) = members
.iter()
.enumerate()
.find(|(_, member)| member.name == *name)
{
let prev_index = if i == 0 { members.len() - 1 } else { i - 1 };
let prev_site = &members[prev_index].site;
@ -54,9 +84,7 @@ async fn prev(
}
#[web::get("/rand")]
async fn rand(
members: web::types::State<Vec<WebringMember>>,
) -> impl web::Responder {
async fn rand(members: web::types::State<Vec<WebringMember>>) -> impl web::Responder {
let rand_index = thread_rng().gen_range(0..members.len());
let rand_site = &members[rand_index].site;
@ -70,8 +98,7 @@ async fn rand(
async fn main() -> std::io::Result<()> {
env_logger::init();
let nix_webring_dir = std::env::var("NIX_WEBRING_DIR")
.expect("NIX_WEBRING_DIR not found");
let nix_webring_dir = std::env::var("NIX_WEBRING_DIR").expect("NIX_WEBRING_DIR not found");
let nix_webring_port = std::env::var("NIX_WEBRING_PORT")
.expect("NIX_WEBRING_PORT not found")
@ -80,10 +107,9 @@ async fn main() -> std::io::Result<()> {
web::server(move || {
let path = format!("{nix_webring_dir}/webring.json");
let json = fs::read_to_string(&path)
.expect(&format!("couldn't open {path}"));
let json = fs::read_to_string(&path).expect(&format!("couldn't open {path}"));
let members: Vec<WebringMember> = serde_json::from_str(&json)
let members: Vec<WebringMember> = normalize(Parser::new(json.chars()).parse().unwrap())
.expect(&format!("failed deserializing webring members: {json}"));
web::App::new()
@ -97,8 +123,8 @@ async fn main() -> std::io::Result<()> {
.service(
nfs::Files::new("/", nix_webring_dir.clone())
.index_file("index.html")
.redirect_to_slash_directory()
)
.redirect_to_slash_directory(),
),
)
})
.bind(("127.0.0.1", nix_webring_port))?