Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
472 changes: 465 additions & 7 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[workspace]
members = ["atrium-core", "atrium-server", "xtask"]
members = ["atrium-client", "atrium-core", "atrium-server", "xtask"]
resolver = "2"

[workspace.package]
edition = "2024"
version = "0.1.0"

[workspace.dependencies]
base64 = "0.22.1"
bytes = "1.10.1"
clap = { version = "4.5.35", features = ["derive"] }
error-stack = { version = "0.5", default-features = false, features = [
Expand All @@ -21,6 +22,7 @@ log = "0.4.27"
logforth = { version = "0.23.1", features = ["colored"] }
poem = "3.1.7"
pretty_assertions = "1.4.1"
reqwest = "0.12.15"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
styled = "0.2.0"
Expand Down
25 changes: 25 additions & 0 deletions atrium-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "atrium-client"

edition.workspace = true
version.workspace = true

[dependencies]
base64 = { workspace = true }
bytes = { workspace = true }
clap = { workspace = true }
error-stack = { workspace = true }
fastrace = { workspace = true }
foyer = { workspace = true }
log = { workspace = true }
logforth = { workspace = true }
poem = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
toml = { workspace = true }

[dev-dependencies]
insta = { workspace = true }
tempfile = { workspace = true }
17 changes: 17 additions & 0 deletions atrium-client/src/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::client::Client;

pub struct ClientBuilder {
endpoint: String,
}

impl ClientBuilder {
pub fn new(endpoint: String) -> Self {
Self { endpoint }
}

pub fn build(self) -> Client {
Client {
endpoint: self.endpoint,
}
}
}
101 changes: 101 additions & 0 deletions atrium-client/src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use base64::Engine;
use base64::prelude::BASE64_STANDARD;
use error_stack::Result;
use error_stack::ResultExt;
use error_stack::bail;
use reqwest::StatusCode;
use reqwest::Url;

#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub enum Error {
#[from(std::io::Error)]
IO(std::io::Error),
#[from(reqwest::Error)]
Http(reqwest::Error),

Other(String),
}

pub struct Client {
pub(crate) endpoint: String,
}

impl Client {
pub async fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
do_get(self, key).await
}

pub async fn put(&self, key: &[u8], value: &[u8]) -> Result<(), Error> {
do_put(self, key, value).await
}

pub async fn delete(&self, key: &[u8]) -> Result<(), Error> {
do_delete(self, key).await
}
}

async fn do_get(client: &Client, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
let make_error = || Error::Other("failed to get".to_string());
let mut url = Url::parse(&client.endpoint).change_context_lazy(make_error)?;

let encoded_key = BASE64_STANDARD.encode(key);

url = url.join(&encoded_key).change_context_lazy(make_error)?;

let resp = reqwest::get(url).await.change_context_lazy(make_error)?;

match resp.status() {
StatusCode::OK => {
let body = resp.bytes().await.change_context_lazy(make_error)?;
Ok(Some(body.to_vec()))
}

StatusCode::NOT_FOUND => Ok(None),

_ => bail!(Error::Other(resp.status().to_string())),
}
}

async fn do_put(client: &Client, key: &[u8], value: &[u8]) -> Result<(), Error> {
let make_error = || Error::Other("failed to get".to_string());
let mut url = Url::parse(&client.endpoint).change_context_lazy(make_error)?;

let encoded_key = BASE64_STANDARD.encode(key);

url = url.join(&encoded_key).change_context_lazy(make_error)?;

let resp = reqwest::Client::new()
.put(url)
.body(value.to_vec())
.send()
.await
.change_context_lazy(make_error)?;

match resp.status() {
StatusCode::OK | StatusCode::CREATED => Ok(()),

_ => bail!(Error::Other(resp.status().to_string())),
}
}

async fn do_delete(client: &Client, key: &[u8]) -> Result<(), Error> {
let make_error = || Error::Other("failed to get".to_string());
let mut url = Url::parse(&client.endpoint).change_context_lazy(make_error)?;

let encoded_key = BASE64_STANDARD.encode(key);

url = url.join(&encoded_key).change_context_lazy(make_error)?;

let resp = reqwest::Client::new()
.delete(url)
.send()
.await
.change_context_lazy(make_error)?;

match resp.status() {
StatusCode::OK | StatusCode::NO_CONTENT => Ok(()),

_ => bail!(Error::Other(resp.status().to_string())),
}
}
5 changes: 5 additions & 0 deletions atrium-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod builder;
mod client;

pub use builder::ClientBuilder;
pub use client::Client;
1 change: 1 addition & 0 deletions atrium-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version.workspace = true

[dependencies]
atrium-core = { workspace = true }
base64 = { workspace = true }
bytes = { workspace = true }
clap = { workspace = true }
error-stack = { workspace = true }
Expand Down
22 changes: 18 additions & 4 deletions atrium-server/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::Arc;

use atrium_core::Config;
use base64::Engine;
use base64::prelude::BASE64_STANDARD;
use poem::Body;
use poem::Endpoint;
use poem::EndpointExt;
Expand Down Expand Up @@ -85,7 +87,11 @@ struct GetParams {

#[handler]
pub async fn get(Data(ctx): Data<&Arc<Context>>, Query(params): Query<GetParams>) -> Response {
let key = params.key.as_bytes().to_vec();
let Ok(key) = BASE64_STANDARD.decode(params.key) else {
return Response::builder()
.status(StatusCode::BAD_REQUEST)
.body("Bad request");
};
let value = ctx.engine.get(&key).await;

match value {
Expand All @@ -110,7 +116,11 @@ pub async fn put(
Query(params): Query<PutParams>,
body: Body,
) -> Response {
let key = params.key.as_bytes().to_vec();
let Ok(key) = BASE64_STANDARD.decode(params.key) else {
return Response::builder()
.status(StatusCode::BAD_REQUEST)
.body("Bad request");
};
let put_result = body
.into_bytes()
.await
Expand All @@ -136,7 +146,11 @@ pub async fn delete(
Data(ctx): Data<&Arc<Context>>,
Query(params): Query<DeleteParams>,
) -> Response {
let key = params.key.as_bytes();
ctx.engine.delete(key);
let Ok(key) = BASE64_STANDARD.decode(params.key) else {
return Response::builder()
.status(StatusCode::BAD_REQUEST)
.body("Bad request");
};
ctx.engine.delete(&key);
Response::builder().status(StatusCode::NO_CONTENT).body("")
}
Loading