mirror of
https://github.com/cupcakearmy/cryptgeon.git
synced 2026-07-04 22:45:31 +00:00
Merge remote-tracking branch 'origin/main' into msgpack
This commit is contained in:
Generated
+1
-1
@@ -252,7 +252,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "cryptgeon"
|
||||
version = "2.9.2"
|
||||
version = "2.9.3"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"bs62",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cryptgeon"
|
||||
version = "2.9.2"
|
||||
version = "2.9.3"
|
||||
authors = ["cupcakearmy <hi@nicco.io>"]
|
||||
edition = "2024"
|
||||
rust-version = "1.95"
|
||||
|
||||
@@ -34,6 +34,10 @@ pub static ref ID_LENGTH: u32 = std::env::var("ID_LENGTH")
|
||||
.unwrap_or("32".to_string())
|
||||
.parse()
|
||||
.unwrap();
|
||||
pub static ref REDIS_PREFIX: String = std::env::var("REDIS_PREFIX")
|
||||
.unwrap_or("".to_string())
|
||||
.parse()
|
||||
.unwrap();
|
||||
pub static ref ALLOW_FILES: bool = std::env::var("ALLOW_FILES")
|
||||
.unwrap_or("true".to_string())
|
||||
.parse()
|
||||
@@ -70,4 +74,8 @@ lazy_static! {
|
||||
.unwrap_or("true".to_string())
|
||||
.parse()
|
||||
.unwrap();
|
||||
pub static ref THEME_HOME_LINK: bool = std::env::var("THEME_HOME_LINK")
|
||||
.unwrap_or("true".to_string())
|
||||
.parse()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ pub struct Status {
|
||||
pub theme_page_title: String,
|
||||
pub theme_favicon: String,
|
||||
pub theme_new_note_notice: bool,
|
||||
pub theme_home_link: bool,
|
||||
}
|
||||
|
||||
pub async fn get_status() -> (StatusCode, Json<Status>) {
|
||||
@@ -33,6 +34,7 @@ pub async fn get_status() -> (StatusCode, Json<Status>) {
|
||||
imprint_url: config::IMPRINT_URL.to_string(),
|
||||
imprint_html: config::IMPRINT_HTML.to_string(),
|
||||
theme_new_note_notice: *config::THEME_NEW_NOTE_NOTICE,
|
||||
theme_home_link: *config::THEME_HOME_LINK,
|
||||
theme_image: config::THEME_IMAGE.to_string(),
|
||||
theme_text: config::THEME_TEXT.to_string(),
|
||||
theme_page_title: config::THEME_PAGE_TITLE.to_string(),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use redis;
|
||||
use redis::Commands;
|
||||
|
||||
use crate::config;
|
||||
use crate::note::now;
|
||||
use crate::note::Note;
|
||||
|
||||
@@ -11,6 +12,10 @@ lazy_static! {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn prefixed(id: &String) -> String {
|
||||
format!("{}{}", config::REDIS_PREFIX.as_str(), id)
|
||||
}
|
||||
|
||||
fn get_connection() -> Result<redis::Connection, &'static str> {
|
||||
let client =
|
||||
redis::Client::open(REDIS_CLIENT.to_string()).map_err(|_| "Unable to connect to redis")?;
|
||||
@@ -28,15 +33,16 @@ pub fn can_reach_redis() -> bool {
|
||||
}
|
||||
|
||||
pub fn set(id: &String, note: &Note) -> Result<(), &'static str> {
|
||||
let key = prefixed(id);
|
||||
let serialized = serde_json::to_string(¬e.clone()).unwrap();
|
||||
let mut conn = get_connection()?;
|
||||
|
||||
conn.set::<_, _, ()>(id, serialized)
|
||||
conn.set::<_, _, ()>(key.as_str(), serialized)
|
||||
.map_err(|_| "Unable to set note in redis")?;
|
||||
match note.expiration {
|
||||
Some(e) => {
|
||||
let seconds = e - now();
|
||||
conn.expire::<_, ()>(id, seconds as i64)
|
||||
conn.expire::<_, ()>(key.as_str(), seconds as i64)
|
||||
.map_err(|_| "Unable to set expiration on note")?
|
||||
}
|
||||
None => {}
|
||||
@@ -45,8 +51,9 @@ pub fn set(id: &String, note: &Note) -> Result<(), &'static str> {
|
||||
}
|
||||
|
||||
pub fn get(id: &String) -> Result<Option<Note>, &'static str> {
|
||||
let key = prefixed(id);
|
||||
let mut conn = get_connection()?;
|
||||
let value: Option<String> = conn.get(id).map_err(|_| "Could not load note in redis")?;
|
||||
let value: Option<String> = conn.get(key.as_str()).map_err(|_| "Could not load note in redis")?;
|
||||
match value {
|
||||
None => return Ok(None),
|
||||
Some(s) => {
|
||||
@@ -57,7 +64,8 @@ pub fn get(id: &String) -> Result<Option<Note>, &'static str> {
|
||||
}
|
||||
|
||||
pub fn del(id: &String) -> Result<(), &'static str> {
|
||||
let key = prefixed(id);
|
||||
let mut conn = get_connection()?;
|
||||
conn.del::<_, ()>(id).map_err(|_| "Unable to delete note in redis")?;
|
||||
conn.del::<_, ()>(key.as_str()).map_err(|_| "Unable to delete note in redis")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user