feat: add post3 s3 proxy for postgresql
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
58
crates/post3-server/src/cli.rs
Normal file
58
crates/post3-server/src/cli.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
pub mod serve;
|
||||
|
||||
use anyhow::Context;
|
||||
use clap::{Parser, Subcommand};
|
||||
use post3::{FilesystemBackend, PostgresBackend};
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::state::State;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "post3-server", about = "S3-compatible storage server")]
|
||||
struct App {
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
Serve(serve::ServeCommand),
|
||||
}
|
||||
|
||||
pub async fn execute() -> anyhow::Result<()> {
|
||||
let app = App::parse();
|
||||
|
||||
match app.command {
|
||||
Commands::Serve(cmd) => match cmd.backend {
|
||||
serve::BackendType::Pg => {
|
||||
let database_url =
|
||||
std::env::var("DATABASE_URL").context("DATABASE_URL not set")?;
|
||||
let pool = PgPool::connect(&database_url).await?;
|
||||
|
||||
sqlx::migrate!("../post3/migrations/")
|
||||
.set_locking(false)
|
||||
.run(&pool)
|
||||
.await?;
|
||||
|
||||
tracing::info!("database migrations applied");
|
||||
|
||||
let backend = PostgresBackend::new(pool);
|
||||
let state = State { store: backend };
|
||||
cmd.run(&state).await
|
||||
}
|
||||
serve::BackendType::Fs => {
|
||||
let data_dir = cmd
|
||||
.data_dir
|
||||
.as_ref()
|
||||
.context("--data-dir is required when using --backend fs")?;
|
||||
|
||||
std::fs::create_dir_all(data_dir)?;
|
||||
tracing::info!(path = %data_dir.display(), "using filesystem backend");
|
||||
|
||||
let backend = FilesystemBackend::new(data_dir);
|
||||
let state = State { store: backend };
|
||||
cmd.run(&state).await
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user