Compare commits

...

6 commits

Author SHA1 Message Date
asonix 9dcb5114f3 Bump version
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2023-01-29 13:52:37 -06:00
asonix 78ad7e876c Bump tracing-actix-web 2023-01-29 13:51:41 -06:00
asonix 2e7ea3052c Bump ructe 2023-01-29 13:51:04 -06:00
asonix e83f98dbe2 Bump deps 2023-01-29 13:42:21 -06:00
asonix 0fbe26deab Clippy 2023-01-29 13:40:21 -06:00
asonix ff242af2ee Add healthcheck for db 2023-01-29 13:36:24 -06:00
15 changed files with 564 additions and 509 deletions

703
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,7 @@
[package]
name = "pict-rs-aggregator"
description = "A simple image aggregation service for pict-rs"
version = "0.2.0-beta.1"
version = "0.2.0-beta.2"
authors = ["asonix <asonix@asonix.dog>"]
license = "AGPL-3.0"
readme = "README.md"
@ -46,7 +46,7 @@ uuid = { version = "1", features = ["serde", "v4"] }
[dependencies.tracing-actix-web]
version = "0.6.1"
version = "0.7.0"
default-features = false
features = ["emit_event_on_error", "opentelemetry_0_18"]
@ -56,8 +56,8 @@ default-features = false
features = ["emit_event_on_error", "opentelemetry_0_18"]
[dev-dependencies]
ructe = "0.15.0"
ructe = "0.16.0"
[build-dependencies]
dotenv = "0.15.0"
ructe = { version = "0.15.0", features = ["sass", "mime03"] }
ructe = { version = "0.16.0", features = ["sass", "mime03"] }

View file

@ -149,22 +149,22 @@ impl Connection {
fn thumbnail_url(&self, size: u16, file: &str, extension: Extension) -> String {
let mut url = self.upstream.clone();
url.set_path(&format!("/image/process.{}", extension));
url.set_query(Some(&format!("src={}&resize={}", file, size)));
url.set_path(&format!("/image/process.{extension}"));
url.set_query(Some(&format!("src={file}&resize={size}")));
url.to_string()
}
fn image_url(&self, file: &str) -> String {
let mut url = self.upstream.clone();
url.set_path(&format!("/image/original/{}", file,));
url.set_path(&format!("/image/original/{file}"));
url.to_string()
}
fn delete_url(&self, file: &str, token: &str) -> String {
let mut url = self.upstream.clone();
url.set_path(&format!("/image/delete/{}/{}", token, file));
url.set_path(&format!("/image/delete/{token}/{file}"));
url.to_string()
}

View file

@ -163,9 +163,9 @@ impl State {
} else if s.is_empty() {
self.scope.clone()
} else if self.scope.is_empty() {
format!("/{}", s)
format!("/{s}")
} else {
format!("{}/{}", self.scope, s)
format!("{}/{s}", self.scope)
}
}
@ -174,37 +174,31 @@ impl State {
}
fn edit_collection_path(&self, id: Uuid, token: &ValidToken) -> String {
self.scoped(&format!("{}?token={}", id, token.token))
self.scoped(&format!("{id}?token={}", token.token))
}
fn update_collection_path(&self, id: Uuid, token: &ValidToken) -> String {
self.scoped(&format!("{}?token={}", id, token.token))
self.scoped(&format!("{id}?token={}", token.token))
}
fn delete_collection_path(&self, id: Uuid, token: &ValidToken, confirmed: bool) -> String {
if confirmed {
self.scoped(&format!(
"{}/delete?token={}&confirmed=true",
id, token.token
))
self.scoped(&format!("{id}/delete?token={}&confirmed=true", token.token))
} else {
self.scoped(&format!("{}/delete?token={}", id, token.token))
self.scoped(&format!("{id}/delete?token={}", token.token))
}
}
fn public_collection_path(&self, id: Uuid) -> String {
self.scoped(&format!("{}", id))
self.scoped(&format!("{id}"))
}
fn create_entry_path(&self, collection_id: Uuid, token: &ValidToken) -> String {
self.scoped(&format!("{}/entry?token={}", collection_id, token.token))
self.scoped(&format!("{collection_id}/entry?token={}", token.token))
}
fn update_entry_path(&self, collection_id: Uuid, id: Uuid, token: &ValidToken) -> String {
self.scoped(&format!(
"{}/entry/{}?token={}",
collection_id, id, token.token
))
self.scoped(&format!("{collection_id}/entry/{id}?token={}", token.token))
}
fn move_entry_path(
@ -215,8 +209,8 @@ impl State {
direction: Direction,
) -> String {
self.scoped(&format!(
"{}/entry/{}/move/{}?token={}",
collection_id, id, direction, token.token
"{collection_id}/entry/{id}/move/{direction}?token={}",
token.token
))
}
@ -229,25 +223,24 @@ impl State {
) -> String {
if confirmed {
self.scoped(&format!(
"{}/entry/{}/delete?token={}&confirmed=true",
collection_id, id, token.token
"{collection_id}/entry/{id}/delete?token={}&confirmed=true",
token.token
))
} else {
self.scoped(&format!(
"{}/entry/{}/delete?token={}",
collection_id, id, token.token
"{collection_id}/entry/{id}/delete?token={}",
token.token
))
}
}
fn statics_path(&self, file: &str) -> String {
self.scoped(&format!("static/{}", file))
self.scoped(&format!("static/{file}"))
}
fn thumbnail_path(&self, filename: &str, size: u16, extension: pict::Extension) -> String {
self.scoped(&format!(
"image/thumbnail.{}?src={}&size={}",
extension, filename, size
"image/thumbnail.{extension}?src={filename}&size={size}",
))
}
@ -256,9 +249,8 @@ impl State {
for size in connection::VALID_SIZES {
sizes.push(format!(
"{} {}w",
"{} {size}w",
self.thumbnail_path(filename, *size, extension),
size,
))
}
@ -266,7 +258,7 @@ impl State {
}
fn image_path(&self, filename: &str) -> String {
self.scoped(&format!("image/full/{}", filename))
self.scoped(&format!("image/full/{filename}"))
}
}
@ -285,6 +277,7 @@ pub fn configure(cfg: &mut web::ServiceConfig, state: State, client: Client) {
client,
)))
.app_data(web::Data::new(state))
.route("/healthz", web::get().to(healthz))
.service(web::resource("/static/{filename}").route(web::get().to(static_files)))
.service(web::resource("/404").route(web::get().to(not_found)))
.service(
@ -355,6 +348,11 @@ where
}
}
async fn healthz(state: web::Data<State>) -> Result<HttpResponse, StateError> {
state.store.check_health().await.stateful(&state)?;
Ok(HttpResponse::Ok().finish())
}
#[tracing::instrument(name = "Static files")]
async fn static_files(filename: web::Path<String>, state: web::Data<State>) -> HttpResponse {
let filename = filename.into_inner();
@ -377,7 +375,7 @@ async fn static_files(filename: web::Path<String>, state: web::Data<State>) -> H
#[tracing::instrument(name = "Not found")]
async fn not_found(state: web::Data<State>) -> Result<HttpResponse, StateError> {
rendered(
|cursor| self::templates::not_found(cursor, &state),
|cursor| self::templates::not_found_html(cursor, &state),
HttpResponse::NotFound(),
)
.stateful(&state)
@ -409,7 +407,7 @@ impl ResponseError for StateError {
fn error_response(&self) -> HttpResponse {
match rendered(
|cursor| self::templates::error(cursor, &self.error.kind.to_string(), &self.state),
|cursor| self::templates::error_html(cursor, &self.error.kind.to_string(), &self.state),
HttpResponse::build(self.status_code()),
) {
Ok(res) => res,
@ -680,7 +678,7 @@ async fn upload(
}
}
Err(e) => {
tracing::warn!("{}", e);
tracing::warn!("{e}");
let _ = store::DeleteEntry {
entry_path: &entry_path2,
}
@ -740,7 +738,7 @@ async fn thumbnail(
#[tracing::instrument(name = "Index")]
async fn index(state: web::Data<State>) -> Result<HttpResponse, StateError> {
rendered(
|cursor| self::templates::index(cursor, &state),
|cursor| self::templates::index_html(cursor, &state),
HttpResponse::Ok(),
)
.stateful(&state)
@ -777,7 +775,13 @@ async fn view_collection(
rendered(
|cursor| {
self::templates::view_collection(cursor, path.collection, &collection, &entries, &state)
self::templates::view_collection_html(
cursor,
path.collection,
&collection,
&entries,
&state,
)
},
HttpResponse::Ok(),
)
@ -803,7 +807,7 @@ async fn edit_collection(
rendered(
|cursor| {
self::templates::edit_collection(
self::templates::edit_collection_html(
cursor,
&collection,
path.collection,
@ -920,7 +924,7 @@ async fn delete_entry(
if !query.confirmed.unwrap_or(false) {
return rendered(
|cursor| {
self::templates::confirm_entry_delete(
self::templates::confirm_entry_delete_html(
cursor,
entry_path.collection,
entry_path.entry,
@ -976,7 +980,7 @@ fn to_svg_string(qr: &qrcodegen::QrCode, border: i32) -> String {
.checked_add(border.checked_mul(2).unwrap())
.unwrap();
result += &format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 {0} {0}\" stroke=\"none\">\n", dimension);
"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 {dimension} {dimension}\" stroke=\"none\">\n");
result += "\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n";
result += "\t<path d=\"";
for y in 0..qr.size() {
@ -1012,7 +1016,7 @@ async fn delete_collection(
return rendered(
|cursor| {
self::templates::confirm_delete(
self::templates::confirm_delete_html(
cursor,
path.collection,
&collection,

View file

@ -1,12 +1,20 @@
use crate::{Collection, CollectionPath, Direction, Entry, EntryPath, MoveEntryPath, Token};
use actix_web::web;
use sled::{Db, Tree};
use std::collections::BTreeMap;
use std::ops::RangeInclusive;
use std::{
collections::BTreeMap,
ops::RangeInclusive,
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
};
use uuid::Uuid;
#[derive(Clone)]
pub(crate) struct Store {
healthz: Tree,
healthz_counter: Arc<AtomicU64>,
tree: Tree,
}
@ -104,10 +112,25 @@ impl<'a> DeleteEntry<'a> {
impl Store {
pub(crate) fn new(db: &Db) -> Result<Self, sled::Error> {
Ok(Store {
healthz: db.open_tree("healthz")?,
healthz_counter: Arc::new(AtomicU64::new(0)),
tree: db.open_tree("collections")?,
})
}
pub(crate) async fn check_health(&self) -> Result<(), Error> {
let next = self.healthz_counter.fetch_add(1, Ordering::Relaxed);
let healthz = self.healthz.clone();
web::block(move || healthz.insert("healthz", &next.to_be_bytes()[..])).await??;
self.healthz.flush_async().await?;
let healthz = self.healthz.clone();
web::block(move || healthz.get("healthz")).await??;
Ok(())
}
async fn create_collection(&self, config: CreateCollection<'_>) -> Result<(), Error> {
let collection_key = config.collection_path.key();
let collection_value = serde_json::to_string(&config.collection)?;

View file

@ -3,22 +3,22 @@
@(text: &str, kind: ButtonKind)
@match kind {
ButtonKind::Submit => {
<div class="button submit">
<span>@text</span>
<button class="action" type="submit">@text</button>
</div>
}
ButtonKind::Plain => {
<div class="button plain">
<span>@text</span>
<button class="action">@text</button>
</div>
}
ButtonKind::Outline => {
<div class="button outline">
<span>@text</span>
<button class="action">@text</button>
</div>
}
ButtonKind::Submit => {
<div class="button submit">
<span>@text</span>
<button class="action" type="submit">@text</button>
</div>
}
ButtonKind::Plain => {
<div class="button plain">
<span>@text</span>
<button class="action">@text</button>
</div>
}
ButtonKind::Outline => {
<div class="button outline">
<span>@text</span>
<button class="action">@text</button>
</div>
}
}

View file

@ -3,23 +3,22 @@
@(text: &str, href: &str, kind: ButtonKind)
@match kind {
ButtonKind::Submit => {
<div class="button submit">
<span>@text</span>
<a class="action" href="@href">@text</a>
</div>
}
ButtonKind::Plain => {
<div class="button plain">
<span>@text</span>
<a class="action" href="@href">@text</a>
</div>
}
ButtonKind::Outline => {
<div class="button outline">
<span>@text</span>
<a class="action" href="@href">@text</a>
</div>
}
ButtonKind::Submit => {
<div class="button submit">
<span>@text</span>
<a class="action" href="@href">@text</a>
</div>
}
ButtonKind::Plain => {
<div class="button plain">
<span>@text</span>
<a class="action" href="@href">@text</a>
</div>
}
ButtonKind::Outline => {
<div class="button outline">
<span>@text</span>
<a class="action" href="@href">@text</a>
</div>
}
}

View file

@ -1,27 +1,28 @@
@use crate::{ui::ButtonKind, Collection, State, ValidToken};
@use super::{layout, button_link, return_home};
@use super::{layout_html, button_link_html, return_home_html};
@use uuid::Uuid;
@(id: Uuid, collection: &Collection, token: &ValidToken, state: &State)
@:layout(state, &format!("Delete: {}", collection.title), Some(&format!("Are you sure you want to delete {}", collection.title)), {
<meta property="og:url" content="@state.delete_collection_path(id, token, false)" />
@:layout_html(state, &format!("Delete: {}", collection.title), Some(&format!("Are you sure you want to delete {}",
collection.title)), {
<meta property="og:url" content="@state.delete_collection_path(id, token, false)" />
}, {
<section>
<article>
<div class="content-group">
<h3>Delete @collection.title</h3>
</div>
<div class="content-group">
<p class="subtitle">Are you sure you want to delete @collection.title</p>
</div>
<div class="content-group">
<div class="button-group">
@:button_link("Delete Collection", &state.delete_collection_path(id, token, true), ButtonKind::Submit)
@:button_link("Cancel", &state.edit_collection_path(id, token), ButtonKind::Outline)
</div>
</div>
</article>
<article>
<div class="content-group">
<h3>Delete @collection.title</h3>
</div>
<div class="content-group">
<p class="subtitle">Are you sure you want to delete @collection.title</p>
</div>
<div class="content-group">
<div class="button-group">
@:button_link_html("Delete Collection", &state.delete_collection_path(id, token, true), ButtonKind::Submit)
@:button_link_html("Cancel", &state.edit_collection_path(id, token), ButtonKind::Outline)
</div>
</div>
</article>
</section>
@:return_home(state)
@:return_home_html(state)
})

View file

@ -1,33 +1,33 @@
@use crate::{ui::ButtonKind, Entry, State, ValidToken};
@use super::{layout, button_link, image, return_home};
@use super::{layout_html, button_link_html, image_html, return_home_html};
@use uuid::Uuid;
@(collection_id: Uuid, id: Uuid, entry: &Entry, token: &ValidToken, state: &State)
@:layout(state, "Delete Image", Some("Are you sure you want to delete this image?"), {
<meta property="og:url" content="@state.delete_entry_path(collection_id, id, token, false)" />
@:layout_html(state, "Delete Image", Some("Are you sure you want to delete this image?"), {
<meta property="og:url" content="@state.delete_entry_path(collection_id, id, token, false)" />
}, {
<section>
<article>
<div class="content-group">
<h3>Delete Image</h3>
<article>
<div class="content-group">
<h3>Delete Image</h3>
</div>
<div class="content-group">
<div class="edit-row">
<div class="edit-item">
@:image_html(entry, state)
</div>
<div class="content-group">
<div class="edit-row">
<div class="edit-item">
@:image(entry, state)
</div>
<div class="edit-item">
<p class="delete-confirmation">Are you sure you want to delete this image?</p>
<div class="button-group button-space">
@:button_link("Delete Image", &state.delete_entry_path(collection_id, id, token, true), ButtonKind::Submit)
@:button_link("Cancel", &state.edit_collection_path(collection_id, token), ButtonKind::Outline)
</div>
</div>
</div>
<div class="edit-item">
<p class="delete-confirmation">Are you sure you want to delete this image?</p>
<div class="button-group button-space">
@:button_link_html("Delete Image", &state.delete_entry_path(collection_id, id, token, true),
ButtonKind::Submit)
@:button_link_html("Cancel", &state.edit_collection_path(collection_id, token), ButtonKind::Outline)
</div>
</div>
</article>
</div>
</div>
</article>
</section>
@:return_home(state)
@:return_home_html(state)
})

View file

@ -1,11 +1,12 @@
@use crate::{ui::ButtonKind, Collection, Direction, Entry, State, ValidToken};
@use super::{button, button_link, image, file_input, layout, return_home, text_area, text_input,
@use super::{button_html, button_link_html, image_html, file_input_html, layout_html, return_home_html, text_area_html,
text_input_html,
statics::file_upload_js};
@use uuid::Uuid;
@(collection: &Collection, collection_id: Uuid, entries: &[(Uuid, Entry)], token: &ValidToken, state: &State, qr: &str)
@:layout(state, "Edit Collection", None, {
@:layout_html(state, "Edit Collection", None, {
<script src="@state.statics_path(file_upload_js.name)" type="text/javascript">
</script>
}, {
@ -33,11 +34,11 @@ statics::file_upload_js};
</article>
<article class="content-group">
<form method="POST" action="@state.update_collection_path(collection_id, token)">
@:text_input("title", Some("Collection Title"), Some(&collection.title), false)
@:text_area("description", Some("Collection Description"), Some(&collection.description), false)
@:text_input_html("title", Some("Collection Title"), Some(&collection.title), false)
@:text_area_html("description", Some("Collection Description"), Some(&collection.description), false)
<div class="button-group button-space">
@:button("Update Collection", ButtonKind::Submit)
@:button_link("Delete Collection", &state.delete_collection_path(collection_id, token, false),
@:button_html("Update Collection", ButtonKind::Submit)
@:button_link_html("Delete Collection", &state.delete_collection_path(collection_id, token, false),
ButtonKind::Outline)
</div>
</form>
@ -48,37 +49,37 @@ statics::file_upload_js};
<article>
<div class="edit-row">
<div class="edit-item">
@:image(entry, state)
@:image_html(entry, state)
</div>
<div class="edit-item">
<form method="POST" action="@state.update_entry_path(collection_id, *id, token)">
@:text_input("title", Some("Image Title"), entry.title.as_deref(), entry.file_parts().is_none())
@:text_area("description", Some("Image Description"), entry.description.as_deref(),
@:text_input_html("title", Some("Image Title"), entry.title.as_deref(), entry.file_parts().is_none())
@:text_area_html("description", Some("Image Description"), entry.description.as_deref(),
entry.file_parts().is_none())
@:text_input("link", Some("Image Link"), entry.link.as_ref().map(|l| l.as_str()),
@:text_input_html("link", Some("Image Link"), entry.link.as_ref().map(|l| l.as_str()),
entry.file_parts().is_none())
<div class="button-group button-space">
@if let Some(upload_id) = entry.upload_id() {
<input type="hidden" name="upload_id" value="@upload_id" />
@:button_link("Refresh", &state.edit_collection_path(collection_id, token), ButtonKind::Submit)
@:button_link_html("Refresh", &state.edit_collection_path(collection_id, token), ButtonKind::Submit)
}
@if let Some((filename, delete_token)) = entry.file_parts() {
<input type="hidden" name="filename" value="@filename" />
<input type="hidden" name="delete_token" value="@delete_token" />
@:button("Update Image", ButtonKind::Submit)
@:button_link("Delete Image", &state.delete_entry_path(collection_id, *id, token, false),
@:button_html("Update Image", ButtonKind::Submit)
@:button_link_html("Delete Image", &state.delete_entry_path(collection_id, *id, token, false),
ButtonKind::Outline)
}
</div>
<div class="button-group button-space">
@if i != 0 {
@:button_link("Move Up", &state.move_entry_path(collection_id, *id, token, Direction::Up),
@:button_link_html("Move Up", &state.move_entry_path(collection_id, *id, token, Direction::Up),
ButtonKind::Outline)
}
@if (i + 1) != entries.len() {
@:button_link("Move Down", &state.move_entry_path(collection_id, *id, token, Direction::Down),
@:button_link_html("Move Down", &state.move_entry_path(collection_id, *id, token, Direction::Down),
ButtonKind::Outline)
}
</div>
@ -100,14 +101,14 @@ statics::file_upload_js};
</div>
<div class="content-group" id="file-input-container">
<div class="button-group">
@:file_input("images[]", Some("Select Image"), Some(crate::accept()), false)
@:file_input_html("images[]", Some("Select Image"), Some(crate::accept()), false)
</div>
<div class="button-group button-space">
@:button("Upload", ButtonKind::Submit)
@:button_html("Upload", ButtonKind::Submit)
</div>
</div>
</form>
</article>
</section>
@:return_home(state)
@:return_home_html(state)
})

View file

@ -1,18 +1,18 @@
@use crate::State;
@use super::{layout, return_home};
@use super::{layout_html, return_home_html};
@(error: &str, state: &State)
@:layout(state, "Error", Some(error), {}, {
@:layout_html(state, "Error", Some(error), {}, {
<section>
<article>
<div class="content-group">
<h3>Error</h3>
</div>
<div class="content-group">
<p class="subtitle">@error</p>
</div>
</article>
<article>
<div class="content-group">
<h3>Error</h3>
</div>
<div class="content-group">
<p class="subtitle">@error</p>
</div>
</article>
</section>
@:return_home(state)
@:return_home_html(state)
})

View file

@ -1,9 +1,9 @@
@use crate::{Entry, State};
@use super::image_preview;
@use super::image_preview_html;
@(entry: &Entry, state: &State)
@:image_preview(entry, state)
@:image_preview_html(entry, state)
<div class="image-meta">
@if let Some(title) = entry.title.as_ref() {
<div class="image-title">@title</div>

View file

@ -1,9 +1,9 @@
@use crate::{ui::ButtonKind, State};
@use super::{layout, text_area, text_input, button};
@use super::{layout_html, text_area_html, text_input_html, button_html};
@(state: &State)
@:layout(state, "Collection", None, {}, {
@:layout_html(state, "Collection", None, {}, {
<section>
<article>
<form method="POST" action="@state.create_collection_path()">
@ -13,12 +13,12 @@
</h3>
</div>
<div class="content-group">
@:text_input("title", Some("Title"), None, false)
@:text_area("description", Some("Description"), None, false)
@:text_input_html("title", Some("Title"), None, false)
@:text_area_html("description", Some("Description"), None, false)
</div>
<div class="content-group">
<div class="button-group">
@:button("Create Collection", ButtonKind::Submit)
@:button_html("Create Collection", ButtonKind::Submit)
</div>
</div>
</form>

View file

@ -1,15 +1,15 @@
@use crate::State;
@use super::layout;
@use super::layout_html;
@(state: &State)
@:layout(state, "Not Found", None, {}, {
@:layout_html(state, "Not Found", None, {}, {
<section>
<article class="content-group">
<h3>Not Found</h3>
</article>
<article class="content-group">
<p><a href="@state.create_collection_path()">Return Home</a></p>
</article>
<article class="content-group">
<h3>Not Found</h3>
</article>
<article class="content-group">
<p><a href="@state.create_collection_path()">Return Home</a></p>
</article>
</section>
})

View file

@ -1,10 +1,10 @@
@use crate::{Collection, Entry, State};
@use super::{layout, image, return_home};
@use super::{layout_html, image_html, return_home_html};
@use uuid::Uuid;
@(id: Uuid, collection: &Collection, entries: &[(Uuid, Entry)], state: &State)
@:layout(state, &collection.title, Some(&collection.description), {
@:layout_html(state, &collection.title, Some(&collection.description), {
<meta property="twitter:card" content="summary_large_image" />
<meta property="twitter:url" content="@state.public_collection_path(id)" />
<meta property="og:url" content="@state.public_collection_path(id)" />
@ -31,11 +31,11 @@
@for (_, entry) in entries {
<li class="content-group even">
<article>
@:image(entry, state)
@:image_html(entry, state)
</article>
</li>
}
</ul>
</section>
@:return_home(state)
@:return_home_html(state)
})