Improve OG descriptions and fix Nix builds

This commit is contained in:
Bad Manners 2026-01-17 15:53:33 -03:00
parent 25bc697309
commit 15486c36ca
6 changed files with 113 additions and 48 deletions

2
Cargo.lock generated
View file

@ -3026,7 +3026,7 @@ dependencies = [
[[package]]
name = "samey"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"askama",
"async-trait",

View file

@ -1,6 +1,6 @@
[package]
name = "samey"
version = "0.1.0"
version = "0.1.1"
edition = "2024"
rust-version = "1.85"
license = "MIT"

View file

@ -3,16 +3,24 @@ let
nixpkgs
rust-overlay
;
pkgs = import nixpkgs {
currentPkgs = import nixpkgs {
overlays = [ (import rust-overlay) ];
};
inherit (pkgs) lib stdenv;
inherit (currentPkgs) lib stdenv;
# Options for your package
pname = "samey";
version = "0.1.0";
docker-image = "badmanners/samey";
cargo-deps-hash = "sha256-oiz2a6Vip199saU/s/sBn/3Cl0eJaSltN3n1uPETHGk=";
crate-info = fromTOML (builtins.readFile ./Cargo.toml);
pname = crate-info.package.name;
version = crate-info.package.version;
docker-image = "badmanners/${pname}";
cargo-deps-hash = "sha256-PD/ZR/sdmqA18xcOi9AnwQtDYVyELPS6GBF/pzcJzkE=";
cargo-src = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./Cargo.toml
./Cargo.lock
];
};
src = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
@ -26,15 +34,34 @@ let
];
};
rust-bin = pkgs.rust-bin.stable.latest.default.override {
archs = {
amd64 = {
system = "x86_64-linux";
pkgs = import nixpkgs {
localSystem = "x86_64-linux";
};
tag = "latest-amd64";
targetTriple = "x86_64-unknown-linux-musl";
};
arm64 = {
system = "aarch64-linux";
pkgs = import nixpkgs {
localSystem = "aarch64-linux";
};
tag = "latest-arm64";
targetTriple = "aarch64-unknown-linux-musl";
};
};
rust-bin = currentPkgs.rust-bin.stable.latest.default.override {
targets = [
"x86_64-unknown-linux-gnu"
"aarch64-unknown-linux-gnu"
"x86_64-unknown-linux-musl"
"aarch64-unknown-linux-musl"
];
};
mkRustPkg =
target:
targetTriple:
stdenv.mkDerivation {
inherit
pname
@ -42,70 +69,63 @@ let
src
;
cargoDeps = pkgs.rustPlatform.fetchCargoVendor {
inherit src;
cargoDeps = currentPkgs.rustPlatform.fetchCargoVendor {
src = cargo-src;
hash = cargo-deps-hash;
};
nativeBuildInputs = [
pkgs.rustPlatform.cargoSetupHook
pkgs.zig
currentPkgs.rustPlatform.cargoSetupHook
currentPkgs.zig
rust-bin
];
buildPhase = ''
export HOME=$(mktemp -d)
${pkgs.cargo-zigbuild}/bin/cargo-zigbuild zigbuild --release --target ${target}
${currentPkgs.cargo-zigbuild}/bin/cargo-zigbuild zigbuild --release --target ${targetTriple}
'';
installPhase = ''
mkdir -p $out/bin
cp ./target/${target}/release/${pname} $out/bin/
cp ./target/${targetTriple}/release/${pname} $out/bin/
'';
};
amd64 = {
system = "x86_64-linux";
pkgs = import nixpkgs {
localSystem = "x86_64-linux";
};
tag = "latest-amd64";
target = "x86_64-unknown-linux-gnu";
};
arm64 = {
system = "aarch64-linux";
pkgs = import nixpkgs {
localSystem = "aarch64-linux";
};
tag = "latest-arm64";
target = "aarch64-unknown-linux-gnu";
};
mkDocker =
targetAttrs:
{
system,
pkgs,
tag,
targetTriple,
}:
let
pkgs-cross =
if targetAttrs.system == builtins.currentSystem then
pkgs
if system == builtins.currentSystem then
currentPkgs
else
(import nixpkgs {
crossSystem = targetAttrs.system;
crossSystem = system;
});
rust-package = mkRustPkg targetAttrs.target;
rust-package = mkRustPkg targetTriple;
in
pkgs-cross.dockerTools.buildLayeredImage {
name = docker-image;
inherit (targetAttrs) tag;
inherit tag;
contents = [
targetAttrs.pkgs.ffmpeg-headless
pkgs.ffmpeg-headless
];
config.Entrypoint = [
"${rust-package}/bin/${pname}"
];
};
currentTargetTriple =
(lib.lists.findFirst (arch: arch.system == builtins.currentSystem) {
targetTriple = throw "Unknown current system ${builtins.currentSystem}";
} (lib.attrValues archs)).targetTriple;
in
{
docker-amd64 = mkDocker amd64;
docker-arm64 = mkDocker arm64;
samey = mkRustPkg currentTargetTriple;
docker-amd64 = mkDocker archs.amd64;
docker-arm64 = mkDocker archs.arm64;
}

View file

@ -84,7 +84,31 @@ async fn main() {
} else {
println!("Listening on http://{}:{}", address, port);
}
#[cfg(unix)]
{
let mut signal_terminate =
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.unwrap();
let mut signal_interrupt =
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt())
.unwrap();
tokio::select! {
server = axum::serve(listener, app) => {
server.unwrap();
},
_ = signal_terminate.recv() => {
println!("Received SIGTERM");
},
_ = signal_interrupt.recv() => {
println!("Received SIGINT");
},
};
}
#[cfg(not(unix))]
{
axum::serve(listener, app).await.unwrap();
}
}
}
}

View file

@ -1460,6 +1460,7 @@ struct ViewPostPageTemplate {
application_name: String,
age_confirmation: bool,
post: samey_post::Model,
description_plaintext: Option<String>,
pool_data: Vec<PostPoolData>,
tags: Vec<samey_tag::Model>,
tags_text: Option<String>,
@ -1563,11 +1564,31 @@ pub(crate) async fn view_post_page(
let pool_data = get_pool_data_for_post(&db, post_id, auth_session.user.as_ref()).await?;
let description_plaintext = post.description.as_ref().map(|description| {
use pulldown_cmark::{Event, Options, Parser, TagEnd, html::write_html_fmt};
let parser = Parser::new_ext(description, Options::empty())
.map(|event| match event {
Event::End(TagEnd::Paragraph) => Event::Text(" ".into()),
_ => event,
})
.filter(|event| match event {
Event::Text(_) => true,
_ => false,
});
let mut buf = String::new();
write_html_fmt(&mut buf, parser)
.ok()
.map(|_| buf)
.unwrap_or_else(|| description.clone())
});
Ok(Html(
ViewPostPageTemplate {
application_name,
age_confirmation,
post,
description_plaintext,
pool_data,
tags,
tags_text: query.tags,

View file

@ -6,7 +6,7 @@
{% include "fragments/common_headers.html" %}
{% if let Some(title) = post.title %}<meta property="og:title" content="{{ title }}"/>{% else %}<meta property="og:title" content="{{ tags_post }}" />{% endif %}
<meta property="og:url" content="/post/{{ post.id }}" />
{% if let Some(description) = post.description %}<meta property="og:description" content="{{ description | markdown | escape }}" />{% endif %}
{% if let Some(description) = description_plaintext %}<meta property="og:description" content="{{ description }}" />{% endif %}
{% match post.media_type.as_ref() %} {% when "image" %}
<meta property="og:image" content="/files/{{ post.media }}" />
<meta property="og:image:width" content="{{ post.width }}" />