Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6469d3aad6 | |||
| 296fb9b1b7 |
Generated
+221
@@ -2,6 +2,16 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "admin_backend"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"axum",
|
||||||
|
"clusters",
|
||||||
|
"serde",
|
||||||
|
"subtle",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "allocator-api2"
|
name = "allocator-api2"
|
||||||
version = "0.2.22"
|
version = "0.2.22"
|
||||||
@@ -17,12 +27,77 @@ dependencies = [
|
|||||||
"num-traits",
|
"num-traits",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "atomic-waker"
|
||||||
|
version = "1.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.5.1"
|
version = "1.5.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "axum"
|
||||||
|
version = "0.8.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90"
|
||||||
|
dependencies = [
|
||||||
|
"axum-core",
|
||||||
|
"bytes",
|
||||||
|
"futures-util",
|
||||||
|
"http",
|
||||||
|
"http-body",
|
||||||
|
"http-body-util",
|
||||||
|
"hyper",
|
||||||
|
"hyper-util",
|
||||||
|
"itoa",
|
||||||
|
"matchit",
|
||||||
|
"memchr",
|
||||||
|
"mime",
|
||||||
|
"percent-encoding",
|
||||||
|
"pin-project-lite",
|
||||||
|
"serde_core",
|
||||||
|
"serde_json",
|
||||||
|
"serde_path_to_error",
|
||||||
|
"sync_wrapper",
|
||||||
|
"tokio",
|
||||||
|
"tower",
|
||||||
|
"tower-layer",
|
||||||
|
"tower-service",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "axum-core"
|
||||||
|
version = "0.5.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"futures-core",
|
||||||
|
"http",
|
||||||
|
"http-body",
|
||||||
|
"http-body-util",
|
||||||
|
"mime",
|
||||||
|
"pin-project-lite",
|
||||||
|
"sync_wrapper",
|
||||||
|
"tower-layer",
|
||||||
|
"tower-service",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "backend"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"admin_backend",
|
||||||
|
"axum",
|
||||||
|
"db",
|
||||||
|
"serde_json",
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "base64"
|
name = "base64"
|
||||||
version = "0.22.1"
|
version = "0.22.1"
|
||||||
@@ -458,6 +533,85 @@ dependencies = [
|
|||||||
"windows-sys 0.61.2",
|
"windows-sys 0.61.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "http"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"itoa",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "http-body"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"http",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "http-body-util"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "23169fe34a5fbcdd3f3862e78fb9b6fccd5f02a6dc6f732547005d45631ce71c"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"futures-core",
|
||||||
|
"http",
|
||||||
|
"http-body",
|
||||||
|
"pin-project-lite",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "httparse"
|
||||||
|
version = "1.10.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "httpdate"
|
||||||
|
version = "1.0.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hyper"
|
||||||
|
version = "1.12.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2c3e324da4c95177d6291d4c8730197c0d1822f8a9766814a4a44fa5ab797c9c"
|
||||||
|
dependencies = [
|
||||||
|
"atomic-waker",
|
||||||
|
"bytes",
|
||||||
|
"futures-core",
|
||||||
|
"http",
|
||||||
|
"http-body",
|
||||||
|
"httparse",
|
||||||
|
"httpdate",
|
||||||
|
"itoa",
|
||||||
|
"pin-project-lite",
|
||||||
|
"smallvec",
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hyper-util"
|
||||||
|
version = "0.1.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ddc03d96684f9226b8a787cdb71488417b53ab5ea8fdb1dac946cb9431cc8bff"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"http",
|
||||||
|
"http-body",
|
||||||
|
"hyper",
|
||||||
|
"pin-project-lite",
|
||||||
|
"tokio",
|
||||||
|
"tower-service",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "icu_collections"
|
name = "icu_collections"
|
||||||
version = "2.4.0"
|
version = "2.4.0"
|
||||||
@@ -649,6 +803,12 @@ version = "0.4.35"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c"
|
checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "matchit"
|
||||||
|
version = "0.8.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "md-5"
|
name = "md-5"
|
||||||
version = "0.10.6"
|
version = "0.10.6"
|
||||||
@@ -665,6 +825,12 @@ version = "2.8.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mime"
|
||||||
|
version = "0.3.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mio"
|
name = "mio"
|
||||||
version = "1.2.5"
|
version = "1.2.5"
|
||||||
@@ -1036,6 +1202,17 @@ dependencies = [
|
|||||||
"zmij",
|
"zmij",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_path_to_error"
|
||||||
|
version = "0.1.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"serde",
|
||||||
|
"serde_core",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_urlencoded"
|
name = "serde_urlencoded"
|
||||||
version = "0.7.1"
|
version = "0.7.1"
|
||||||
@@ -1076,6 +1253,16 @@ version = "2.0.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "signal-hook-registry"
|
||||||
|
version = "1.4.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
|
||||||
|
dependencies = [
|
||||||
|
"errno",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "signature"
|
name = "signature"
|
||||||
version = "2.2.0"
|
version = "2.2.0"
|
||||||
@@ -1365,6 +1552,12 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sync_wrapper"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "synstructure"
|
name = "synstructure"
|
||||||
version = "0.14.0"
|
version = "0.14.0"
|
||||||
@@ -1435,6 +1628,7 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
"mio",
|
"mio",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
|
"signal-hook-registry",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio-macros",
|
"tokio-macros",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.61.2",
|
||||||
@@ -1462,6 +1656,33 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tower"
|
||||||
|
version = "0.5.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4"
|
||||||
|
dependencies = [
|
||||||
|
"futures-core",
|
||||||
|
"futures-util",
|
||||||
|
"pin-project-lite",
|
||||||
|
"sync_wrapper",
|
||||||
|
"tokio",
|
||||||
|
"tower-layer",
|
||||||
|
"tower-service",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tower-layer"
|
||||||
|
version = "0.3.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tower-service"
|
||||||
|
version = "0.3.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tracing"
|
name = "tracing"
|
||||||
version = "0.1.44"
|
version = "0.1.44"
|
||||||
|
|||||||
@@ -7,11 +7,13 @@ edition = "2024"
|
|||||||
rust-version = "1.91"
|
rust-version = "1.91"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
|
axum = { version = "0.8", default-features = false, features = ["http1", "json", "tokio"] }
|
||||||
clusters = { path = "crates/clusters" }
|
clusters = { path = "crates/clusters" }
|
||||||
db = { path = "crates/db" }
|
db = { path = "crates/db" }
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
sqlx = { version = "0.8.6", default-features = false, features = ["any", "runtime-tokio", "tls-rustls-ring-webpki"] }
|
sqlx = { version = "0.8.6", default-features = false, features = ["any", "runtime-tokio", "tls-rustls-ring-webpki"] }
|
||||||
|
subtle = "2"
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
thiserror = "2"
|
thiserror = "2"
|
||||||
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
||||||
|
|||||||
@@ -8,6 +8,29 @@ Bedrock stores JSON documents in SQLite, PostgreSQL or MySQL. Each backend has i
|
|||||||
|
|
||||||
Notion describes sharding online PostgreSQL block data by workspace and feeding a downstream S3 data lake from PostgreSQL changes. Bedrock currently supports selecting a configured SQL cluster and using its read replicas. Workspace sharding, S3 object storage, and data-lake ingestion are future work. [Notion's PostgreSQL sharding](https://www.notion.com/blog/sharding-postgres-at-notion) · [Notion's data lake](https://www.notion.com/blog/building-and-scaling-notions-data-lake)
|
Notion describes sharding online PostgreSQL block data by workspace and feeding a downstream S3 data lake from PostgreSQL changes. Bedrock currently supports selecting a configured SQL cluster and using its read replicas. Workspace sharding, S3 object storage, and data-lake ingestion are future work. [Notion's PostgreSQL sharding](https://www.notion.com/blog/sharding-postgres-at-notion) · [Notion's data lake](https://www.notion.com/blog/building-and-scaling-notions-data-lake)
|
||||||
|
|
||||||
|
## Web backend
|
||||||
|
|
||||||
|
Run the Axum service with `cargo run --locked -p backend`. One process owns
|
||||||
|
the database connection, loads one deployment configuration, and serves
|
||||||
|
separate routers on the business and optional admin listeners.
|
||||||
|
|
||||||
|
| Listener | Setting and default | Routes |
|
||||||
|
|---|---|---|
|
||||||
|
| Business | `BEDROCK_BACKEND_LISTEN`, default `127.0.0.1:8080` | `GET /healthz`, `GET /readyz` |
|
||||||
|
| Admin | `BEDROCK_ADMIN_LISTEN`, default `127.0.0.1:8081` when enabled | `GET /healthz`, `GET /readyz`, `GET /admin/v1/status` |
|
||||||
|
|
||||||
|
The business readiness probe checks access to the current deployment revision.
|
||||||
|
Set `BEDROCK_ADMIN_TOKEN` from a private credential source to enable the admin
|
||||||
|
listener. Send it as `Authorization: Bearer <token>` for the status endpoint.
|
||||||
|
Keep the admin listener on loopback or behind an internal TLS endpoint.
|
||||||
|
|
||||||
|
The `admin_backend` library reports the deployment, storage driver, read-only
|
||||||
|
mode, and optional `BEDROCK_CONFIG_VERSION` for the instance that served the
|
||||||
|
request. It reads the same resolved configuration that the process used to open
|
||||||
|
the database. Give every replica the same config file and version label through
|
||||||
|
your deployment process. The root `bedrock` binary remains the one-shot storage
|
||||||
|
initializer.
|
||||||
|
|
||||||
## Database configuration
|
## Database configuration
|
||||||
|
|
||||||
Set `BEDROCK_DB_CONFIG` to the path of a private JSON deployment configuration to select a different cluster. For example:
|
Set `BEDROCK_DB_CONFIG` to the path of a private JSON deployment configuration to select a different cluster. For example:
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "admin_backend"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition.workspace = true
|
||||||
|
rust-version.workspace = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
axum.workspace = true
|
||||||
|
clusters.workspace = true
|
||||||
|
serde.workspace = true
|
||||||
|
subtle.workspace = true
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
use std::{io, sync::Arc};
|
||||||
|
|
||||||
|
use axum::{
|
||||||
|
Json, Router,
|
||||||
|
extract::{Request, State},
|
||||||
|
http::{HeaderMap, StatusCode, header},
|
||||||
|
middleware::{self, Next},
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
routing::get,
|
||||||
|
};
|
||||||
|
use clusters::{Driver, ResolvedDeployment};
|
||||||
|
use serde::Serialize;
|
||||||
|
use subtle::ConstantTimeEq;
|
||||||
|
|
||||||
|
#[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 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",
|
||||||
|
Driver::Mysql => "mysql",
|
||||||
|
},
|
||||||
|
read_only: config.read_only,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
"BEDROCK_ADMIN_TOKEN must contain visible ASCII characters without spaces",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let admin = Router::new()
|
||||||
|
.route("/status", get(deployment_status))
|
||||||
|
.route_layer(middleware::from_fn_with_state(
|
||||||
|
Arc::<str>::from(token),
|
||||||
|
require_admin,
|
||||||
|
));
|
||||||
|
Ok(Router::new()
|
||||||
|
.route("/healthz", get(|| async { StatusCode::NO_CONTENT }))
|
||||||
|
.route("/readyz", get(|| async { StatusCode::NO_CONTENT }))
|
||||||
|
.nest("/admin/v1", admin)
|
||||||
|
.with_state(AdminState {
|
||||||
|
config,
|
||||||
|
config_version,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
if authorized(request.headers(), &token) {
|
||||||
|
next.run(request).await
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
[(header::WWW_AUTHENTICATE, "Bearer")],
|
||||||
|
)
|
||||||
|
.into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn authorized(headers: &HeaderMap, token: &str) -> bool {
|
||||||
|
let mut values = headers.get_all(header::AUTHORIZATION).iter();
|
||||||
|
let Some(value) = values.next().and_then(|value| value.to_str().ok()) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
if values.next().is_some() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let Some((scheme, supplied)) = value.split_once(' ') else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
scheme.eq_ignore_ascii_case("Bearer") && bool::from(supplied.as_bytes().ct_eq(token.as_bytes()))
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "backend"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition.workspace = true
|
||||||
|
rust-version.workspace = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
admin_backend = { path = "../admin_backend" }
|
||||||
|
axum.workspace = true
|
||||||
|
db.workspace = true
|
||||||
|
serde_json.workspace = true
|
||||||
|
tokio = { workspace = true, features = ["net", "signal", "sync", "time"] }
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
use std::{sync::Arc, time::Duration};
|
||||||
|
|
||||||
|
use axum::{Router, extract::State, http::StatusCode, routing::get};
|
||||||
|
|
||||||
|
pub fn router(db: db::Connection) -> Router {
|
||||||
|
Router::new()
|
||||||
|
.route("/healthz", get(|| async { StatusCode::NO_CONTENT }))
|
||||||
|
.route("/readyz", get(readiness))
|
||||||
|
.with_state(Arc::new(db))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn readiness(State(db): State<Arc<db::Connection>>) -> StatusCode {
|
||||||
|
match tokio::time::timeout(Duration::from_secs(2), db.revision()).await {
|
||||||
|
Ok(Ok(_)) => StatusCode::NO_CONTENT,
|
||||||
|
_ => StatusCode::SERVICE_UNAVAILABLE,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
use std::{env, error::Error, fs, io, net::SocketAddr, sync::Arc};
|
||||||
|
|
||||||
|
use axum::Router;
|
||||||
|
use db::{DeploymentConfig, connect_resolved};
|
||||||
|
use tokio::{net::TcpListener, sync::watch};
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
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) => serde_json::from_slice::<DeploymentConfig>(&fs::read(path)?)?,
|
||||||
|
None => DeploymentConfig::default(),
|
||||||
|
};
|
||||||
|
let config = Arc::new(config.resolve()?);
|
||||||
|
|
||||||
|
let admin_address = optional_env("BEDROCK_ADMIN_LISTEN")?;
|
||||||
|
let admin = match optional_env("BEDROCK_ADMIN_TOKEN")? {
|
||||||
|
Some(token) => {
|
||||||
|
let address: SocketAddr = admin_address
|
||||||
|
.as_deref()
|
||||||
|
.unwrap_or("127.0.0.1:8081")
|
||||||
|
.parse()?;
|
||||||
|
let router = admin_backend::router(
|
||||||
|
Arc::clone(&config),
|
||||||
|
optional_env("BEDROCK_CONFIG_VERSION")?,
|
||||||
|
token,
|
||||||
|
)?;
|
||||||
|
Some((TcpListener::bind(address).await?, router))
|
||||||
|
}
|
||||||
|
None if admin_address.is_some() => {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::InvalidInput,
|
||||||
|
"BEDROCK_ADMIN_LISTEN requires BEDROCK_ADMIN_TOKEN",
|
||||||
|
)
|
||||||
|
.into());
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let listener = TcpListener::bind(address).await?;
|
||||||
|
let db = connect_resolved(&config).await?;
|
||||||
|
println!("Bedrock backend listening on {}", listener.local_addr()?);
|
||||||
|
|
||||||
|
let (stop, shutdown_signal) = watch::channel(false);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
shutdown().await;
|
||||||
|
stop.send_replace(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Some((admin_listener, admin_router)) = admin {
|
||||||
|
println!("Bedrock admin listener on {}", admin_listener.local_addr()?);
|
||||||
|
tokio::try_join!(
|
||||||
|
serve(listener, backend::router(db), shutdown_signal.clone()),
|
||||||
|
serve(admin_listener, admin_router, shutdown_signal),
|
||||||
|
)?;
|
||||||
|
} else {
|
||||||
|
serve(listener, backend::router(db), shutdown_signal).await?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn optional_env(name: &str) -> Result<Option<String>, env::VarError> {
|
||||||
|
match env::var(name) {
|
||||||
|
Ok(value) => Ok(Some(value)),
|
||||||
|
Err(env::VarError::NotPresent) => Ok(None),
|
||||||
|
Err(error) => Err(error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn serve(
|
||||||
|
listener: TcpListener,
|
||||||
|
router: Router,
|
||||||
|
mut shutdown_signal: watch::Receiver<bool>,
|
||||||
|
) -> io::Result<()> {
|
||||||
|
axum::serve(listener, router)
|
||||||
|
.with_graceful_shutdown(async move {
|
||||||
|
let _ = shutdown_signal.wait_for(|stop| *stop).await;
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn shutdown() {
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
use tokio::signal::unix::{SignalKind, signal};
|
||||||
|
if let Ok(mut terminate) = signal(SignalKind::terminate()) {
|
||||||
|
tokio::select! {
|
||||||
|
_ = terminate.recv() => {},
|
||||||
|
_ = tokio::signal::ctrl_c() => {},
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let _ = tokio::signal::ctrl_c().await;
|
||||||
|
}
|
||||||
@@ -44,6 +44,10 @@ pub struct Connection {
|
|||||||
|
|
||||||
pub async fn connect(config: &DeploymentConfig) -> Result<Connection, ConnectError> {
|
pub async fn connect(config: &DeploymentConfig) -> Result<Connection, ConnectError> {
|
||||||
let config = config.resolve()?;
|
let config = config.resolve()?;
|
||||||
|
Ok(connect_resolved(&config).await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn connect_resolved(config: &ResolvedDeployment) -> Result<Connection, StorageError> {
|
||||||
let writer = if config.read_only {
|
let writer = if config.read_only {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
@@ -60,7 +64,7 @@ pub async fn connect(config: &DeploymentConfig) -> Result<Connection, ConnectErr
|
|||||||
Some(Writer::Sql(store)) => store.reader(),
|
Some(Writer::Sql(store)) => store.reader(),
|
||||||
Some(Writer::MySql(store)) => store.reader(),
|
Some(Writer::MySql(store)) => store.reader(),
|
||||||
None => {
|
None => {
|
||||||
let reader = reader(&config, &config.primary)?;
|
let reader = reader(config, &config.primary)?;
|
||||||
reader.revision().await?;
|
reader.revision().await?;
|
||||||
reader
|
reader
|
||||||
}
|
}
|
||||||
@@ -68,7 +72,7 @@ pub async fn connect(config: &DeploymentConfig) -> Result<Connection, ConnectErr
|
|||||||
let replicas = config
|
let replicas = config
|
||||||
.replicas
|
.replicas
|
||||||
.iter()
|
.iter()
|
||||||
.map(|address| reader(&config, address))
|
.map(|address| reader(config, address))
|
||||||
.collect::<Result<_, _>>()?;
|
.collect::<Result<_, _>>()?;
|
||||||
Ok(Connection {
|
Ok(Connection {
|
||||||
driver: config.driver,
|
driver: config.driver,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ pub mod postgres;
|
|||||||
pub mod sqlite;
|
pub mod sqlite;
|
||||||
|
|
||||||
pub use clusters::{Address, Cluster, ConfigError, DeploymentConfig, Driver, Tls};
|
pub use clusters::{Address, Cluster, ConfigError, DeploymentConfig, Driver, Tls};
|
||||||
pub use connection::{ConnectError, Connection, ReadPreference, connect};
|
pub use connection::{ConnectError, Connection, ReadPreference, connect, connect_resolved};
|
||||||
pub use mysql::{Lease, MySql};
|
pub use mysql::{Lease, MySql};
|
||||||
|
|
||||||
pub use common::Error as StorageError;
|
pub use common::Error as StorageError;
|
||||||
|
|||||||
Reference in New Issue
Block a user