Skip to content

Commit

Permalink
feat: feature and experimental feature flags (#448)
Browse files Browse the repository at this point in the history
* feat: feature and experimental feature flags

* chore: clippy

* fix: review

---------

Co-authored-by: Danny Browning <dbrowning@3box.io>
  • Loading branch information
dbcfd and Danny Browning authored Aug 5, 2024
1 parent f7c467e commit e9675ab
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
7 changes: 7 additions & 0 deletions api/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ pub struct Server<C, I, M> {
// so we just keep track to gracefully shutdown, but if the task dies, the server is in a fatal error state.
insert_task: Arc<InsertTask>,
marker: PhantomData<C>,
authentication: bool,
}

impl<C, I, M> Server<C, I, M>
Expand All @@ -346,9 +347,15 @@ where
model,
insert_task,
marker: PhantomData,
authentication: false,
}
}

pub fn with_authentication(&mut self, authentication: bool) -> &mut Self {
self.authentication = authentication;
self
}

fn start_insert_task(
event_store: Arc<M>,
mut event_rx: tokio::sync::mpsc::Receiver<EventInsert>,
Expand Down
52 changes: 52 additions & 0 deletions one/src/feature_flags.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use anyhow::{anyhow, Result};

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum ExperimentalFeatureFlags {
None,
Authentication,
}

impl std::str::FromStr for ExperimentalFeatureFlags {
type Err = anyhow::Error;

fn from_str(value: &str) -> Result<Self> {
match value.to_ascii_lowercase().as_str() {
"none" => Ok(ExperimentalFeatureFlags::None),
"authentication" => Ok(ExperimentalFeatureFlags::Authentication),
_ => Err(anyhow!("invalid value")),
}
}
}

impl std::fmt::Display for ExperimentalFeatureFlags {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExperimentalFeatureFlags::None => write!(f, "none"),
ExperimentalFeatureFlags::Authentication => write!(f, "authentication"),
}
}
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum FeatureFlags {
None,
}

impl std::fmt::Display for FeatureFlags {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FeatureFlags::None => write!(f, "none"),
}
}
}

impl std::str::FromStr for FeatureFlags {
type Err = anyhow::Error;

fn from_str(value: &str) -> Result<Self> {
match value.to_ascii_lowercase().as_str() {
"none" => Ok(FeatureFlags::None),
_ => Err(anyhow!("invalid value")),
}
}
}
32 changes: 29 additions & 3 deletions one/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Ceramic implements a single binary ceramic node.
#![warn(missing_docs)]

mod feature_flags;
mod http;
mod http_metrics;
mod metrics;
Expand All @@ -17,6 +18,7 @@ use ceramic_metrics::{config::Config as MetricsConfig, MetricsHandle};
use ceramic_p2p::{load_identity, DiskStorage, Keychain, Libp2pConfig};
use ceramic_service::{CeramicEventService, CeramicInterestService, CeramicService};
use clap::{Args, Parser, Subcommand, ValueEnum};
use feature_flags::*;
use futures::StreamExt;
use multibase::Base;
use multihash::Multihash;
Expand All @@ -42,7 +44,7 @@ struct Cli {
#[derive(Subcommand, Debug)]
enum Command {
/// Run a daemon process
Daemon(DaemonOpts),
Daemon(Box<DaemonOpts>),
/// Perform various migrations
#[command(subcommand)]
Migrations(migrations::EventsCommand),
Expand Down Expand Up @@ -167,6 +169,24 @@ struct DaemonOpts {
env = "CERAMIC_ONE_CORS_ALLOW_ORIGINS"
)]
cors_allow_origins: Vec<String>,

/// Enable experimental feature flags
#[arg(
long,
use_value_delimiter = true,
value_delimiter = ',',
env = "CERAMIC_ONE_EXPERIMENTAL_FEATURE_FLAGS"
)]
experimental_feature_flags: Vec<ExperimentalFeatureFlags>,

/// Enable feature flags
#[arg(
long,
use_value_delimiter = true,
value_delimiter = ',',
env = "CERAMIC_ONE_FEATURE_FLAGS"
)]
feature_flags: Vec<FeatureFlags>,
}

#[derive(Args, Debug)]
Expand Down Expand Up @@ -264,7 +284,7 @@ impl Network {
pub async fn run() -> Result<()> {
let args = Cli::parse();
match args.command {
Command::Daemon(opts) => Daemon::run(opts).await,
Command::Daemon(opts) => Daemon::run(*opts).await,
Command::Migrations(opts) => migrations::migrate(opts).await,
}
}
Expand Down Expand Up @@ -534,12 +554,18 @@ impl Daemon {
})?;

// Build HTTP server
let ceramic_server = ceramic_api::Server::new(
let mut ceramic_server = ceramic_api::Server::new(
peer_id,
network,
interest_api_store,
Arc::new(model_api_store),
);
if opts
.experimental_feature_flags
.contains(&ExperimentalFeatureFlags::Authentication)
{
ceramic_server.with_authentication(true);
}
let ceramic_service = ceramic_api_server::server::MakeService::new(ceramic_server);
let ceramic_service = MakeAllowAllAuthenticator::new(ceramic_service, "");
let ceramic_service =
Expand Down

0 comments on commit e9675ab

Please sign in to comment.