From 22f8028e460e4f9d2b698f2fdd47d456e23d7585 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Fri, 13 Jan 2023 16:25:47 -0300 Subject: [PATCH] Mostly the incoming request --- store/Cargo.toml | 3 ++- store/src/domain/attachment.rs | 11 +++++++++++ store/src/domain/entry.rs | 14 ++++++++++++++ store/src/domain/meta.rs | 10 ++++++++++ store/src/domain/mod.rs | 3 +++ store/src/domain/options.rs | 3 +++ store/src/main.rs | 13 +++++++++++-- 7 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 store/src/domain/attachment.rs create mode 100644 store/src/domain/entry.rs create mode 100644 store/src/domain/meta.rs diff --git a/store/Cargo.toml b/store/Cargo.toml index c3aea8d..4069efe 100644 --- a/store/Cargo.toml +++ b/store/Cargo.toml @@ -14,4 +14,5 @@ sqlx = { version = "0.6.2", features = ["sqlite", "uuid", "chrono", "migrate", " tokio = { version = "1.22.0", features = ["macros", "rt"] } tracing = "0.1.37" tracing-subscriber = { version = "0.3.16", default-features = false, features = ["env-filter", "fmt", "ansi"] } -axum = { version = "0.5.15" } +axum = { version = "0.6", features = ["json"] } +serde = { version = "1.0.152", features = ["derive"] } diff --git a/store/src/domain/attachment.rs b/store/src/domain/attachment.rs new file mode 100644 index 0000000..e086696 --- /dev/null +++ b/store/src/domain/attachment.rs @@ -0,0 +1,11 @@ +//! Versions of the attachments. + +use serde::Deserialize; +use serde::Serialize; + +#[derive(Deserialize, Serialize)] +pub struct IncomingAttachment { + name: String, + content_type: String, + content_base64: String, +} diff --git a/store/src/domain/entry.rs b/store/src/domain/entry.rs new file mode 100644 index 0000000..c228819 --- /dev/null +++ b/store/src/domain/entry.rs @@ -0,0 +1,14 @@ +//! Versions of the entry. + +use serde::Deserialize; +use serde::Serialize; + +#[derive(Deserialize, Serialize)] +pub struct IncomingEntry { + title: String, + content: String, + source: String, + + meta: Vec, + attachments: Vec, +} diff --git a/store/src/domain/meta.rs b/store/src/domain/meta.rs new file mode 100644 index 0000000..f90c8c6 --- /dev/null +++ b/store/src/domain/meta.rs @@ -0,0 +1,10 @@ +//! Version of the entry meta information. + +use serde::Deserialize; +use serde::Serialize; + +#[derive(Deserialize, Serialize)] +pub struct IncomingMeta { + name: String, + value: String, +} diff --git a/store/src/domain/mod.rs b/store/src/domain/mod.rs index 2812cb7..9ba8d68 100644 --- a/store/src/domain/mod.rs +++ b/store/src/domain/mod.rs @@ -1,3 +1,6 @@ //! Store own domain. +pub mod attachment; +pub mod entry; +pub mod meta; pub mod options; diff --git a/store/src/domain/options.rs b/store/src/domain/options.rs index 18ef1a8..eadc9e6 100644 --- a/store/src/domain/options.rs +++ b/store/src/domain/options.rs @@ -12,4 +12,7 @@ pub struct Options { /// Working directory #[arg(short, long, env = "STORE_PATH", default_value = "./")] pub working_directory: PathBuf, + + #[arg(short, long, env = "STORE_ADDR", default_value = "127.0.0.1:4000")] + pub addr: String, } diff --git a/store/src/main.rs b/store/src/main.rs index 3f5c565..c97e32c 100644 --- a/store/src/main.rs +++ b/store/src/main.rs @@ -3,9 +3,12 @@ mod domain; use std::path::Path; use std::sync::Arc; +use axum::extract::State; use axum::routing::post; +use axum::Json; use axum::Router; use clap::Parser; +use domain::entry::IncomingEntry; use sqlx::sqlite::SqliteConnectOptions; use sqlx::sqlite::SqlitePoolOptions; use sqlx::SqlitePool; @@ -26,6 +29,7 @@ async fn main() -> Result<(), sqlx::Error> { let options = Options::parse(); let pool = init_db(&options.working_directory).await?; let state = Arc::new(pool); + serve(state.clone(), &options.addr).await; Ok(()) } @@ -46,7 +50,7 @@ async fn init_db(wd: &Path) -> Result { /// Start the server. async fn serve(state: Arc, addr: &str) { - let app = Router::new().route("/", post(add_entry)); + let app = Router::new().route("/", post(add_entry)).with_state(state); tracing::info!(addr, "Store server up"); axum::Server::bind(&addr.parse().unwrap()) .serve(app.into_make_service()) @@ -55,4 +59,9 @@ async fn serve(state: Arc, addr: &str) { } /// -async fn add_entry(state: Arc) -> Json {} +async fn add_entry( + State(state): State>, + Json(payload): Json, +) -> Json { + Json(payload) +}