diff --git a/store/Cargo.toml b/store/Cargo.toml index 7fd9e3b..c3aea8d 100644 --- a/store/Cargo.toml +++ b/store/Cargo.toml @@ -14,3 +14,4 @@ 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" } diff --git a/store/src/main.rs b/store/src/main.rs index 95a459a..3f5c565 100644 --- a/store/src/main.rs +++ b/store/src/main.rs @@ -1,8 +1,14 @@ mod domain; +use std::path::Path; +use std::sync::Arc; + +use axum::routing::post; +use axum::Router; use clap::Parser; use sqlx::sqlite::SqliteConnectOptions; use sqlx::sqlite::SqlitePoolOptions; +use sqlx::SqlitePool; use tracing_subscriber::fmt; use tracing_subscriber::prelude::*; use tracing_subscriber::EnvFilter; @@ -18,16 +24,35 @@ async fn main() -> Result<(), sqlx::Error> { .init(); let options = Options::parse(); + let pool = init_db(&options.working_directory).await?; + let state = Arc::new(pool); + Ok(()) +} +/// Initialize the database. +async fn init_db(wd: &Path) -> Result { tracing::debug!("Migrating..."); let pool = SqlitePoolOptions::new() .connect_with( SqliteConnectOptions::new() - .filename(options.working_directory.join("memoirs.sqlite")) + .filename(wd.join("memoirs.sqlite")) .create_if_missing(true), ) .await?; sqlx::migrate!("./migrations").run(&pool).await?; tracing::debug!("Migration done."); - Ok(()) + Ok(pool) } + +/// Start the server. +async fn serve(state: Arc, addr: &str) { + let app = Router::new().route("/", post(add_entry)); + tracing::info!(addr, "Store server up"); + axum::Server::bind(&addr.parse().unwrap()) + .serve(app.into_make_service()) + .await + .unwrap(); +} + +/// +async fn add_entry(state: Arc) -> Json {}