1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#![deny(
rustdoc::broken_intra_doc_links,
clippy::correctness,
clippy::suspicious,
clippy::complexity,
clippy::perf,
clippy::style,
clippy::cargo,
clippy::nursery,
clippy::dbg_macro,
clippy::rc_buffer,
clippy::as_underscore,
clippy::assertions_on_result_states,
clippy::cast_lossless,
clippy::cast_possible_wrap,
clippy::ptr_as_ptr,
clippy::bool_to_int_with_if,
clippy::borrow_as_ptr,
clippy::case_sensitive_file_extension_comparisons,
clippy::cast_lossless,
clippy::cast_ptr_alignment,
clippy::naive_bytecount
)]
#![allow(clippy::multiple_crate_versions, clippy::missing_const_for_fn)]
pub use axum::{
extract::{Path, Query, State},
handler::Handler,
response::{Html, IntoResponse, Redirect},
routing::{get, post},
Extension, Form, Router,
};
pub use axum_extra::routing::TypedPath;
pub use axum_login::{
memory_store::MemoryStore as AuthMemoryStore, secrecy::SecretVec, AuthLayer, AuthUser,
RequireAuthorizationLayer,
};
pub use axum_sessions::{
async_session::MemoryStore,
extractors::{ReadableSession, WritableSession},
SessionLayer,
};
pub type AuthContext =
axum_login::extractors::AuthContext<i64, auth::User, Arc<AppState>, auth::Role>;
pub type RequireAuth = auth::auth_request::RequireAuthorizationLayer<i64, auth::User, auth::Role>;
pub use std::result::Result;
use std::{borrow::Cow, collections::HashMap, sync::Arc};
use chrono::Datelike;
pub use http::{Request, Response, StatusCode};
pub use mailpot::{models::DbVal, rusqlite::OptionalExtension, *};
use minijinja::{
value::{Object, Value},
Environment, Error, Source,
};
use tokio::sync::RwLock;
pub mod auth;
pub mod cal;
pub mod help;
pub mod lists;
pub mod minijinja_utils;
pub mod settings;
pub mod typed_paths;
pub mod utils;
pub use auth::*;
pub use cal::{calendarize, *};
pub use help::*;
pub use lists::*;
pub use minijinja_utils::*;
pub use settings::*;
pub use typed_paths::{tsr::RouterExt, *};
pub use utils::*;
#[derive(Debug)]
pub struct ResponseError {
pub inner: Box<dyn std::error::Error>,
pub status: StatusCode,
}
impl std::fmt::Display for ResponseError {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "Inner: {}, status: {}", self.inner, self.status)
}
}
impl ResponseError {
pub fn new(msg: String, status: StatusCode) -> Self {
Self {
inner: Box::<dyn std::error::Error + Send + Sync>::from(msg),
status,
}
}
}
impl<E: Into<Box<dyn std::error::Error>>> From<E> for ResponseError {
fn from(err: E) -> Self {
Self {
inner: err.into(),
status: StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
pub trait IntoResponseError {
fn with_status(self, status: StatusCode) -> ResponseError;
}
impl<E: Into<Box<dyn std::error::Error>>> IntoResponseError for E {
fn with_status(self, status: StatusCode) -> ResponseError {
ResponseError {
status,
..ResponseError::from(self)
}
}
}
impl IntoResponse for ResponseError {
fn into_response(self) -> axum::response::Response {
let Self { inner, status } = self;
(status, inner.to_string()).into_response()
}
}
pub trait IntoResponseErrorResult<R> {
fn with_status(self, status: StatusCode) -> std::result::Result<R, ResponseError>;
}
impl<R, E> IntoResponseErrorResult<R> for std::result::Result<R, E>
where
E: IntoResponseError,
{
fn with_status(self, status: StatusCode) -> std::result::Result<R, ResponseError> {
self.map_err(|err| err.with_status(status))
}
}
#[derive(Clone)]
pub struct AppState {
pub conf: Configuration,
pub root_url_prefix: Value,
pub public_url: String,
pub site_title: Cow<'static, str>,
pub user_store: Arc<RwLock<HashMap<i64, User>>>,
}
mod auth_impls {
use super::*;
type UserId = i64;
type User = auth::User;
type Role = auth::Role;
impl AppState {
pub async fn insert_user(&self, pk: UserId, user: User) {
self.user_store.write().await.insert(pk, user);
}
}
#[axum::async_trait]
impl axum_login::UserStore<UserId, Role> for Arc<AppState>
where
User: axum_login::AuthUser<UserId, Role>,
{
type User = User;
async fn load_user(
&self,
user_id: &UserId,
) -> std::result::Result<Option<Self::User>, eyre::Report> {
Ok(self.user_store.read().await.get(user_id).cloned())
}
}
}