Skip to content

Commit

Permalink
Merge pull request #26 from pubky/chore/cleanup-unused
Browse files Browse the repository at this point in the history
Chore/cleanup unused
  • Loading branch information
Nuhvi authored Aug 20, 2024
2 parents 7c6d961 + 4391b1c commit 6ed8af7
Show file tree
Hide file tree
Showing 23 changed files with 78 additions and 166 deletions.
2 changes: 1 addition & 1 deletion pubky-homeserver/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Configuration for the server
use anyhow::{anyhow, Result};
use pkarr::{mainline::dht::DhtSettings, Keypair};
use pkarr::Keypair;
// use serde::{Deserialize, Serialize};
use std::{fmt::Debug, path::PathBuf, time::Duration};

Expand Down
23 changes: 9 additions & 14 deletions pubky-homeserver/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ use std::fs;

use std::path::Path;

use bytes::Bytes;
use heed::{types::Str, Database, Env, EnvOpenOptions, RwTxn};
use heed::{Env, EnvOpenOptions};

mod migrations;
pub mod tables;

use pubky_common::crypto::Hasher;

use tables::{entries::Entry, Tables, TABLES_COUNT};

use pkarr::PublicKey;
use tables::blobs::{BlobsTable, BLOBS_TABLE};
use tables::{Tables, TABLES_COUNT};

#[derive(Debug, Clone)]
pub struct DB {
Expand All @@ -37,12 +31,11 @@ impl DB {

#[cfg(test)]
mod tests {
use bytes::Bytes;
use pkarr::Keypair;
use pubky_common::timestamp::Timestamp;

use crate::config::Config;

use super::{Bytes, DB};
use super::DB;

#[tokio::test]
async fn entries() {
Expand All @@ -61,13 +54,15 @@ mod tests {
let cloned_keypair = keypair.clone();

let done = tokio::task::spawn_blocking(move || {
cloned.put_entry(&cloned_keypair.public_key(), path, rx);
cloned
.put_entry(&cloned_keypair.public_key(), path, rx)
.unwrap();
});

tx.send(vec![1, 2, 3, 4, 5].into());
tx.send(vec![1, 2, 3, 4, 5].into()).unwrap();
drop(tx);

done.await;
done.await.unwrap();

let blob = db.get_blob(&keypair.public_key(), path).unwrap().unwrap();

Expand Down
4 changes: 2 additions & 2 deletions pubky-homeserver/src/database/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use heed::{types::Str, Database, Env, RwTxn};
use heed::Env;

mod m0;

Expand All @@ -7,7 +7,7 @@ use super::tables::Tables;
pub fn run(env: &Env) -> anyhow::Result<Tables> {
let mut wtxn = env.write_txn()?;

m0::run(env, &mut wtxn);
m0::run(env, &mut wtxn)?;

let tables = Tables::new(env, &mut wtxn)?;

Expand Down
2 changes: 1 addition & 1 deletion pubky-homeserver/src/database/migrations/m0.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use heed::{types::Str, Database, Env, RwTxn};
use heed::{Env, RwTxn};

use crate::database::tables::{blobs, entries, sessions, users};

Expand Down
9 changes: 2 additions & 7 deletions pubky-homeserver/src/database/tables/blobs.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::{borrow::Cow, time::SystemTime};

use heed::{
types::{Bytes, Str},
BoxedError, BytesDecode, BytesEncode, Database,
};
use heed::{types::Bytes, Database};
use pkarr::PublicKey;

use crate::database::DB;
Expand Down Expand Up @@ -36,7 +31,7 @@ impl DB {
None
};

rtxn.commit();
rtxn.commit()?;

Ok(result)
}
Expand Down
24 changes: 6 additions & 18 deletions pubky-homeserver/src/database/tables/entries.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use pkarr::PublicKey;
use postcard::{from_bytes, to_allocvec};
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, fmt::Result, time::SystemTime};
use tracing::{debug, instrument};

use heed::{
types::{Bytes, Str},
BoxedError, BytesDecode, BytesEncode, Database, RoTxn,
Database, RoTxn,
};

use pubky_common::{
Expand Down Expand Up @@ -53,7 +52,9 @@ impl DB {

let key = format!("{public_key}/{path}");

self.tables.entries.put(&mut wtxn, &key, &entry.serialize());
self.tables
.entries
.put(&mut wtxn, &key, &entry.serialize())?;

wtxn.commit()?;

Expand Down Expand Up @@ -126,11 +127,11 @@ impl DB {
.unwrap_or(next_threshold(path, "", false, reverse, shallow));

for _ in 0..limit {
if let Some((key, _)) = (if reverse {
if let Some((key, _)) = if reverse {
self.tables.entries.get_lower_than(txn, &threshold)?
} else {
self.tables.entries.get_greater_than(txn, &threshold)?
}) {
} {
if !key.starts_with(path) {
break;
}
Expand Down Expand Up @@ -228,25 +229,12 @@ impl Entry {
self
}

pub fn set_content_type(&mut self, content_type: &str) -> &mut Self {
self.content_type = content_type.to_string();
self
}

// === Getters ===

pub fn content_hash(&self) -> &[u8; 32] {
&self.content_hash
}

pub fn content_length(&self) -> usize {
self.content_length
}

pub fn content_type(&self) -> &str {
&self.content_type
}

// === Public Method ===

pub fn serialize(&self) -> Vec<u8> {
Expand Down
9 changes: 2 additions & 7 deletions pubky-homeserver/src/database/tables/sessions.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::{borrow::Cow, time::SystemTime};

use heed::{
types::{Bytes, Str},
BoxedError, BytesDecode, BytesEncode, Database,
Database,
};
use pkarr::PublicKey;
use pubky_common::session::Session;
use serde::Deserialize;
use tower_cookies::Cookies;

use crate::database::DB;
Expand All @@ -21,9 +18,8 @@ impl DB {
&mut self,
cookies: Cookies,
public_key: &PublicKey,
path: &str,
) -> anyhow::Result<Option<Session>> {
if let Some(bytes) = self.get_session_bytes(cookies, public_key, path)? {
if let Some(bytes) = self.get_session_bytes(cookies, public_key)? {
return Ok(Some(Session::deserialize(&bytes)?));
};

Expand All @@ -34,7 +30,6 @@ impl DB {
&mut self,
cookies: Cookies,
public_key: &PublicKey,
path: &str,
) -> anyhow::Result<Option<Vec<u8>>> {
if let Some(cookie) = cookies.get(&public_key.to_string()) {
let rtxn = self.env.read_txn()?;
Expand Down
6 changes: 2 additions & 4 deletions pubky-homeserver/src/database/tables/users.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::{borrow::Cow, time::SystemTime};
use std::borrow::Cow;

use postcard::{from_bytes, to_allocvec};
use pubky_common::timestamp::Timestamp;
use serde::{Deserialize, Serialize};

use heed::{types::Str, BoxedError, BytesDecode, BytesEncode, Database};
use heed::{BoxedError, BytesDecode, BytesEncode, Database};
use pkarr::PublicKey;

extern crate alloc;
use alloc::vec::Vec;

/// PublicKey => User.
pub type UsersTable = Database<PublicKeyCodec, User>;
Expand Down
31 changes: 19 additions & 12 deletions pubky-homeserver/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use axum::{
response::IntoResponse,
};
use pubky_common::auth::AuthnSignatureError;
use tracing::debug;

pub type Result<T, E = Error> = core::result::Result<T, E>;

Expand Down Expand Up @@ -70,6 +69,22 @@ impl From<PathRejection> for Error {
}
}

// === Pubky specific errors ===

impl From<AuthnSignatureError> for Error {
fn from(error: AuthnSignatureError) -> Self {
Self::new(StatusCode::BAD_REQUEST, Some(error))
}
}

impl From<pkarr::Error> for Error {
fn from(error: pkarr::Error) -> Self {
Self::new(StatusCode::BAD_REQUEST, Some(error))
}
}

// === INTERNAL_SERVER_ERROR ===

impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Self::new(StatusCode::INTERNAL_SERVER_ERROR, error.into())
Expand Down Expand Up @@ -100,16 +115,8 @@ impl From<axum::Error> for Error {
}
}

// === Pubky specific errors ===

impl From<AuthnSignatureError> for Error {
fn from(error: AuthnSignatureError) -> Self {
Self::new(StatusCode::BAD_REQUEST, Some(error))
}
}

impl From<pkarr::Error> for Error {
fn from(error: pkarr::Error) -> Self {
Self::new(StatusCode::BAD_REQUEST, Some(error))
impl<T> From<flume::SendError<T>> for Error {
fn from(error: flume::SendError<T>) -> Self {
Self::new(StatusCode::INTERNAL_SERVER_ERROR, error.into())
}
}
4 changes: 0 additions & 4 deletions pubky-homeserver/src/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ impl EntryPath {
pub fn as_str(&self) -> &str {
self.0.as_str()
}

pub fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
}

#[async_trait]
Expand Down
2 changes: 0 additions & 2 deletions pubky-homeserver/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(unused)]

pub mod config;
mod database;
mod error;
Expand Down
8 changes: 1 addition & 7 deletions pubky-homeserver/src/routes.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
use std::sync::Arc;

use axum::{
extract::DefaultBodyLimit,
http::Method,
routing::{delete, get, post, put},
Router,
};
use tower_cookies::CookieManagerLayer;
use tower_http::{
cors::{self, CorsLayer},
trace::TraceLayer,
};
use tower_http::{cors::CorsLayer, trace::TraceLayer};

use crate::server::AppState;

Expand Down
14 changes: 3 additions & 11 deletions pubky-homeserver/src/routes/auth.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
use axum::{
debug_handler,
extract::{Request, State},
http::{uri::Scheme, HeaderMap, StatusCode, Uri},
extract::State,
http::{uri::Scheme, StatusCode, Uri},
response::IntoResponse,
Router,
};
use axum_extra::{headers::UserAgent, TypedHeader};
use bytes::Bytes;
use heed::BytesEncode;
use postcard::to_allocvec;
use tower_cookies::{cookie::SameSite, Cookie, Cookies};

use pubky_common::{
crypto::{random_bytes, random_hash},
session::Session,
timestamp::Timestamp,
};
use pubky_common::{crypto::random_bytes, session::Session, timestamp::Timestamp};

use crate::{
database::tables::{
Expand Down Expand Up @@ -43,7 +36,6 @@ pub async fn signup(

pub async fn session(
State(state): State<AppState>,
TypedHeader(user_agent): TypedHeader<UserAgent>,
cookies: Cookies,
pubky: Pubky,
) -> Result<impl IntoResponse> {
Expand Down
7 changes: 2 additions & 5 deletions pubky-homeserver/src/routes/pkarr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{collections::HashMap, sync::RwLock};

use axum::{
body::{Body, Bytes},
extract::State,
Expand All @@ -10,8 +8,7 @@ use axum::{
};
use futures_util::stream::StreamExt;

use pkarr::{PublicKey, SignedPacket};
use tracing::debug;
use pkarr::SignedPacket;

use crate::{
error::{Error, Result},
Expand All @@ -31,7 +28,7 @@ pub fn pkarr_router(state: AppState) -> Router {
}

pub async fn pkarr_put(
State(mut state): State<AppState>,
State(state): State<AppState>,
pubky: Pubky,
body: Body,
) -> Result<impl IntoResponse> {
Expand Down
Loading

0 comments on commit 6ed8af7

Please sign in to comment.