initial impl

This commit is contained in:
dusk 2023-07-17 09:01:10 +03:00
parent 9c8dc8c749
commit 6d4665af74
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw
9 changed files with 1679 additions and 10 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
use flake

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/.direnv
/target /target
result* result*

1287
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,13 @@
[package] [package]
name = "quick-start-simple" name = "limbusart"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
axum = {git = "https://github.com/tokio-rs/axum.git", version = "0.6"}
tokio = {version = "1", features = ["rt-multi-thread", "macros"]}
http = "0.2"
fastrand = {version = "2", features = ["std"]}
reqwest = {version = "0.11", default-features = false, features = ["rustls-tls-native-roots"]}
dashmap = "5"
maud = "0.25"

3
arts.txt Normal file
View File

@ -0,0 +1,3 @@
https://twitter.com/FFJ_OFF/status/1679888209705340928
https://twitter.com/rauchoi_ovu/status/1680486681663922177
https://twitter.com/lk0_71604/status/1680425494741938179

159
flake.lock Normal file
View File

@ -0,0 +1,159 @@
{
"nodes": {
"crane": {
"inputs": {
"flake-compat": "flake-compat",
"flake-utils": "flake-utils",
"nixpkgs": [
"nixpkgs"
],
"rust-overlay": "rust-overlay"
},
"locked": {
"lastModified": 1688772518,
"narHash": "sha256-ol7gZxwvgLnxNSZwFTDJJ49xVY5teaSvF7lzlo3YQfM=",
"owner": "ipetkov",
"repo": "crane",
"rev": "8b08e96c9af8c6e3a2b69af5a7fa168750fcf88e",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1673956053,
"narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1687709756,
"narHash": "sha256-Y5wKlQSkgEK2weWdOu4J3riRd+kV/VCgHsqLNTTWQ/0=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "dbabf0ca0c0c4bce6ea5eaf65af5cb694d2082c7",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_2": {
"inputs": {
"systems": "systems_2"
},
"locked": {
"lastModified": 1689068808,
"narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1689449371,
"narHash": "sha256-sK3Oi8uEFrFPL83wKPV6w0+96NrmwqIpw9YFffMifVg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "29bcead8405cfe4c00085843eb372cc43837bb9d",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"crane": "crane",
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs"
}
},
"rust-overlay": {
"inputs": {
"flake-utils": [
"crane",
"flake-utils"
],
"nixpkgs": [
"crane",
"nixpkgs"
]
},
"locked": {
"lastModified": 1688351637,
"narHash": "sha256-CLTufJ29VxNOIZ8UTg0lepsn3X03AmopmaLTTeHDCL4=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "f9b92316727af9e6c7fee4a761242f7f46880329",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"systems_2": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

View File

@ -46,15 +46,16 @@
}; };
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
name = "limbusart-shell";
inputsFrom = builtins.attrValues self.checks.${system}; inputsFrom = builtins.attrValues self.checks.${system};
# Additional dev-shell environment variables can be set directly
# MY_CUSTOM_DEVELOPMENT_VAR = "something else";
# Extra inputs can be added here # Extra inputs can be added here
nativeBuildInputs = with pkgs; [ packages = with pkgs; [
cargo cargo
rustc rustc
rust-analyzer
rustfmt
]; ];
}; };
}); });

47
src/error.rs Normal file
View File

@ -0,0 +1,47 @@
use std::fmt::Display;
use axum::response::IntoResponse;
use http::StatusCode;
type BoxedError = Box<dyn std::error::Error>;
#[derive(Debug)]
pub(crate) struct AppError {
internal: BoxedError,
status: Option<StatusCode>,
}
impl AppError {
pub(crate) fn status(mut self, code: StatusCode) -> Self {
self.status = Some(code);
self
}
}
impl<E> From<E> for AppError
where
E: Into<BoxedError>,
{
fn from(err: E) -> Self {
Self {
internal: err.into(),
status: None,
}
}
}
impl IntoResponse for AppError {
fn into_response(self) -> axum::response::Response {
(
self.status.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
format!("Something went wrong: {}", self.internal),
)
.into_response()
}
}
impl Display for AppError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.internal.fmt(f)
}
}

View File

@ -1,3 +1,170 @@
fn main() { use axum::{extract::State, response::Html, routing::get, Router};
println!("Hello, world!"); use dashmap::DashMap;
use error::AppError;
use http::Uri;
use std::{ops::Deref, str::FromStr, sync::Arc};
mod error;
type AppResult<T> = Result<T, AppError>;
enum ArtKind {
Twitter,
}
impl FromStr for ArtKind {
type Err = AppError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"twitter.com" => Ok(Self::Twitter),
_ => Err("not support website".into()),
}
}
}
struct Art {
url: Uri,
kind: ArtKind,
}
impl FromStr for Art {
type Err = AppError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let url: Uri = s.parse()?;
let kind: ArtKind = url.authority().unwrap().host().parse()?;
Ok(Self { url, kind })
}
}
struct Data {
// actual arts
art: Vec<Art>,
}
impl Data {
fn parse(data: &str) -> AppResult<Self> {
let mut this = Self {
art: Default::default(),
};
for entry in data.lines() {
let art: Art = entry.parse()?;
this.art.push(art);
}
Ok(this)
}
fn pick_random_art(&self) -> &Art {
let no = fastrand::usize(0..self.art.len());
&self.art[no]
}
}
struct InternalAppState {
// cached direct links to images
direct_links: DashMap<Uri, String>,
data: Data,
http: reqwest::Client,
}
#[derive(Clone)]
struct AppState {
internal: Arc<InternalAppState>,
}
impl AppState {
fn new(data: Data) -> Self {
Self {
internal: Arc::new(InternalAppState {
data,
direct_links: Default::default(),
http: reqwest::ClientBuilder::new()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap(),
}),
}
}
}
impl Deref for AppState {
type Target = InternalAppState;
fn deref(&self) -> &Self::Target {
&self.internal
}
}
#[tokio::main]
async fn main() {
let arts = std::fs::read_to_string("arts.txt").unwrap();
let data = AppState::new(Data::parse(&arts).unwrap());
let app = Router::new().route("/", get(show_art)).with_state(data);
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
println!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}
async fn show_art(state: State<AppState>) -> AppResult<Html<String>> {
let art = state.data.pick_random_art();
if let Some(image_link) = state.direct_links.get(&art.url) {
Ok(render_page(art, &image_link))
} else {
let image_link = match art.kind {
ArtKind::Twitter => fetch_twitter_image_link(&state.http, &art.url).await?,
};
let page = render_page(art, &image_link);
state.direct_links.insert(art.url.clone(), image_link);
Ok(page)
}
}
fn render_page(art: &Art, image_link: &str) -> Html<String> {
let body_style =
"margin: 0px; background: #0e0e0e; height: 100vh; width: 100vw; display: flex;";
let img_style = "display: block; margin: auto; max-height: 100vh; max-width: 100vw;";
let about_style = "position: absolute; bottom: 0; font-size: 0.75em; color: #ffffff; background-color: #0e0e0eaa;";
let content = maud::html! {
(maud::DOCTYPE)
head {
meta charset="utf8";
meta property="og:image" content=(image_link);
title { "random limbussy art" }
}
body style=(body_style) {
img style=(img_style) src=(image_link);
a style=(format!("{about_style} left: 0;")) href=(art.url) target="_blank" {
"source: " (art.url)
}
a style=(format!("{about_style} right: 0;")) href="https://gaze.systems" target="_blank" {
"website made by dusk"
}
}
};
Html(content.into_string())
}
async fn fetch_twitter_image_link(http: &reqwest::Client, url: &Uri) -> AppResult<String> {
let fxurl = Uri::builder()
.scheme("https")
.authority("d.fxtwitter.com")
.path_and_query(url.path_and_query().unwrap().clone())
.build()?
.to_string();
let req = http.get(fxurl).build()?;
let resp = http.execute(req).await?.error_for_status()?;
let link = resp
.headers()
.get(http::header::LOCATION)
.unwrap()
.to_str()?
.to_owned();
Ok(link)
} }