cba to fix the reqwest get title issue in docker production
This commit is contained in:
parent
5b97755da8
commit
ea734d7809
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -572,6 +572,7 @@ dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"axum-extra",
|
"axum-extra",
|
||||||
"minijinja",
|
"minijinja",
|
||||||
|
"quick-xml",
|
||||||
"regex",
|
"regex",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -823,6 +824,15 @@ dependencies = [
|
||||||
"unicode-ident",
|
"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]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.33"
|
version = "1.0.33"
|
||||||
|
|
|
@ -15,3 +15,4 @@ regex = "1.10.0"
|
||||||
tl = "0.7.7"
|
tl = "0.7.7"
|
||||||
axum-extra = { version = "0.8.0", features = ["cookie-signed", "cookie"] }
|
axum-extra = { version = "0.8.0", features = ["cookie-signed", "cookie"] }
|
||||||
serde = { version = "1.0.189", features = ["derive"] }
|
serde = { version = "1.0.189", features = ["derive"] }
|
||||||
|
quick-xml = "0.31.0"
|
||||||
|
|
10
Dockerfile
10
Dockerfile
|
@ -1,4 +1,4 @@
|
||||||
FROM rust:1.73.0 as builder
|
FROM rust:1.73-buster as builder
|
||||||
|
|
||||||
WORKDIR /build
|
WORKDIR /build
|
||||||
|
|
||||||
|
@ -8,16 +8,18 @@ COPY ./Cargo.toml /build/Cargo.toml
|
||||||
RUN rustup update nightly && rustup default nightly && \
|
RUN rustup update nightly && rustup default nightly && \
|
||||||
cargo clean && cargo build --release
|
cargo clean && cargo build --release
|
||||||
|
|
||||||
FROM debian:buster-slim
|
FROM debian:buster
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY --from=builder /build/target/release/linky /app/linky
|
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 mkdir /store
|
||||||
|
|
||||||
|
RUN touch /store/store.xmlfrag
|
||||||
|
|
||||||
COPY ./templates/ /app/templates
|
COPY ./templates/ /app/templates
|
||||||
|
|
||||||
CMD [ "/app/linky /store/store.xmlfrag" ]
|
CMD [ "/app/linky", "/store/store.xmlfrag" ]
|
||||||
|
|
34
src/main.rs
34
src/main.rs
|
@ -2,11 +2,11 @@
|
||||||
mod writer;
|
mod writer;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::State,
|
extract::{Query, State},
|
||||||
http::{HeaderMap, Request, StatusCode},
|
http::{HeaderMap, Request, StatusCode},
|
||||||
middleware,
|
middleware,
|
||||||
middleware::Next,
|
middleware::Next,
|
||||||
response::{Html, Redirect, Response, Result},
|
response::{Html, IntoResponse, Redirect, Response, Result},
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Form, Router,
|
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()))
|
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 template = fs::read_to_string("templates/rss.xml").unwrap();
|
||||||
let linkfile = state.lock().await;
|
let linkfile = state.lock().await;
|
||||||
let xmlfrag = fs::read_to_string(linkfile.get_path()).unwrap();
|
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();
|
env.add_template("rss_xml", &template).unwrap();
|
||||||
let template = env.get_template("rss_xml").unwrap();
|
let template = env.get_template("rss_xml").unwrap();
|
||||||
|
|
||||||
Ok(template
|
let mut resp = template
|
||||||
.render(context! {
|
.render(context! {
|
||||||
xmlfrag => xmlfrag,
|
xmlfrag => xmlfrag,
|
||||||
link => link,
|
link => link,
|
||||||
})
|
})
|
||||||
.unwrap())
|
.unwrap()
|
||||||
|
.into_response();
|
||||||
|
|
||||||
|
resp.headers_mut()
|
||||||
|
.insert("Content-Type", "text/xml".parse().unwrap())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -149,8 +156,14 @@ async fn post_login(
|
||||||
Ok((jar, Redirect::to("/")))
|
Ok((jar, Redirect::to("/")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Token {
|
||||||
|
rss_token: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
async fn authenticate<B>(
|
async fn authenticate<B>(
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
|
Query(token): Query<Token>,
|
||||||
request: Request<B>,
|
request: Request<B>,
|
||||||
next: Next<B>,
|
next: Next<B>,
|
||||||
) -> Result<Response, Redirect> {
|
) -> 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"))
|
Err(Redirect::to("/login"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use minijinja::{context, Environment};
|
use minijinja::{context, Environment};
|
||||||
|
use quick_xml::escape::escape;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use std::fs::{File, OpenOptions};
|
use std::fs::{File, OpenOptions};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
@ -53,7 +54,7 @@ impl LinkFile {
|
||||||
pub async fn add_link(&mut self, link: &str) -> Result<()> {
|
pub async fn add_link(&mut self, link: &str) -> Result<()> {
|
||||||
let title = match get_link_title(&self.client, link).await {
|
let title = match get_link_title(&self.client, link).await {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(_) => link.to_string(),
|
Err(_) => escape(link).to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut env = Environment::new();
|
let mut env = Environment::new();
|
||||||
|
@ -64,7 +65,7 @@ impl LinkFile {
|
||||||
template
|
template
|
||||||
.render(context! {
|
.render(context! {
|
||||||
title => title,
|
title => title,
|
||||||
link => link,
|
link => escape(link).to_string(),
|
||||||
})?
|
})?
|
||||||
.as_bytes(),
|
.as_bytes(),
|
||||||
)?;
|
)?;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user