added zasa :3 #1

Open
MicroPanda123 wants to merge 2 commits from MicroPanda123/nixwebr.ing:zasa into main
3 changed files with 47 additions and 20 deletions

25
server/Cargo.lock generated
View file

@ -804,8 +804,7 @@ dependencies = [
"ntex",
"ntex-files",
"rand",
"serde",
"serde_json",
"zasa",
]
[[package]]
@ -1893,6 +1892,28 @@ version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"
[[package]]
name = "zasa"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e406a4941ce0c5351a09bb1d30e93744f430f80ffebcb1687f1550cff4553de4"
dependencies = [
"zasa_core",
"zasa_derive",
]
[[package]]
name = "zasa_core"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "828c3c824073e930389d33e932d376504bc88acf89be3c05bfa9bb406a09e52f"
[[package]]
name = "zasa_derive"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f3f2e32a4c0a55c14568fee40a9ca566193d0c78d62282cb451e38f9defc5ec"
[[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 = { version = "0.4.0", features = [ "derive" ] }

View file

@ -1,11 +1,14 @@
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 zasa::{parser::Parser, value::normalize, Normalize};
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Normalize)]
struct WebringMember {
name: String,
site: String,
@ -16,7 +19,11 @@ 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 +44,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 +65,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 +79,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 +88,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 +104,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))?