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 outputs = { nixpkgs, nte, self, systems, ... }: let
forEachSystem = nixpkgs.lib.genAttrs (import systems); forEachSystem = nixpkgs.lib.genAttrs (import systems);
pkgsForEach = nixpkgs.legacyPackages; pkgsForEach = nixpkgs.legacyPackages;
webringMembers = import ./webring.nix;
in { in {
packages = forEachSystem ( packages = forEachSystem (
system: let system: let
pkgs = pkgsForEach.${system}; pkgs = pkgsForEach.${system};
in { in {
site = import ./site/default.nix { site = import ./site/default.nix {
inherit pkgs; inherit pkgs webringMembers;
inherit (pkgs) jetbrains-mono lib; inherit (pkgs) jetbrains-mono lib writeText;
inherit (nte.functions.${system}) mkNteDerivation; inherit (nte.functions.${system}) mkNteDerivation;
}; };
server = import ./server/default.nix { server = import ./server/default.nix {

15
server/Cargo.lock generated
View file

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

View file

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

View file

@ -5,5 +5,5 @@ rustPlatform.buildRustPackage {
src = ./.; 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::{http::{header, Response}, web::{self, middleware}};
use ntex_files as nfs; use ntex_files as nfs;
use ::rand::{thread_rng, Rng}; use ::rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize)]
struct WebringMember {
name: String,
domain: String,
}
#[web::get("/next/{name}")] #[web::get("/next/{name}")]
async fn next(name: web::types::Path<String>) -> impl web::Responder { async fn next(
if let Some((i, _)) = WEBRING_MEMBERS.iter().enumerate().find(|(_, member)| member.name == *name) { members: web::types::State<Vec<WebringMember>>,
let next_index = (i + 1) % WEBRING_MEMBERS.len(); name: web::types::Path<String>,
let next_domain = WEBRING_MEMBERS[next_index].domain; ) -> 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}/"); let next_url = format!("https://{next_domain}/");
return Response::PermanentRedirect() return Response::PermanentRedirect()
@ -23,10 +32,13 @@ async fn next(name: web::types::Path<String>) -> impl web::Responder {
} }
#[web::get("/prev/{name}")] #[web::get("/prev/{name}")]
async fn prev(name: web::types::Path<String>) -> impl web::Responder { async fn prev(
if let Some((i, _)) = WEBRING_MEMBERS.iter().enumerate().find(|(_, member)| member.name == *name) { members: web::types::State<Vec<WebringMember>>,
let prev_index = if i == 0 { WEBRING_MEMBERS.len() - 1 } else { i - 1 }; name: web::types::Path<String>,
let prev_domain = WEBRING_MEMBERS[prev_index].domain; ) -> 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}/"); let prev_url = format!("https://{prev_domain}/");
return Response::PermanentRedirect() return Response::PermanentRedirect()
@ -40,9 +52,11 @@ async fn prev(name: web::types::Path<String>) -> impl web::Responder {
} }
#[web::get("/rand")] #[web::get("/rand")]
async fn rand() -> impl web::Responder { async fn rand(
let rand_index = thread_rng().gen_range(0..WEBRING_MEMBERS.len()); members: web::types::State<Vec<WebringMember>>,
let rand_domain = WEBRING_MEMBERS[rand_index].domain; ) -> 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}/"); let rand_url = format!("https://{rand_domain}/");
Response::PermanentRedirect() Response::PermanentRedirect()
@ -64,8 +78,16 @@ async fn main() -> std::io::Result<()> {
.expect("NIX_WEBRING_PORT has to be u16"); .expect("NIX_WEBRING_PORT has to be u16");
web::server(move || { 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() web::App::new()
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.state(members)
.service( .service(
web::scope("/") web::scope("/")
.service(next) .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, jetbrains-mono,
lib, lib,
mkNteDerivation, mkNteDerivation,
webringMembers,
writeText,
... ...
}: let }: let
inherit (lib.attrsets) listToAttrs; inherit (lib.attrsets) listToAttrs;
@ -13,19 +15,19 @@
in /*html*/'' in /*html*/''
<h${toString n} id="${id}"><a href="#${id}">#</a> ${content}</h${toString n}> <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 { in mkNteDerivation {
name = "nix-webring-site"; name = "nix-webring-site";
version = "0.1.0"; version = "0.1.0";
src = ./.; src = ./.;
extraArgs = { extraArgs = hs // {
inherit h; inherit h webringMembers;
} };
// listToAttrs (map (n: {
name = "h${toString n}";
value = text: h n text;
}) [ 1 2 3 4 5 6 ]
);
entries = [ entries = [
./index.nix ./index.nix
@ -35,5 +37,9 @@ in mkNteDerivation {
{ source = "./index.css"; destination = "/"; } { source = "./index.css"; destination = "/"; }
{ source = "./nix-webring.svg"; destination = "/"; } { source = "./nix-webring.svg"; destination = "/"; }
{ source = "${jetbrains-mono}/share/fonts/truetype/JetBrainsMono-Regular.ttf"; 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; margin: 0;
height: 100%; height: 100%;
} }
a:link {
color: #7EB6E1;
}
a:visited {
color: #5075BF;
}

View file

@ -1,4 +1,9 @@
_: { {
h2,
pkgs,
webringMembers,
...
}: {
template = "passthrough"; template = "passthrough";
format = "html"; format = "html";
@ -15,6 +20,14 @@ _: {
</head> </head>
<body> <body>
<p>work in progress</p> <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> </body>
</html> </html>
''; '';

4
webring.nix Normal file
View file

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