move webring data to webring.nix
All checks were successful
Deploy website / deploy (push) Successful in 12s

This commit is contained in:
jacekpoz 2024-09-13 21:38:59 +02:00
parent 71a6690aab
commit 001e274186
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
10 changed files with 91 additions and 47 deletions

View file

@ -13,14 +13,16 @@
outputs = { nixpkgs, nte, self, systems, ... }: let
forEachSystem = nixpkgs.lib.genAttrs (import systems);
pkgsForEach = nixpkgs.legacyPackages;
webringMembers = import ./webring.nix;
in {
packages = forEachSystem (
system: let
pkgs = pkgsForEach.${system};
in {
site = import ./site/default.nix {
inherit pkgs;
inherit (pkgs) jetbrains-mono lib;
inherit pkgs webringMembers;
inherit (pkgs) jetbrains-mono lib writeText;
inherit (nte.functions.${system}) mkNteDerivation;
};
server = import ./server/default.nix {

15
server/Cargo.lock generated
View file

@ -804,6 +804,8 @@ dependencies = [
"ntex",
"ntex-files",
"rand",
"serde",
"serde_json",
]
[[package]]
@ -1383,18 +1385,18 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
[[package]]
name = "serde"
version = "1.0.204"
version = "1.0.210"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12"
checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.204"
version = "1.0.210"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222"
checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f"
dependencies = [
"proc-macro2",
"quote",
@ -1403,11 +1405,12 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.120"
version = "1.0.128"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5"
checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]

View file

@ -7,4 +7,6 @@ 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"

View file

@ -5,5 +5,5 @@ rustPlatform.buildRustPackage {
src = ./.;
cargoHash = "sha256-O+rhWIjIBQdemTsFc1TV/lpKZpGE/IL7uPs3TP8KVu0=";
cargoHash = "sha256-v96+UzVjj44DOs4nFUR6+qyqI+0JwyQK+/5z/bi1J1o=";
}

View file

@ -1,15 +1,24 @@
mod members;
use std::fs::{self};
use members::WEBRING_MEMBERS;
use ntex::{http::{header, Response}, web::{self, middleware}};
use ntex_files as nfs;
use ::rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize)]
struct WebringMember {
name: String,
domain: String,
}
#[web::get("/next/{name}")]
async fn next(name: web::types::Path<String>) -> impl web::Responder {
if let Some((i, _)) = WEBRING_MEMBERS.iter().enumerate().find(|(_, member)| member.name == *name) {
let next_index = (i + 1) % WEBRING_MEMBERS.len();
let next_domain = WEBRING_MEMBERS[next_index].domain;
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) {
let next_index = (i + 1) % members.len();
let next_domain = &members[next_index].domain;
let next_url = format!("https://{next_domain}/");
return Response::PermanentRedirect()
@ -23,10 +32,13 @@ async fn next(name: web::types::Path<String>) -> impl web::Responder {
}
#[web::get("/prev/{name}")]
async fn prev(name: web::types::Path<String>) -> impl web::Responder {
if let Some((i, _)) = WEBRING_MEMBERS.iter().enumerate().find(|(_, member)| member.name == *name) {
let prev_index = if i == 0 { WEBRING_MEMBERS.len() - 1 } else { i - 1 };
let prev_domain = WEBRING_MEMBERS[prev_index].domain;
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) {
let prev_index = if i == 0 { members.len() - 1 } else { i - 1 };
let prev_domain = &members[prev_index].domain;
let prev_url = format!("https://{prev_domain}/");
return Response::PermanentRedirect()
@ -40,9 +52,11 @@ async fn prev(name: web::types::Path<String>) -> impl web::Responder {
}
#[web::get("/rand")]
async fn rand() -> impl web::Responder {
let rand_index = thread_rng().gen_range(0..WEBRING_MEMBERS.len());
let rand_domain = WEBRING_MEMBERS[rand_index].domain;
async fn rand(
members: web::types::State<Vec<WebringMember>>,
) -> impl web::Responder {
let rand_index = thread_rng().gen_range(0..members.len());
let rand_domain = &members[rand_index].domain;
let rand_url = format!("https://{rand_domain}/");
Response::PermanentRedirect()
@ -64,8 +78,16 @@ async fn main() -> std::io::Result<()> {
.expect("NIX_WEBRING_PORT has to be u16");
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 members: Vec<WebringMember> = serde_json::from_str(&json)
.expect(&format!("failed deserializing webring members: {json}"));
web::App::new()
.wrap(middleware::Logger::default())
.state(members)
.service(
web::scope("/")
.service(next)

View file

@ -1,16 +0,0 @@
// this is hilariously bad but I'm too lazy to do anything about it right now
pub struct WebringMember {
pub name: &'static str,
pub domain: &'static str,
}
pub const WEBRING_MEMBERS: &'static [WebringMember] = &[
WebringMember {
name: "poz",
domain: "jacekpoz.pl",
},
WebringMember {
name: "krizej",
domain: "krizej.codeberg.page",
},
];

View file

@ -2,6 +2,8 @@
jetbrains-mono,
lib,
mkNteDerivation,
webringMembers,
writeText,
...
}: let
inherit (lib.attrsets) listToAttrs;
@ -13,19 +15,19 @@
in /*html*/''
<h${toString n} id="${id}"><a href="#${id}">#</a> ${content}</h${toString n}>
'';
hs = listToAttrs (map (n: {
name = "h${toString n}";
value = text: h n text;
}) [ 1 2 3 4 5 6 ]);
in mkNteDerivation {
name = "nix-webring-site";
version = "0.1.0";
src = ./.;
extraArgs = {
inherit h;
}
// listToAttrs (map (n: {
name = "h${toString n}";
value = text: h n text;
}) [ 1 2 3 4 5 6 ]
);
extraArgs = hs // {
inherit h webringMembers;
};
entries = [
./index.nix
@ -35,5 +37,9 @@ in mkNteDerivation {
{ source = "./index.css"; destination = "/"; }
{ source = "./nix-webring.svg"; destination = "/"; }
{ source = "${jetbrains-mono}/share/fonts/truetype/JetBrainsMono-Regular.ttf"; destination = "/"; }
{
source = writeText "webring.json" (builtins.toJSON webringMembers);
destination = "/webring.json";
}
];
}

View file

@ -19,3 +19,11 @@ body {
margin: 0;
height: 100%;
}
a:link {
color: #7EB6E1;
}
a:visited {
color: #5075BF;
}

View file

@ -1,4 +1,9 @@
_: {
{
h2,
pkgs,
webringMembers,
...
}: {
template = "passthrough";
format = "html";
@ -15,6 +20,14 @@ _: {
</head>
<body>
<p>work in progress</p>
${h2 "webring members"}
<ul>
${pkgs.lib.concatStrings (map (member: /*html*/''
<li>
<a href="https://${member.domain}">${member.name}</a>
</li>
'') webringMembers)}
</ul>
</body>
</html>
'';

4
webring.nix Normal file
View file

@ -0,0 +1,4 @@
[
{ name = "poz"; domain = "jacekpoz.pl"; }
{ name = "krizej"; domain = "krizej.codeberg.page"; }
]