Move storage initialization into the backend executable
This commit is contained in:
@@ -1,20 +1,38 @@
|
||||
use std::{env, error::Error, io, net::SocketAddr, sync::Arc};
|
||||
|
||||
use axum::Router;
|
||||
use db::{DeploymentConfig, connect_resolved};
|
||||
use clusters::{DOCUMENTS, DeploymentConfig, ResolvedDeployment};
|
||||
use db::{ConnectError, connect_resolved, connect_sql};
|
||||
use tokio::{net::TcpListener, sync::watch};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let mut args = env::args_os().skip(1);
|
||||
let init_storage = match (args.next(), args.next()) {
|
||||
(None, None) => false,
|
||||
(Some(arg), None) if arg == "--init-storage" => true,
|
||||
_ => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"expected no arguments or --init-storage",
|
||||
)
|
||||
.into());
|
||||
}
|
||||
};
|
||||
let config = match env::var_os("BEDROCK_DB_CONFIG") {
|
||||
Some(path) => DeploymentConfig::load(path)?,
|
||||
None => DeploymentConfig::default(),
|
||||
}
|
||||
.resolve()?;
|
||||
if init_storage {
|
||||
return initialize_storage(&config).await;
|
||||
}
|
||||
|
||||
let address: SocketAddr = optional_env("BEDROCK_BACKEND_LISTEN")?
|
||||
.as_deref()
|
||||
.unwrap_or("127.0.0.1:8080")
|
||||
.parse()?;
|
||||
let config = match env::var_os("BEDROCK_DB_CONFIG") {
|
||||
Some(path) => DeploymentConfig::load(path)?,
|
||||
None => DeploymentConfig::default(),
|
||||
};
|
||||
let config = Arc::new(config.resolve()?);
|
||||
let config = Arc::new(config);
|
||||
|
||||
let admin_address = optional_env("BEDROCK_ADMIN_LISTEN")?;
|
||||
let admin = match optional_env("BEDROCK_ADMIN_TOKEN")? {
|
||||
@@ -62,6 +80,30 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn initialize_storage(resolved: &ResolvedDeployment) -> Result<(), Box<dyn Error>> {
|
||||
if resolved
|
||||
.binding(DOCUMENTS)
|
||||
.is_some_and(|resource| resource.as_sql().is_none())
|
||||
{
|
||||
return Err(ConnectError::InvalidDocuments.into());
|
||||
}
|
||||
for (role, resource) in resolved.bound_resources() {
|
||||
let Some(settings) = resource.as_sql() else {
|
||||
continue;
|
||||
};
|
||||
let connection = connect_sql(settings).await?;
|
||||
println!(
|
||||
"Bedrock storage ready (config {}, role {}, {:?}, revision {}, read-only: {})",
|
||||
resolved.config_version,
|
||||
role,
|
||||
connection.driver(),
|
||||
connection.revision().await?,
|
||||
connection.is_read_only(),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn optional_env(name: &str) -> Result<Option<String>, env::VarError> {
|
||||
match env::var(name) {
|
||||
Ok(value) => Ok(Some(value)),
|
||||
|
||||
Reference in New Issue
Block a user