Serve business and admin routes from one backend process
This commit is contained in:
@@ -12,19 +12,26 @@ use clusters::{Driver, ResolvedDeployment};
|
||||
use serde::Serialize;
|
||||
use subtle::ConstantTimeEq;
|
||||
|
||||
#[derive(Clone, Serialize)]
|
||||
pub struct DeploymentStatus {
|
||||
deployment: String,
|
||||
#[derive(Clone)]
|
||||
struct AdminState {
|
||||
config: Arc<ResolvedDeployment>,
|
||||
config_version: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct DeploymentStatus<'a> {
|
||||
deployment: &'a str,
|
||||
config_version: Option<&'a str>,
|
||||
storage_driver: &'static str,
|
||||
read_only: bool,
|
||||
}
|
||||
|
||||
impl DeploymentStatus {
|
||||
pub fn new(config: ResolvedDeployment, config_version: Option<String>) -> Self {
|
||||
Self {
|
||||
deployment: config.deployment,
|
||||
config_version,
|
||||
impl AdminState {
|
||||
fn status(&self) -> DeploymentStatus<'_> {
|
||||
let config = &self.config;
|
||||
DeploymentStatus {
|
||||
deployment: &config.deployment,
|
||||
config_version: self.config_version.as_deref(),
|
||||
storage_driver: match config.driver {
|
||||
Driver::Sqlite => "sqlite",
|
||||
Driver::Postgres => "postgres",
|
||||
@@ -35,7 +42,11 @@ impl DeploymentStatus {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn router(status: DeploymentStatus, token: String) -> io::Result<Router> {
|
||||
pub fn router(
|
||||
config: Arc<ResolvedDeployment>,
|
||||
config_version: Option<String>,
|
||||
token: String,
|
||||
) -> io::Result<Router> {
|
||||
if token.is_empty() || !token.bytes().all(|byte| byte.is_ascii_graphic()) {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
@@ -53,11 +64,14 @@ pub fn router(status: DeploymentStatus, token: String) -> io::Result<Router> {
|
||||
.route("/healthz", get(|| async { StatusCode::NO_CONTENT }))
|
||||
.route("/readyz", get(|| async { StatusCode::NO_CONTENT }))
|
||||
.nest("/admin/v1", admin)
|
||||
.with_state(status))
|
||||
.with_state(AdminState {
|
||||
config,
|
||||
config_version,
|
||||
}))
|
||||
}
|
||||
|
||||
async fn deployment_status(State(status): State<DeploymentStatus>) -> impl IntoResponse {
|
||||
([(header::CACHE_CONTROL, "no-store")], Json(status))
|
||||
async fn deployment_status(State(state): State<AdminState>) -> Response {
|
||||
([(header::CACHE_CONTROL, "no-store")], Json(state.status())).into_response()
|
||||
}
|
||||
|
||||
async fn require_admin(State(token): State<Arc<str>>, request: Request, next: Next) -> Response {
|
||||
|
||||
Reference in New Issue
Block a user