Browse Source

Plugging Axum into the store

main
Julio Biason 1 year ago
parent
commit
fcc17a3a23
  1. 1
      store/Cargo.toml
  2. 29
      store/src/main.rs

1
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" }

29
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<SqlitePool, sqlx::Error> {
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<SqlitePool>, 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<SqlitePool>) -> Json {}

Loading…
Cancel
Save