|
|
|
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;
|
|
|
|
use tracing_subscriber::fmt;
|
|
|
|
use tracing_subscriber::prelude::*;
|
|
|
|
use tracing_subscriber::EnvFilter;
|
|
|
|
|
|
|
|
use crate::domain::options::Options;
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), sqlx::Error> {
|
|
|
|
dotenv::dotenv().ok();
|
|
|
|
tracing_subscriber::registry()
|
|
|
|
.with(fmt::layer())
|
|
|
|
.with(EnvFilter::from_default_env())
|
|
|
|
.init();
|
|
|
|
|
|
|
|
let options = Options::parse();
|
|
|
|
let pool = init_db(&options.working_directory).await?;
|
|
|
|
let state = Arc::new(pool);
|
|
|
|
serve(state.clone(), &options.addr).await;
|
|
|
|
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(wd.join("memoirs.sqlite"))
|
|
|
|
.create_if_missing(true),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
sqlx::migrate!("./migrations").run(&pool).await?;
|
|
|
|
tracing::debug!("Migration done.");
|
|
|
|
Ok(pool)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start the server.
|
|
|
|
async fn serve(state: Arc<SqlitePool>, addr: &str) {
|
|
|
|
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())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
async fn add_entry(
|
|
|
|
State(state): State<Arc<SqlitePool>>,
|
|
|
|
Json(payload): Json<IncomingEntry>,
|
|
|
|
) -> Json<IncomingEntry> {
|
|
|
|
Json(payload)
|
|
|
|
}
|