Shorten crate directory names and update workspace paths
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m53s
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m53s
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
use std::error::Error;
|
||||
use std::future::Future;
|
||||
use std::process::ExitCode;
|
||||
use std::sync::Arc;
|
||||
|
||||
use aster_core::Vm;
|
||||
use aster_storage::{Storage, StorageConfig};
|
||||
use axum::{Json, Router, extract::State, http::StatusCode, routing::get};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
vm: Arc<Vm>,
|
||||
storage: Storage,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Health {
|
||||
status: &'static str,
|
||||
storage: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct BootstrapInfo {
|
||||
object_model: &'static str,
|
||||
type_system: bool,
|
||||
heap_objects: usize,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> ExitCode {
|
||||
match run().await {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Err(error) => {
|
||||
eprintln!("aster-server: {error}");
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn run() -> Result<(), Box<dyn Error>> {
|
||||
let vm = Arc::new(Vm::new()?);
|
||||
let storage = Storage::connect(&StorageConfig::from_env()?).await?;
|
||||
storage
|
||||
.put("host.version", env!("CARGO_PKG_VERSION"))
|
||||
.await?;
|
||||
let address = std::env::var("ASTER_LISTEN").unwrap_or_else(|_| "127.0.0.1:8080".into());
|
||||
let listener = tokio::net::TcpListener::bind(&address).await?;
|
||||
let shutdown = shutdown_signal()?;
|
||||
let app = Router::new()
|
||||
.route("/health", get(health))
|
||||
.route("/bootstrap", get(bootstrap))
|
||||
.with_state(AppState {
|
||||
vm,
|
||||
storage: storage.clone(),
|
||||
});
|
||||
println!(
|
||||
"Aster host listening on {} (storage={})",
|
||||
listener.local_addr()?,
|
||||
storage.backend_name()
|
||||
);
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(shutdown)
|
||||
.await?;
|
||||
storage.close().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn health(State(state): State<AppState>) -> (StatusCode, Json<Health>) {
|
||||
let (code, status) = match state.storage.ping().await {
|
||||
Ok(()) => (StatusCode::OK, "ok"),
|
||||
Err(_) => (StatusCode::SERVICE_UNAVAILABLE, "storage_unavailable"),
|
||||
};
|
||||
(
|
||||
code,
|
||||
Json(Health {
|
||||
status,
|
||||
storage: state.storage.backend_name(),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
async fn bootstrap(State(state): State<AppState>) -> Json<BootstrapInfo> {
|
||||
Json(BootstrapInfo {
|
||||
object_model: "EndorObject",
|
||||
type_system: false,
|
||||
heap_objects: state.vm.object_count(),
|
||||
})
|
||||
}
|
||||
|
||||
fn shutdown_signal() -> std::io::Result<impl Future<Output = ()>> {
|
||||
#[cfg(unix)]
|
||||
let mut terminate = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?;
|
||||
Ok(async move {
|
||||
#[cfg(unix)]
|
||||
tokio::select! { _ = tokio::signal::ctrl_c() => {}, _ = terminate.recv() => {} }
|
||||
#[cfg(not(unix))]
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn health_reports_unavailable_after_storage_closes() {
|
||||
let storage = Storage::connect(&StorageConfig::SqliteMemory)
|
||||
.await
|
||||
.unwrap();
|
||||
let state = AppState {
|
||||
vm: Arc::new(Vm::new().unwrap()),
|
||||
storage,
|
||||
};
|
||||
let (status, _) = health(State(state.clone())).await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
state.storage.close().await;
|
||||
let (status, Json(body)) = health(State(state)).await;
|
||||
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
|
||||
assert_eq!(body.status, "storage_unavailable");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user