Skip to content

Commit

Permalink
Add session middleware (Basic auth tests are avaliable, but Bearer an…
Browse files Browse the repository at this point in the history
…d ApiKey ones will be soon!)
  • Loading branch information
auguwu committed Feb 12, 2025
1 parent 313b6b5 commit 17852e1
Show file tree
Hide file tree
Showing 11 changed files with 517 additions and 145 deletions.
37 changes: 32 additions & 5 deletions crates/configuration/src/database/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,39 @@ use azalia::config::{env, merge::Merge, TryFromEnv};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Untagged enumeration to determine if a value is a [`PathBuf`] or a [`String`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, derive_more::From, derive_more::Display)]
#[serde(untagged)]
pub enum StringOrPath {
#[display("{}", _0.display())]
Path(PathBuf),
String(String),
}

impl Merge for StringOrPath {
fn merge(&mut self, other: Self) {
match (self, other) {
(Self::Path(p1), Self::Path(p2)) => {
p1.merge(p2);
}

(Self::String(s1), Self::String(s2)) => {
s1.merge(s2);
}

(me, other) => {
*me = other;
}
}
}
}

/// ## `[database.sqlite]`
///
/// This database driver uses the almighty, holy [SQLite](https://sqlite.org). This is mainly used
/// for development, evaluation purposes, or if PostgreSQL is too heavy for your use-cases.
#[derive(Debug, Clone, Merge, Serialize, Deserialize, derive_more::Deref, derive_more::Display)]
#[display("sqlite://{}", self.path.display())]
#[display("sqlite://{}", self.path)]
pub struct Config {
#[serde(flatten)]
#[deref]
Expand All @@ -33,7 +60,7 @@ pub struct Config {
///
/// The [official Docker image](https://docker.noelware.org/~/charted/server) will overwrite this path to `/var/lib/noelware/charted/data/charted.db`.
#[serde(default = "__db_path")]
pub path: PathBuf,
pub path: StringOrPath,
}

impl Default for Config {
Expand All @@ -54,12 +81,12 @@ impl TryFromEnv for Config {
fn try_from_env() -> Result<Self::Output, Self::Error> {
Ok(Config {
common: common::Config::try_from_env()?,
path: env!(PATH).map(PathBuf::from).unwrap_or(__db_path()),
path: env!(PATH).map(|p| StringOrPath::Path(p.into())).unwrap_or(__db_path()),
})
}
}

#[inline]
fn __db_path() -> PathBuf {
PathBuf::from("./data/charted.db")
fn __db_path() -> StringOrPath {
PathBuf::from("./data/charted.db").into()
}
10 changes: 9 additions & 1 deletion crates/core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl ::schemars::JsonSchema for Version {
pub type Result<T> = std::result::Result<Response<T>, Response>;

/// Representation of a response that the API server sends for each request.
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct Response<T = ()> {
/// The status of the response.
Expand Down Expand Up @@ -144,6 +144,14 @@ pub struct Response<T = ()> {
pub errors: Vec<Error>,
}

impl<T: PartialEq> PartialEq for Response<T> {
fn eq(&self, other: &Self) -> bool {
self.success == other.success && self.data.eq(&other.data) && self.errors.eq(&other.errors)
}
}

impl<T: Eq> Eq for Response<T> {}

#[cfg(feature = "axum")]
#[cfg_attr(any(noeldoc, docsrs), doc(cfg(feature = "axum")))]
impl<T: Serialize> ::axum::response::IntoResponse for Response<T> {
Expand Down
2 changes: 2 additions & 0 deletions crates/database/src/entities/apikey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Model {
pub updated_at: ChronoDateTimeUtc,
pub scopes: i64,
pub owner: Ulid,
pub token: String,
pub name: Name,

#[sea_orm(column_type = "Text", primary_key, auto_increment = false)]
Expand Down Expand Up @@ -82,6 +83,7 @@ pub(crate) fn table() -> TableCreateStatement {
.col(string_len_null(Column::Description, 140))
.col(timestamp_null(Column::ExpiresIn))
.col(big_integer(Column::Scopes))
.col(text(Column::Token))
.col(text(Column::Owner))
.col(Name::into_column(Column::Name))
.col(id())
Expand Down
5 changes: 4 additions & 1 deletion crates/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub mod multipart;
pub mod openapi;
pub mod routing;

#[cfg(test)]
pub mod testing;

use azalia::remi::StorageService;
use charted_authz::Authenticator;
use charted_config::Config;
Expand Down Expand Up @@ -56,7 +59,7 @@ impl Context {
}
}

pub fn set(ctx: Context) {
pub fn set_context(ctx: Context) {
match SINGLETON.set(ctx) {
Ok(_) => {}
Err(_) => panic!("global context was already set"),
Expand Down
Loading

0 comments on commit 17852e1

Please # to comment.