Compare commits
5 Commits
f588f3cf27
...
7514e98f1b
| Author | SHA1 | Date | |
|---|---|---|---|
| 7514e98f1b | |||
|
3f10b51da9
|
|||
|
947db47fdf
|
|||
|
f49fb9df6f
|
|||
|
252f7b164b
|
@@ -28,3 +28,4 @@ README.md
|
|||||||
readme
|
readme
|
||||||
**/*.db*
|
**/*.db*
|
||||||
**/*.db3*
|
**/*.db3*
|
||||||
|
/mnemodata
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,6 +1,7 @@
|
|||||||
/target
|
/target
|
||||||
.DS_Store
|
.DS_Store
|
||||||
/database
|
/database
|
||||||
|
/mnemodata
|
||||||
*.db
|
*.db
|
||||||
*.db-shm
|
*.db-shm
|
||||||
*.db-wal
|
*.db-wal
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ RUN adduser \
|
|||||||
--uid "${UID}" \
|
--uid "${UID}" \
|
||||||
appuser
|
appuser
|
||||||
|
|
||||||
RUN mkdir -p /app && chown appuser:appuser /app
|
RUN mkdir -p /app/data && chown -R appuser:appuser /app
|
||||||
USER appuser
|
USER appuser
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
ENV IN_DOCKER=true
|
ENV IN_DOCKER=true
|
||||||
|
|||||||
26
compose.yaml
Normal file
26
compose.yaml
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
services:
|
||||||
|
mnemosyne:
|
||||||
|
container_name: mnemosyne
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
target: final
|
||||||
|
ports:
|
||||||
|
- 39321:39321
|
||||||
|
restart: unless-stopped
|
||||||
|
volumes:
|
||||||
|
# Mnemosyne would greatly enjoy not having an ephemeral database.
|
||||||
|
# If you're okay with storing it side by side with the compose file,
|
||||||
|
# a bind mount like this is one way to do it. Remember to mkdir!
|
||||||
|
- ./mnemodata:/app/data
|
||||||
|
# Another way is to use a docker volume.
|
||||||
|
# - mnemodata:/app/data
|
||||||
|
environment:
|
||||||
|
# DATABASE_URL is crucial for Mnemosyne to work; it will fail without it.
|
||||||
|
# Point it at where you'd like your database to be.
|
||||||
|
- DATABASE_URL=/app/data/db.db
|
||||||
|
# Mnemosyne uses port 39321 for HTTP by default;
|
||||||
|
# - PORT=39321
|
||||||
|
|
||||||
|
# Declaring a volume for the docker volume example.
|
||||||
|
# volumes:
|
||||||
|
# mnemodata:
|
||||||
@@ -1,18 +1,8 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
Router,
|
Router,
|
||||||
response::{IntoResponse, Response},
|
|
||||||
routing::{delete, get, patch, post},
|
routing::{delete, get, patch, post},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
|
||||||
database::DatabaseError,
|
|
||||||
persons::PersonError,
|
|
||||||
quotes::QuoteError,
|
|
||||||
tags::TagError,
|
|
||||||
users::{UserError, auth::AuthError, sessions::SessionError},
|
|
||||||
web::RedirectViaError,
|
|
||||||
};
|
|
||||||
|
|
||||||
mod auth;
|
mod auth;
|
||||||
mod persons;
|
mod persons;
|
||||||
mod quotes;
|
mod quotes;
|
||||||
@@ -58,32 +48,3 @@ pub fn api_router() -> Router {
|
|||||||
.route("/api/quotes", post(quotes::create))
|
.route("/api/quotes", post(quotes::create))
|
||||||
.route("/api/quotes/{id}", get(quotes::get_by_id))
|
.route("/api/quotes/{id}", get(quotes::get_by_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CompositeError(Response);
|
|
||||||
impl IntoResponse for CompositeError {
|
|
||||||
fn into_response(self) -> Response {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! composite_from {
|
|
||||||
($($t:ty),+ $(,)?) => {
|
|
||||||
$(
|
|
||||||
impl From<$t> for CompositeError {
|
|
||||||
fn from(e: $t) -> Self {
|
|
||||||
CompositeError(e.into_response())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)+
|
|
||||||
};
|
|
||||||
}
|
|
||||||
composite_from!(
|
|
||||||
AuthError,
|
|
||||||
UserError,
|
|
||||||
SessionError,
|
|
||||||
TagError,
|
|
||||||
PersonError,
|
|
||||||
QuoteError,
|
|
||||||
DatabaseError,
|
|
||||||
RedirectViaError,
|
|
||||||
);
|
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ use serde::Deserialize;
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::CompositeError,
|
database::{self},
|
||||||
database::{self, DatabaseError},
|
error::CompositeError,
|
||||||
logs::{LogAction, LogEntry},
|
logs::{LogAction, LogEntry},
|
||||||
persons::{Name, Person},
|
persons::{Name, Person},
|
||||||
users::{
|
users::{
|
||||||
@@ -54,7 +54,7 @@ pub async fn create(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
let p = Person::create(&tx, form.name, u.id)?;
|
let p = Person::create(&tx, form.name, u.id)?;
|
||||||
LogEntry::new(
|
LogEntry::new(
|
||||||
@@ -65,7 +65,7 @@ pub async fn create(
|
|||||||
pname: p.primary_name.as_str().to_string(),
|
pname: p.primary_name.as_str().to_string(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
Ok((StatusCode::CREATED, Json(p)).into_response())
|
Ok((StatusCode::CREATED, Json(p)).into_response())
|
||||||
}
|
}
|
||||||
pub async fn add_name(
|
pub async fn add_name(
|
||||||
@@ -75,7 +75,7 @@ pub async fn add_name(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
let p = Person::get_by_id(&tx, id)?;
|
let p = Person::get_by_id(&tx, id)?;
|
||||||
let n = p.add_name(&tx, form.name, u.id)?;
|
let n = p.add_name(&tx, form.name, u.id)?;
|
||||||
@@ -89,7 +89,7 @@ pub async fn add_name(
|
|||||||
nn: n.name.clone(),
|
nn: n.name.clone(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
Ok((StatusCode::CREATED, Json(n)).into_response())
|
Ok((StatusCode::CREATED, Json(n)).into_response())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +104,7 @@ pub async fn n_setprimary(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
if !u.has_permission(&tx, Permission::ChangePersonPrimaryName)? {
|
if !u.has_permission(&tx, Permission::ChangePersonPrimaryName)? {
|
||||||
return Ok((StatusCode::FORBIDDEN, CANT_SET_PRIMARYNAME).into_response());
|
return Ok((StatusCode::FORBIDDEN, CANT_SET_PRIMARYNAME).into_response());
|
||||||
@@ -124,7 +124,7 @@ pub async fn n_setprimary(
|
|||||||
nn: n.name.clone(),
|
nn: n.name.clone(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
|
|
||||||
Ok(Json(n).into_response())
|
Ok(Json(n).into_response())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ use serde::Deserialize;
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::CompositeError,
|
database::{self},
|
||||||
database::{self, DatabaseError},
|
error::CompositeError,
|
||||||
logs::{LogAction, LogEntry},
|
logs::{LogAction, LogEntry},
|
||||||
persons::Name,
|
persons::Name,
|
||||||
quotes::Quote,
|
quotes::Quote,
|
||||||
@@ -50,7 +50,7 @@ pub async fn create(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
let lines = form
|
let lines = form
|
||||||
.lines
|
.lines
|
||||||
@@ -69,6 +69,6 @@ pub async fn create(
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
LogEntry::new(&tx, u, LogAction::CreateQuote { id: q.id })?;
|
LogEntry::new(&tx, u, LogAction::CreateQuote { id: q.id })?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
Ok((StatusCode::CREATED, Json(q)).into_response())
|
Ok((StatusCode::CREATED, Json(q)).into_response())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ use axum::{
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::CompositeError,
|
database::{self},
|
||||||
database::{self, DatabaseError},
|
error::CompositeError,
|
||||||
logs::{LogAction, LogEntry},
|
logs::{LogAction, LogEntry},
|
||||||
users::{
|
users::{
|
||||||
User,
|
User,
|
||||||
@@ -43,7 +43,7 @@ pub async fn revoke_by_id(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
let mut s = Session::get_by_id(&tx, id)?;
|
let mut s = Session::get_by_id(&tx, id)?;
|
||||||
match s.user_id == u.id
|
match s.user_id == u.id
|
||||||
@@ -53,7 +53,7 @@ pub async fn revoke_by_id(
|
|||||||
true => {
|
true => {
|
||||||
s.revoke(&tx, Some(&u))?;
|
s.revoke(&tx, Some(&u))?;
|
||||||
LogEntry::new(&tx, u, LogAction::ManuallyRevokeSession { id })?;
|
LogEntry::new(&tx, u, LogAction::ManuallyRevokeSession { id })?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
Ok(Json(s).into_response())
|
Ok(Json(s).into_response())
|
||||||
}
|
}
|
||||||
false => match u.has_permission(&tx, Permission::ListOthersSessions)? {
|
false => match u.has_permission(&tx, Permission::ListOthersSessions)? {
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ use serde::Deserialize;
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::CompositeError,
|
database::{self},
|
||||||
database::{self, DatabaseError},
|
error::CompositeError,
|
||||||
logs::{LogAction, LogEntry},
|
logs::{LogAction, LogEntry},
|
||||||
tags::{Tag, TagName},
|
tags::{Tag, TagName},
|
||||||
users::{
|
users::{
|
||||||
@@ -58,7 +58,7 @@ pub async fn create(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
if !u.has_permission(&tx, Permission::CreateTags)? {
|
if !u.has_permission(&tx, Permission::CreateTags)? {
|
||||||
return Ok((StatusCode::FORBIDDEN, CANT_MAKE_TAGS).into_response());
|
return Ok((StatusCode::FORBIDDEN, CANT_MAKE_TAGS).into_response());
|
||||||
@@ -73,7 +73,7 @@ pub async fn create(
|
|||||||
name: t.name.as_str().to_string(),
|
name: t.name.as_str().to_string(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
Ok(Json(t).into_response())
|
Ok(Json(t).into_response())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ pub async fn rename(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
if !u.has_permission(&tx, Permission::RenameTags)? {
|
if !u.has_permission(&tx, Permission::RenameTags)? {
|
||||||
return Ok((StatusCode::FORBIDDEN, CANT_RENAME_TAGS).into_response());
|
return Ok((StatusCode::FORBIDDEN, CANT_RENAME_TAGS).into_response());
|
||||||
@@ -101,7 +101,7 @@ pub async fn rename(
|
|||||||
nn: tag.name.as_str().to_string(),
|
nn: tag.name.as_str().to_string(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
|
|
||||||
Ok(Json(tag).into_response())
|
Ok(Json(tag).into_response())
|
||||||
}
|
}
|
||||||
@@ -109,7 +109,7 @@ pub async fn rename(
|
|||||||
pub async fn delete(Path(id): Path<Uuid>, headers: HeaderMap) -> Result<Response, CompositeError> {
|
pub async fn delete(Path(id): Path<Uuid>, headers: HeaderMap) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
if !u.has_permission(&tx, Permission::DeleteTags)? {
|
if !u.has_permission(&tx, Permission::DeleteTags)? {
|
||||||
return Ok((StatusCode::FORBIDDEN, CANT_DEL_TAGS).into_response());
|
return Ok((StatusCode::FORBIDDEN, CANT_DEL_TAGS).into_response());
|
||||||
@@ -118,7 +118,7 @@ pub async fn delete(Path(id): Path<Uuid>, headers: HeaderMap) -> Result<Response
|
|||||||
let name = t.name.as_str().to_string();
|
let name = t.name.as_str().to_string();
|
||||||
t.delete(&tx)?;
|
t.delete(&tx)?;
|
||||||
LogEntry::new(&tx, u, LogAction::DeleteTag { id, name })?;
|
LogEntry::new(&tx, u, LogAction::DeleteTag { id, name })?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
|
|
||||||
Ok((StatusCode::OK, TAG_DELETED).into_response())
|
Ok((StatusCode::OK, TAG_DELETED).into_response())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ use serde::Deserialize;
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::CompositeError,
|
database::{self},
|
||||||
database::{self, DatabaseError},
|
error::CompositeError,
|
||||||
logs::{LogAction, LogEntry},
|
logs::{LogAction, LogEntry},
|
||||||
users::{
|
users::{
|
||||||
User,
|
User,
|
||||||
@@ -63,7 +63,7 @@ pub async fn create(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
if !u.has_permission(&tx, Permission::ManuallyCreateUsers)? {
|
if !u.has_permission(&tx, Permission::ManuallyCreateUsers)? {
|
||||||
return Ok((StatusCode::FORBIDDEN, CANT_MANUALLY_MAKE_USERS).into_response());
|
return Ok((StatusCode::FORBIDDEN, CANT_MANUALLY_MAKE_USERS).into_response());
|
||||||
@@ -78,7 +78,7 @@ pub async fn create(
|
|||||||
handle: nu.handle.as_str().to_string(),
|
handle: nu.handle.as_str().to_string(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
|
|
||||||
Ok(Json(nu).into_response())
|
Ok(Json(nu).into_response())
|
||||||
}
|
}
|
||||||
@@ -89,7 +89,7 @@ pub async fn change_handle(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
let mut target = if u.id == id {
|
let mut target = if u.id == id {
|
||||||
u.clone()
|
u.clone()
|
||||||
@@ -111,7 +111,7 @@ pub async fn change_handle(
|
|||||||
new: target.handle.as_str().to_string(),
|
new: target.handle.as_str().to_string(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
|
|
||||||
Ok(HANDLE_CHANGED_SUCCESS.into_response())
|
Ok(HANDLE_CHANGED_SUCCESS.into_response())
|
||||||
}
|
}
|
||||||
@@ -127,7 +127,7 @@ pub async fn change_password(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
let mut target = if u.id == id {
|
let mut target = if u.id == id {
|
||||||
u.clone()
|
u.clone()
|
||||||
@@ -144,7 +144,7 @@ pub async fn change_password(
|
|||||||
u,
|
u,
|
||||||
LogAction::ManuallyChangeUsersPassword { id: target.id },
|
LogAction::ManuallyChangeUsersPassword { id: target.id },
|
||||||
)?;
|
)?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
|
|
||||||
Ok(PASSW_CHANGED_SUCCESS.into_response())
|
Ok(PASSW_CHANGED_SUCCESS.into_response())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ CREATE TABLE users (
|
|||||||
id BLOB NOT NULL UNIQUE PRIMARY KEY, -- UUIDv7 as bytes
|
id BLOB NOT NULL UNIQUE PRIMARY KEY, -- UUIDv7 as bytes
|
||||||
handle TEXT NOT NULL UNIQUE COLLATE NOCASE,
|
handle TEXT NOT NULL UNIQUE COLLATE NOCASE,
|
||||||
password TEXT, -- hashed, nullable in case of OAuth2-only login
|
password TEXT, -- hashed, nullable in case of OAuth2-only login
|
||||||
prof_pic TEXT -- link probably
|
prof_pic TEXT -- serialized ProfilePic
|
||||||
);
|
);
|
||||||
CREATE TABLE sessions (
|
CREATE TABLE sessions (
|
||||||
id BLOB NOT NULL UNIQUE PRIMARY KEY, -- UUIDv7 as bytes
|
id BLOB NOT NULL UNIQUE PRIMARY KEY, -- UUIDv7 as bytes
|
||||||
@@ -8,7 +8,7 @@ macro_rules! migration {
|
|||||||
($name, include_str!(concat!("./migrations/", $name, ".sql")))
|
($name, include_str!(concat!("./migrations/", $name, ".sql")))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const MIGRATIONS: &[(&str, &str)] = &[migration!("2026-03-07--01")];
|
const MIGRATIONS: &[(&str, &str)] = &[migration!("2026-04-04--01")];
|
||||||
|
|
||||||
pub static DB_URL: LazyLock<String> =
|
pub static DB_URL: LazyLock<String> =
|
||||||
LazyLock::new(|| env::var("DATABASE_URL").expect("DATABASE_URL is not set"));
|
LazyLock::new(|| env::var("DATABASE_URL").expect("DATABASE_URL is not set"));
|
||||||
|
|||||||
45
src/error.rs
Normal file
45
src/error.rs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
use axum::response::{IntoResponse, Response};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
database::DatabaseError,
|
||||||
|
persons::PersonError,
|
||||||
|
quotes::QuoteError,
|
||||||
|
tags::TagError,
|
||||||
|
users::{UserError, auth::AuthError, sessions::SessionError},
|
||||||
|
web::RedirectViaError,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct CompositeError(Response);
|
||||||
|
impl IntoResponse for CompositeError {
|
||||||
|
fn into_response(self) -> Response {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! composite_from {
|
||||||
|
($($t:ty),+ $(,)?) => {
|
||||||
|
$(
|
||||||
|
impl From<$t> for CompositeError {
|
||||||
|
fn from(e: $t) -> Self {
|
||||||
|
CompositeError(e.into_response())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)+
|
||||||
|
};
|
||||||
|
}
|
||||||
|
composite_from!(
|
||||||
|
AuthError,
|
||||||
|
UserError,
|
||||||
|
SessionError,
|
||||||
|
TagError,
|
||||||
|
PersonError,
|
||||||
|
QuoteError,
|
||||||
|
DatabaseError,
|
||||||
|
RedirectViaError,
|
||||||
|
);
|
||||||
|
|
||||||
|
impl From<rusqlite::Error> for CompositeError {
|
||||||
|
fn from(e: rusqlite::Error) -> Self {
|
||||||
|
CompositeError(DatabaseError::from(e).into_response())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
use std::fmt::format;
|
|
||||||
|
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use strum::IntoStaticStr;
|
use strum::IntoStaticStr;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use tokio::net::TcpListener;
|
|||||||
mod api;
|
mod api;
|
||||||
mod config;
|
mod config;
|
||||||
mod database;
|
mod database;
|
||||||
|
mod error;
|
||||||
mod logs;
|
mod logs;
|
||||||
mod persons;
|
mod persons;
|
||||||
mod quotes;
|
mod quotes;
|
||||||
|
|||||||
@@ -1,16 +1,22 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
Router,
|
Router,
|
||||||
|
http::header,
|
||||||
response::{IntoResponse, Redirect, Response},
|
response::{IntoResponse, Redirect, Response},
|
||||||
|
routing::get,
|
||||||
};
|
};
|
||||||
use tower_http::services::ServeFile;
|
|
||||||
|
|
||||||
mod components;
|
mod components;
|
||||||
mod icons;
|
mod icons;
|
||||||
mod pages;
|
mod pages;
|
||||||
|
|
||||||
|
pub const STYLES_CSS: &str = include_str!("./styles.css");
|
||||||
|
|
||||||
pub fn web_router() -> Router {
|
pub fn web_router() -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route_service("/styles.css", ServeFile::new("src/web/styles.css"))
|
.route(
|
||||||
|
"/styles.css",
|
||||||
|
get(|| async { ([(header::CONTENT_TYPE, "text/css")], STYLES_CSS) }),
|
||||||
|
)
|
||||||
.merge(pages::pages())
|
.merge(pages::pages())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ use maud::{Markup, PreEscaped, html};
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::CompositeError,
|
database::{self},
|
||||||
database::{self, DatabaseError},
|
error::CompositeError,
|
||||||
persons::{Name, Person},
|
persons::{Name, Person},
|
||||||
quotes::{Quote, QuoteLine},
|
quotes::{Quote, QuoteLine},
|
||||||
tags::Tag,
|
tags::Tag,
|
||||||
@@ -25,7 +25,7 @@ const LINKS: &[(&str, &str, &str)] = &[
|
|||||||
pub async fn page(req: Request) -> Result<Markup, CompositeError> {
|
pub async fn page(req: Request) -> Result<Markup, CompositeError> {
|
||||||
let u = User::authenticate(req.headers()).ok().flatten();
|
let u = User::authenticate(req.headers()).ok().flatten();
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
Ok(base(
|
Ok(base(
|
||||||
"Dashboard | Mnemosyne",
|
"Dashboard | Mnemosyne",
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ use axum::{
|
|||||||
use maud::{PreEscaped, html};
|
use maud::{PreEscaped, html};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::CompositeError,
|
database::{self},
|
||||||
database::{self, DatabaseError},
|
error::CompositeError,
|
||||||
logs::LogEntry,
|
logs::LogEntry,
|
||||||
users::{User, auth::UserAuthenticate, permissions::Permission},
|
users::{User, auth::UserAuthenticate, permissions::Permission},
|
||||||
web::{RedirectViaError, components::nav::nav, icons, pages::base},
|
web::{RedirectViaError, components::nav::nav, icons, pages::base},
|
||||||
@@ -16,7 +16,7 @@ pub async fn page(req: Request) -> Result<Response, CompositeError> {
|
|||||||
let u = User::authenticate(req.headers())?
|
let u = User::authenticate(req.headers())?
|
||||||
.ok_or(RedirectViaError(Redirect::to("/login?re=/logs")))?;
|
.ok_or(RedirectViaError(Redirect::to("/login?re=/logs")))?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
let logs = LogEntry::get_all(&tx)?;
|
let logs = LogEntry::get_all(&tx)?;
|
||||||
|
|
||||||
Ok(base(
|
Ok(base(
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ use maud::{PreEscaped, html};
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::CompositeError,
|
database::{self},
|
||||||
database::{self, DatabaseError},
|
error::CompositeError,
|
||||||
logs::{LogAction, LogEntry},
|
logs::{LogAction, LogEntry},
|
||||||
persons::Person,
|
persons::Person,
|
||||||
users::{
|
users::{
|
||||||
@@ -100,7 +100,7 @@ pub async fn create(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
let p = Person::create(&tx, form.primary_name, u.id)?;
|
let p = Person::create(&tx, form.primary_name, u.id)?;
|
||||||
LogEntry::new(
|
LogEntry::new(
|
||||||
@@ -111,6 +111,6 @@ pub async fn create(
|
|||||||
pname: p.primary_name,
|
pname: p.primary_name,
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
Ok(Redirect::to("/persons").into_response())
|
Ok(Redirect::to("/persons").into_response())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ use maud::{PreEscaped, html};
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::CompositeError,
|
database::{self},
|
||||||
database::{self, DatabaseError},
|
error::CompositeError,
|
||||||
logs::{LogAction, LogEntry},
|
logs::{LogAction, LogEntry},
|
||||||
tags::{Tag, TagName},
|
tags::{Tag, TagName},
|
||||||
users::{
|
users::{
|
||||||
@@ -99,7 +99,7 @@ pub async fn create(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
let t = Tag::create(&tx, form.tagname)?;
|
let t = Tag::create(&tx, form.tagname)?;
|
||||||
LogEntry::new(
|
LogEntry::new(
|
||||||
@@ -110,6 +110,6 @@ pub async fn create(
|
|||||||
name: t.name.to_string(),
|
name: t.name.to_string(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
Ok(Redirect::to("/tags").into_response())
|
Ok(Redirect::to("/tags").into_response())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ use maud::{PreEscaped, html};
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::CompositeError,
|
database::{self},
|
||||||
database::{self, DatabaseError},
|
error::CompositeError,
|
||||||
logs::{LogAction, LogEntry},
|
logs::{LogAction, LogEntry},
|
||||||
users::{
|
users::{
|
||||||
User,
|
User,
|
||||||
@@ -73,8 +73,8 @@ pub async fn create_user(
|
|||||||
Form(form): Form<CreateUserWithPasswordForm>,
|
Form(form): Form<CreateUserWithPasswordForm>,
|
||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let u = User::authenticate(&headers)?.required()?;
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn().map_err(DatabaseError::from)?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
if !u.has_permission(&tx, Permission::ManuallyCreateUsers)? {
|
if !u.has_permission(&tx, Permission::ManuallyCreateUsers)? {
|
||||||
return Ok((StatusCode::FORBIDDEN).into_response());
|
return Ok((StatusCode::FORBIDDEN).into_response());
|
||||||
@@ -89,6 +89,6 @@ pub async fn create_user(
|
|||||||
handle: nu.handle.as_str().to_string(),
|
handle: nu.handle.as_str().to_string(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
Ok(Redirect::to("/users").into_response())
|
Ok(Redirect::to("/users").into_response())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ use maud::{PreEscaped, html};
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::CompositeError,
|
database::{self},
|
||||||
database::{self, DatabaseError},
|
error::CompositeError,
|
||||||
persons::Name,
|
persons::Name,
|
||||||
quotes::{Quote, QuoteLine},
|
quotes::{Quote, QuoteLine},
|
||||||
users::{User, UserError, auth::UserAuthenticate},
|
users::{User, UserError, auth::UserAuthenticate},
|
||||||
@@ -24,8 +24,8 @@ pub async fn page(Path(id): Path<Uuid>, req: Request) -> Result<Response, Compos
|
|||||||
Some(u) => u,
|
Some(u) => u,
|
||||||
None => return Ok(Redirect::to("/users").into_response()),
|
None => return Ok(Redirect::to("/users").into_response()),
|
||||||
};
|
};
|
||||||
let mut conn = database::conn().map_err(DatabaseError::from)?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
let user = match User::get_by_id(&tx, id) {
|
let user = match User::get_by_id(&tx, id) {
|
||||||
Ok(u) => u,
|
Ok(u) => u,
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ use maud::{PreEscaped, html};
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::CompositeError,
|
database::{self},
|
||||||
database::{self, DatabaseError},
|
error::CompositeError,
|
||||||
logs::{LogAction, LogEntry},
|
logs::{LogAction, LogEntry},
|
||||||
users::{
|
users::{
|
||||||
User,
|
User,
|
||||||
@@ -83,7 +83,7 @@ pub async fn change_handle(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let mut u = User::authenticate(&headers)?.required()?;
|
let mut u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
let oldhandle = u.handle.as_str().to_string();
|
let oldhandle = u.handle.as_str().to_string();
|
||||||
u.set_handle(&tx, form.handle)?;
|
u.set_handle(&tx, form.handle)?;
|
||||||
@@ -96,7 +96,7 @@ pub async fn change_handle(
|
|||||||
new: u.handle.as_str().to_string(),
|
new: u.handle.as_str().to_string(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
Ok(Redirect::to("/user-settings").into_response())
|
Ok(Redirect::to("/user-settings").into_response())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,8 +110,8 @@ pub async fn change_password(
|
|||||||
) -> Result<Response, CompositeError> {
|
) -> Result<Response, CompositeError> {
|
||||||
let mut u = User::authenticate(&headers)?.required()?;
|
let mut u = User::authenticate(&headers)?.required()?;
|
||||||
let mut conn = database::conn()?;
|
let mut conn = database::conn()?;
|
||||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
let tx = conn.transaction()?;
|
||||||
u.set_password(&tx, Some(&form.password))?;
|
u.set_password(&tx, Some(&form.password))?;
|
||||||
tx.commit().map_err(DatabaseError::from)?;
|
tx.commit()?;
|
||||||
Ok(Redirect::to("/user-settings").into_response())
|
Ok(Redirect::to("/user-settings").into_response())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user