cba to fix the reqwest get title issue in docker production

This commit is contained in:
h 2023-10-28 17:42:34 -04:00
parent 5b97755da8
commit ea734d7809
5 changed files with 49 additions and 11 deletions

10
Cargo.lock generated
View File

@ -572,6 +572,7 @@ dependencies = [
"axum",
"axum-extra",
"minijinja",
"quick-xml",
"regex",
"reqwest",
"serde",
@ -823,6 +824,15 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "quick-xml"
version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1004a344b30a54e2ee58d66a71b32d2db2feb0a31f9a2d302bf0536f15de2a33"
dependencies = [
"memchr",
]
[[package]]
name = "quote"
version = "1.0.33"

View File

@ -15,3 +15,4 @@ regex = "1.10.0"
tl = "0.7.7"
axum-extra = { version = "0.8.0", features = ["cookie-signed", "cookie"] }
serde = { version = "1.0.189", features = ["derive"] }
quick-xml = "0.31.0"

View File

@ -1,4 +1,4 @@
FROM rust:1.73.0 as builder
FROM rust:1.73-buster as builder
WORKDIR /build
@ -8,16 +8,18 @@ COPY ./Cargo.toml /build/Cargo.toml
RUN rustup update nightly && rustup default nightly && \
cargo clean && cargo build --release
FROM debian:buster-slim
FROM debian:buster
WORKDIR /app
COPY --from=builder /build/target/release/linky /app/linky
RUN mkdir /app/service
RUN apt-get update && apt-get install libssl-dev -y
RUN mkdir /store
RUN touch /store/store.xmlfrag
COPY ./templates/ /app/templates
CMD [ "/app/linky /store/store.xmlfrag" ]
CMD [ "/app/linky", "/store/store.xmlfrag" ]

View File

@ -2,11 +2,11 @@
mod writer;
use axum::{
extract::State,
extract::{Query, State},
http::{HeaderMap, Request, StatusCode},
middleware,
middleware::Next,
response::{Html, Redirect, Response, Result},
response::{Html, IntoResponse, Redirect, Response, Result},
routing::{get, post},
Form, Router,
};
@ -71,7 +71,7 @@ async fn get_login(State(_state): State<Arc<Mutex<LinkFile>>>) -> Result<Html<St
Ok(Html(fs::read_to_string("templates/login.html").unwrap()))
}
async fn get_rss(State(state): State<Arc<Mutex<LinkFile>>>) -> Result<String> {
async fn get_rss(State(state): State<Arc<Mutex<LinkFile>>>) -> Result<Response> {
let template = fs::read_to_string("templates/rss.xml").unwrap();
let linkfile = state.lock().await;
let xmlfrag = fs::read_to_string(linkfile.get_path()).unwrap();
@ -85,12 +85,19 @@ async fn get_rss(State(state): State<Arc<Mutex<LinkFile>>>) -> Result<String> {
env.add_template("rss_xml", &template).unwrap();
let template = env.get_template("rss_xml").unwrap();
Ok(template
let mut resp = template
.render(context! {
xmlfrag => xmlfrag,
link => link,
})
.unwrap())
.unwrap()
.into_response();
resp.headers_mut()
.insert("Content-Type", "text/xml".parse().unwrap())
.unwrap();
Ok(resp)
}
#[derive(Deserialize)]
@ -149,8 +156,14 @@ async fn post_login(
Ok((jar, Redirect::to("/")))
}
#[derive(Deserialize)]
struct Token {
rss_token: Option<String>,
}
async fn authenticate<B>(
headers: HeaderMap,
Query(token): Query<Token>,
request: Request<B>,
next: Next<B>,
) -> Result<Response, Redirect> {
@ -161,5 +174,16 @@ async fn authenticate<B>(
}
}
let rss_token = match env::var("LINKY_TOKEN") {
Ok(v) => v,
Err(_) => "linkytoken".to_string(),
};
if let Some(t) = token.rss_token {
if t == rss_token {
return Ok(next.run(request).await);
}
}
Err(Redirect::to("/login"))
}

View File

@ -1,5 +1,6 @@
use anyhow::{Context, Result};
use minijinja::{context, Environment};
use quick_xml::escape::escape;
use reqwest::Client;
use std::fs::{File, OpenOptions};
use std::io::Write;
@ -53,7 +54,7 @@ impl LinkFile {
pub async fn add_link(&mut self, link: &str) -> Result<()> {
let title = match get_link_title(&self.client, link).await {
Ok(s) => s,
Err(_) => link.to_string(),
Err(_) => escape(link).to_string(),
};
let mut env = Environment::new();
@ -64,7 +65,7 @@ impl LinkFile {
template
.render(context! {
title => title,
link => link,
link => escape(link).to_string(),
})?
.as_bytes(),
)?;