Skip to content

Allow building custom standalone servers #2416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/core/src/auth/token_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl CachingOidcTokenValidator {
}

// Jwks fetcher for the async cache.
struct KeyFetcher;
pub struct KeyFetcher;

impl async_cache::Fetcher<Arc<JwksValidator>> for KeyFetcher {
type Error = TokenValidationError;
Expand Down Expand Up @@ -275,7 +275,7 @@ impl TokenValidator for OidcTokenValidator {
}
}

struct JwksValidator {
pub struct JwksValidator {
pub issuer: String,
pub keyset: Jwks,
}
Expand Down
38 changes: 20 additions & 18 deletions crates/standalone/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod subcommands;
pub mod util;
pub mod version;

use crate::control_db::ControlDb;
pub use crate::control_db::ControlDb;
use crate::subcommands::start;
use anyhow::{ensure, Context, Ok};
use async_trait::async_trait;
Expand All @@ -20,7 +20,7 @@ use spacetimedb::host::{
use spacetimedb::identity::Identity;
use spacetimedb::messages::control_db::{Database, Node, Replica};
use spacetimedb::worker_metrics::WORKER_METRICS;
use spacetimedb_client_api::auth::{self, LOCALHOST};
use spacetimedb_client_api::auth::{self, DefaultJwtAuthProvider, JwtAuthProvider, LOCALHOST};
use spacetimedb_client_api::{Host, NodeDelegate};
use spacetimedb_client_api_messages::name::{DomainName, InsertDomainResult, RegisterTldResult, SetDomainsResult, Tld};
use spacetimedb_paths::server::{ModuleLogsDir, PidFile, ServerDataDir};
Expand All @@ -29,17 +29,17 @@ use std::sync::Arc;

pub use spacetimedb_client_api::routes::subscribe::{BIN_PROTOCOL, TEXT_PROTOCOL};

pub struct StandaloneEnv {
control_db: ControlDb,
program_store: Arc<DiskStorage>,
host_controller: HostController,
client_actor_index: ClientActorIndex,
metrics_registry: prometheus::Registry,
_pid_file: PidFile,
auth_provider: auth::DefaultJwtAuthProvider,
pub struct StandaloneEnv<AuthProvider: JwtAuthProvider> {
pub control_db: ControlDb,
pub program_store: Arc<DiskStorage>,
pub host_controller: HostController,
pub client_actor_index: ClientActorIndex,
pub metrics_registry: prometheus::Registry,
pub _pid_file: PidFile,
pub auth_provider: AuthProvider,
}

impl StandaloneEnv {
impl StandaloneEnv<DefaultJwtAuthProvider> {
pub async fn init(
config: Config,
certs: &CertificateAuthority,
Expand Down Expand Up @@ -87,14 +87,16 @@ impl StandaloneEnv {
auth_provider: auth_env,
}))
}
}

impl<AuthProvider: JwtAuthProvider> StandaloneEnv<AuthProvider> {
pub fn data_dir(&self) -> &Arc<ServerDataDir> {
&self.host_controller.data_dir
}
}

struct StandaloneDurabilityProvider {
data_dir: Arc<ServerDataDir>,
pub struct StandaloneDurabilityProvider {
pub data_dir: Arc<ServerDataDir>,
}

#[async_trait]
Expand All @@ -116,7 +118,7 @@ impl DurabilityProvider for StandaloneDurabilityProvider {
}

#[async_trait]
impl NodeDelegate for StandaloneEnv {
impl<AuthProvider: JwtAuthProvider> NodeDelegate for StandaloneEnv<AuthProvider> {
fn gather_metrics(&self) -> Vec<prometheus::proto::MetricFamily> {
self.metrics_registry.gather()
}
Expand All @@ -125,7 +127,7 @@ impl NodeDelegate for StandaloneEnv {
&self.client_actor_index
}

type JwtAuthProviderT = auth::DefaultJwtAuthProvider;
type JwtAuthProviderT = AuthProvider;

fn jwt_auth_provider(&self) -> &Self::JwtAuthProviderT {
&self.auth_provider
Expand Down Expand Up @@ -154,7 +156,7 @@ impl NodeDelegate for StandaloneEnv {
}
}

impl spacetimedb_client_api::ControlStateReadAccess for StandaloneEnv {
impl<AuthProvider: JwtAuthProvider> spacetimedb_client_api::ControlStateReadAccess for StandaloneEnv<AuthProvider> {
// Nodes
fn get_node_id(&self) -> Option<u64> {
Some(0)
Expand Down Expand Up @@ -216,7 +218,7 @@ impl spacetimedb_client_api::ControlStateReadAccess for StandaloneEnv {
}

#[async_trait]
impl spacetimedb_client_api::ControlStateWriteAccess for StandaloneEnv {
impl<AuthProvider: JwtAuthProvider> spacetimedb_client_api::ControlStateWriteAccess for StandaloneEnv<AuthProvider> {
async fn publish_database(
&self,
publisher: &Identity,
Expand Down Expand Up @@ -377,7 +379,7 @@ impl spacetimedb_client_api::ControlStateWriteAccess for StandaloneEnv {
}
}

impl StandaloneEnv {
impl<AuthProvider: JwtAuthProvider> StandaloneEnv<AuthProvider> {
async fn insert_replica(&self, replica: Replica) -> Result<(), anyhow::Error> {
let mut new_replica = replica.clone();
let id = self.control_db.insert_replica(replica)?;
Expand Down
Loading