Compare commits

...

5 commits

16 changed files with 5007 additions and 296 deletions

2
.cargo/config.toml Normal file
View file

@ -0,0 +1,2 @@
[unstable]
bindeps = true

6
.gitignore vendored
View file

@ -1,7 +1,7 @@
/target
.env
config.toml
/config.toml
.direnv
target/
target-bin/
/target
/target-bin
secrets/

1320
Cargo.lock generated

File diff suppressed because it is too large Load diff

3587
Cargo.nix

File diff suppressed because it is too large Load diff

View file

@ -1,8 +1,10 @@
[workspace]
members = [
"chir-rs-castore", "chir-rs-client",
"chir-rs-castore",
"chir-rs-client",
"chir-rs-config",
"chir-rs-db",
"chir-rs-fe",
"chir-rs-gemini",
"chir-rs-http",
"chir-rs-http-api",
@ -35,7 +37,11 @@ sentry = { version = "0.34.0", default-features = false, features = [
] }
sentry-eyre = "0.2.0"
sentry-tracing = { version = "0.34.0", features = ["backtrace"] }
tokio = { version = "1.41.1", features = ["macros", "rt-multi-thread", "signal"] }
tokio = { version = "1.41.1", features = [
"macros",
"rt-multi-thread",
"signal",
] }
tracing = "0.1.41"
tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] }
@ -94,9 +100,3 @@ codegen-units = 1
lto = true
debug = "full"
strip = "none"
[profile.dev.package."*"]
opt-level = 2
[profile.dev]
opt-level = 1

View file

@ -15,6 +15,7 @@ eyre = "0.6.12"
stretto = { version = "0.8.4", features = ["async"] }
tokio = { version = "1.41.1", features = ["fs"] }
tracing = "0.1.41"
[lints.rust]
deprecated-safe = "forbid"
elided_lifetimes_in_paths = "warn"

View file

@ -9,7 +9,7 @@ use eyre::{eyre, Context as _, OptionExt as _, Result};
use mime_guess::{Mime, MimeGuess};
use reqwest::Body;
use tokio::join;
use tracing::instrument;
use tracing::{info, instrument};
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
@ -92,6 +92,7 @@ async fn upload(url: String, source: impl AsRef<Path>, dest: String) -> Result<(
.body(Body::from(file))
.send()
.await?;
info!("Finished uploading {dest}");
if !res.status().is_success() {
let response = res.bytes().await?;
let response: APIError =
@ -122,11 +123,10 @@ async fn upload_dir(url: String, source: impl AsRef<Path>, dest: String) -> Resu
continue;
}
if file_name_str == "index.html" {
if dest.is_empty() {
upload(url.clone(), ent.path(), "".to_string()).await?;
} else {
if !dest.is_empty() {
upload(url.clone(), ent.path(), format!("{dest}/")).await?;
}
upload(url.clone(), ent.path(), dest.clone()).await?;
}
upload(url.clone(), ent.path(), tgt).await?;
}

View file

@ -19,7 +19,7 @@ use tracing::{error, info, instrument};
#[instrument]
pub async fn expire(db: &Database) -> Result<()> {
let id = id_generator::generate();
let oldest_acceptable_id = id - ((24 * 60 * 60) << 48);
let oldest_acceptable_id = id - ((24 * 60 * 60 * 1_000_000_000) << 48);
let oldest_acceptable_id = oldest_acceptable_id.to_be_bytes();
#[expect(clippy::panic, reason = "sqlx moment")]
query!(

74
chir-rs-fe/Cargo.toml Normal file
View file

@ -0,0 +1,74 @@
[package]
name = "chir-rs-fe"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
bincode = "2.0.0-rc.3"
chir-rs-http-api = { version = "0.1.0", path = "../chir-rs-http-api" }
console_error_panic_hook = "0.1.7"
eyre = "0.6.12"
gloo-net = "0.6.0"
gloo-storage = "0.3.0"
tracing = "0.1.41"
tracing-subscriber = "0.3.19"
unicode-normalization = "0.1.24"
wasm-bindgen = "=0.2.99"
wasm-tracing = "0.2.1"
web-sys = { version = "0.3.76", features = ["HtmlInputElement"] }
yew = { version = "0.21.0", features = ["csr"] }
yew-router = "0.18.0"
[build-dependencies]
wasm-bindgen-cli = { version = "=0.2.99", artifact = "bin" }
[lints.rust]
deprecated-safe = "forbid"
elided_lifetimes_in_paths = "warn"
explicit_outlives_requirements = "warn"
impl-trait-overcaptures = "warn"
keyword-idents-2024 = "forbid"
let-underscore-drop = "warn"
macro-use-extern-crate = "deny"
meta-variable-misuse = "deny"
missing-abi = "forbid"
missing-copy-implementations = "warn"
missing-debug-implementations = "deny"
missing-docs = "warn"
missing-unsafe-on-extern = "deny"
non-local-definitions = "warn"
redundant-lifetimes = "warn"
single-use-lifetimes = "warn"
trivial-casts = "warn"
trivial-numeric-casts = "warn"
unit-bindings = "deny"
unnameable-types = "warn"
unreachable-pub = "warn"
unsafe-code = "forbid"
unused-crate-dependencies = "warn"
unused-extern-crates = "warn"
unused-import-braces = "warn"
unused-lifetimes = "warn"
unused-macro-rules = "warn"
unused-qualifications = "warn"
variant-size-differences = "warn"
[lints.clippy]
nursery = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }
module-name-repetitions = "allow"
alloc-instead-of-core = "warn"
allow-attributes-without-reason = "deny"
assertions-on-result-states = "forbid"
clone-on-ref-ptr = "warn"
empty-drop = "warn"
expect-used = "deny"
inline-asm-x86-att-syntax = "forbid"
missing-docs-in-private-items = "warn"
panic = "deny"
panic-in-result-fn = "forbid"
rc-buffer = "warn"
rc-mutex = "deny"

17
chir-rs-fe/index.html Normal file
View file

@ -0,0 +1,17 @@
<!doctype html>
<html lang="en">
<head>
<script type="module">
import init from './chir_rs_fe.js';
async function run() {
await init();
}
run();
</script>
<link rel="stylesheet" href="/css/style.css">
</head>
<body></body>
</html>

View file

@ -0,0 +1,19 @@
//! Homepage
use gloo_storage::{SessionStorage, Storage};
use yew::prelude::*;
use yew_router::prelude::*;
use crate::Route;
/// Render the home page of the admin page
pub fn home_page() -> Html {
SessionStorage::get::<String>("api").map_or_else(
|_| {
html! {
<Redirect<Route> to={Route::Login} />
}
},
|api_key| html! {<p>{api_key}</p>},
)
}

76
chir-rs-fe/src/lib.rs Normal file
View file

@ -0,0 +1,76 @@
//! Management frontend for chir.rs
use std::future::Future;
use tracing::{error, instrument};
use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _};
use wasm_bindgen::prelude::*;
use wasm_tracing::{WASMLayer, WASMLayerConfigBuilder};
use yew::{platform::spawn_local, prelude::*};
use yew_router::prelude::*;
pub mod home;
pub mod login;
#[instrument(skip(fut))]
pub fn spawn<F>(fut: F)
where
F: Future<Output = eyre::Result<()>> + 'static,
{
spawn_local(async move {
if let Err(e) = fut.await {
error!("Failed to run async closure: {e:?}");
}
});
}
/// Routes defined by this SPA
#[derive(Clone, Routable, PartialEq)]
enum Route {
/// Home page
#[at("/")]
Home,
/// Login page
#[at("/login")]
Login,
}
/// Render the main site content
#[allow(clippy::needless_pass_by_value, reason = "API reasons")]
fn switch(routes: Route) -> Html {
match routes {
Route::Home => home::home_page(),
Route::Login => html! {
<login::Login />
},
}
}
#[function_component(App)]
fn app() -> Html {
html! {
<div class="container">
<header>
<hgroup>
<h1> {"Lottes Attic"} </h1>
<p> {"Super secret raccministrator portal :3c "} </p>
</hgroup>
</header>
<main id="main" role="main">
<HashRouter>
<Switch<Route> render={switch} />
</HashRouter>
</main>
</div>
}
}
#[wasm_bindgen(start)]
fn main() {
console_error_panic_hook::set_once();
tracing_subscriber::registry()
.with(WASMLayer::new(WASMLayerConfigBuilder::new().build()))
.init();
yew::Renderer::<App>::new().render();
}

100
chir-rs-fe/src/login/mod.rs Normal file
View file

@ -0,0 +1,100 @@
//! Login screen
use std::collections::HashSet;
use chir_rs_http_api::{
auth::{LoginRequest, PasetoToken, Scope},
errors::APIError,
};
use eyre::Context;
use gloo_net::http::Request;
use gloo_storage::{SessionStorage, Storage as _};
use tracing::info;
use unicode_normalization::UnicodeNormalization;
use wasm_bindgen::JsCast as _;
use web_sys::HtmlInputElement;
use yew::prelude::*;
use yew_router::hooks::use_navigator;
use crate::{spawn, Route};
/// Login view
#[function_component]
pub fn Login() -> Html {
let navigator = use_navigator().unwrap();
let username_handle = use_state(String::default);
let username = username_handle.clone();
let password_handle = use_state(String::default);
let password = password_handle.clone();
let on_username_input = Callback::from(move |e: InputEvent| {
let target = e.target();
let input = target.and_then(|t| t.dyn_into::<HtmlInputElement>().ok());
if let Some(input) = input {
username.set(input.value());
}
});
let on_password_input = Callback::from(move |e: InputEvent| {
let target = e.target();
let input = target.and_then(|t| t.dyn_into::<HtmlInputElement>().ok());
if let Some(input) = input {
password.set(input.value());
}
});
let onsubmit = Callback::from(move |e: SubmitEvent| {
let navigator = navigator.clone();
e.prevent_default();
let username = username_handle.trim();
let password = password_handle.trim().nfkc().collect::<String>();
let request = LoginRequest {
username: username.to_string(),
password,
scopes: HashSet::from([Scope::Full]),
};
spawn(async move {
let req_body = bincode::encode_to_vec(request, bincode::config::standard())
.context("Encoding login request")?;
let resp = Request::post("/.api/auth/login")
.header("Content-Type", "application/x+bincode")
.header("Accept", "application/x+bincode")
.body(req_body)
.context("Setting body for login request")?
.send()
.await
.context("Sending request for login request")?;
let status = resp.status();
let resp_body = resp
.binary()
.await
.context("Loading server response for login request")?;
if status >= 400 {
let error: APIError =
bincode::decode_from_slice(&resp_body, bincode::config::standard())
.context("Decoding error response")?
.0;
Err(error).context("While logging in")?;
} else {
let token: PasetoToken =
bincode::decode_from_slice(&resp_body, bincode::config::standard())
.context("Decoding success response")?
.0;
SessionStorage::set("api", token.to_paseto());
}
navigator.replace(&Route::Home);
Ok(())
});
info!("{e:?}");
});
html! {
<form accept-charset="utf-8" onsubmit={onsubmit}>
<label for="username">{"Username:"}</label>
<input id="username" type="text" placeholder="https://lotte.chir.rs/" oninput={on_username_input} />
<label for="password">{"Password:"}</label>
<input id="password" type="password" placeholder="●●●●●●●●●●●●●●●●" oninput={on_password_input} />
<input type="submit" value="Submit"/>
</form>
}
}

View file

@ -14,11 +14,11 @@
]
},
"locked": {
"lastModified": 1726552619,
"narHash": "sha256-ytTBILVMnRZYvjiLYz+J6IFf/TOXdGuP6RDesMx9qgA=",
"lastModified": 1733731208,
"narHash": "sha256-s9O/KIBhKOS49VmYJIBEiCrLK5hocJLKuvAkwAoELLQ=",
"owner": "DarkKirb",
"repo": "cargo2nix",
"rev": "baa12124e2de09e1cbbdac320f14809fa55af1a2",
"rev": "37f356e1ba56e591550647c86039ffb0ca0c2201",
"type": "github"
},
"original": {

View file

@ -59,11 +59,38 @@
pkgs = import nixpkgs {
inherit system overlays;
};
pkgs-wasm32 = import nixpkgs {
inherit system overlays;
crossSystem = {
system = "wasm32-wasi";
useLLVM = true;
};
};
rustPkgs = pkgs.rustBuilder.makePackageSet {
packageFun = import ./Cargo.nix;
rustChannel = "stable";
rustChannel = "nightly";
rustVersion = "latest";
packageOverrides = pkgs: pkgs.rustBuilder.overrides.all;
extraConfigToml = ''
[unstable]
bindeps = true
'';
};
rustPkgs-wasm32 = pkgs-wasm32.rustBuilder.makePackageSet {
packageFun = import ./Cargo.nix;
rustChannel = "nightly";
rustVersion = "latest";
packageOverrides = pkgs: pkgs.rustBuilder.overrides.all;
target = "wasm32-unknown-unknown";
hostPlatform = pkgs-wasm32.stdenv.hostPlatform // {
parsed = pkgs-wasm32.stdenv.hostPlatform.parsed // {
kernel.name = "unknown";
};
};
extraConfigToml = ''
[unstable]
bindeps = true
'';
};
in
rec {
@ -71,6 +98,13 @@
with pkgs;
mkShell {
buildInputs = [
(rust-bin.selectLatestNightlyWith (
toolchain:
toolchain.default.override {
extensions = [ "rust-src" ];
targets = [ "wasm32-unknown-unknown" ];
}
))
cargo2nix.packages.${system}.cargo2nix
rustfilt
gdb
@ -79,9 +113,39 @@
sqlite
treefmt
nixfmt-rfc-style
wabt
trunk
(rustPkgs."registry+https://github.com/rust-lang/crates.io-index".wasm-bindgen-cli."0.2.99" { })
binaryen
];
};
packages = pkgs.lib.mapAttrs (_: v: (v { }).overrideAttrs { dontStrip = true; }) rustPkgs.workspace;
packages =
(pkgs.lib.mapAttrs (_: v: (v { }).overrideAttrs { dontStrip = true; }) rustPkgs.workspace)
// {
chir-rs-fe =
let
chir-rs-fe = rustPkgs-wasm32.workspace.chir-rs-fe { };
wasm-bindgen-cli =
rustPkgs."registry+https://github.com/rust-lang/crates.io-index".wasm-bindgen-cli."0.2.99"
{ };
in
pkgs.stdenvNoCC.mkDerivation {
inherit (chir-rs-fe) name version;
src = chir-rs-fe.out;
dontUnpack = true;
dontBuild = true;
nativeBuildInputs = [
wasm-bindgen-cli
pkgs.binaryen
];
installPhase = ''
mkdir $out
wasm-opt $src/lib/chir_rs_fe.wasm -o chir_rs_fe.wasm
wasm-bindgen chir_rs_fe.wasm --out-dir $out --target web
cp ${./chir-rs-fe/index.html} $out/index.html
'';
};
};
nixosModules.default = import ./nixos {
inherit inputs system;
};

1
result Symbolic link
View file

@ -0,0 +1 @@
/nix/store/navjjbwfm21p9z8c9f62zpislxhbn1xb-chir-rs-fe