Skip to content

Commit

Permalink
feat(FTL-16583): parametrized api prefix added (#23)
Browse files Browse the repository at this point in the history
* feat(FTL-16583): parametrized api prefix added

* fix(FTL-16583): atm fixes

* fix(FTL-16583): default MEDIATOR_DID restored

* fix(FTL-16583): fmt fixes
  • Loading branch information
robert-affinidi authored Sep 13, 2024
1 parent 2c7782c commit 438b8d3
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 89 deletions.
4 changes: 4 additions & 0 deletions affinidi-messaging-mediator/conf/mediator.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ mediator_did = "${MEDIATOR_DID:did://did:peer:2.Vz6MkiToqovww7vYtxm1xNM15u9JzqzU
mediator_secrets = "${MEDIATOR_SECRETS:file://./conf/secrets.json}"

[server]
### api_prefix: API prefix
### Default: /mediator/v1/
api_prefix = "/"

### http_size_limit: Maximum size of a http request payload in bytes
### Default: 10485760 (10MB)
### It is recommended to use infrastructure level limitation instead of application level limitations
Expand Down
5 changes: 5 additions & 0 deletions affinidi-messaging-mediator/src/common/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use tracing_subscriber::filter::LevelFilter;

#[derive(Debug, Serialize, Deserialize)]
pub struct ServerConfig {
pub api_prefix: String,
pub http_size_limit: String,
pub ws_size_limit: String,
}
Expand Down Expand Up @@ -115,6 +116,7 @@ pub struct Config {
pub database_url: String,
pub database_pool_size: usize,
pub database_timeout: u32,
pub api_prefix: String,
pub http_size_limit: u32,
pub ws_size_limit: u32,
pub max_message_size: u32,
Expand Down Expand Up @@ -161,6 +163,7 @@ impl fmt::Debug for Config {
.field("streaming_uuid", &self.streaming_uuid)
.field("DID Resolver config", &self.did_resolver_config)
.field("to_recipients_limit", &self.to_recipients_limit)
.field("api_prefix", &self.api_prefix)
.field("http_size_limit", &self.http_size_limit)
.field("ws_size_limit", &self.ws_size_limit)
.field(
Expand Down Expand Up @@ -206,6 +209,7 @@ impl Default for Config {
to_recipients_limit: 100,
cors_allow_origin: CorsLayer::new().allow_origin(Any),
ws_size_limit: 10485760,
api_prefix: "/mediator/v1/".into(),
http_size_limit: 10485760,
crypto_operations_per_message_limit: 1_000,
to_keys_per_recipient_limit: 100,
Expand Down Expand Up @@ -251,6 +255,7 @@ impl TryFrom<ConfigRaw> for Config {
streaming_enabled: raw.streaming.enabled.parse().unwrap_or(true),
did_resolver_config: raw.did_resolver.convert(),
to_recipients_limit: raw.other.to_recipients_limit.parse().unwrap_or(100),
api_prefix: raw.server.api_prefix,
http_size_limit: raw.server.http_size_limit.parse().unwrap_or(10485760),
ws_size_limit: raw.server.ws_size_limit.parse().unwrap_or(10485760),
crypto_operations_per_message_limit: raw
Expand Down
2 changes: 1 addition & 1 deletion affinidi-messaging-mediator/src/database/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl DatabaseHandler {
let function_load: Result<String, deadpool_redis::redis::RedisError> =
deadpool_redis::redis::cmd("FUNCTION")
.arg("LOAD")
.arg(LUA_SCRIPTS.clone())
.arg(LUA_SCRIPTS)
.query_async(&mut conn)
.await;
match function_load {
Expand Down
4 changes: 2 additions & 2 deletions affinidi-messaging-mediator/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod message_outbound;
pub mod websocket;
pub mod well_known_did_fetch;

pub fn application_routes(shared_data: &SharedData) -> Router {
pub fn application_routes(api_prefix: String, shared_data: &SharedData) -> Router {
let app = Router::new()
// Inbound message handling from ATM clients
.route("/inbound", post(message_inbound::message_inbound_handler))
Expand Down Expand Up @@ -47,7 +47,7 @@ pub fn application_routes(shared_data: &SharedData) -> Router {
);

Router::new()
.nest("/atm/v1/", app)
.nest(api_prefix.as_str(), app)
.with_state(shared_data.to_owned())
}

Expand Down
4 changes: 2 additions & 2 deletions affinidi-messaging-mediator/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub async fn start() {
};

// build our application routes
let app: Router = application_routes(&shared_state);
let app: Router = application_routes(config.api_prefix, &shared_state);

// Add middleware to all routes
let app = Router::new()
Expand All @@ -141,7 +141,7 @@ pub async fn start() {
.layer(RequestBodyLimitLayer::new(config.http_size_limit as usize))
// Add the healthcheck route after the tracing so we don't fill up logs with healthchecks
.route(
"/atm/healthchecker",
"/mediator/healthchecker",
get(health_checker_handler).with_state(shared_state),
);

Expand Down
30 changes: 23 additions & 7 deletions affinidi-messaging-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,32 @@ export RUST_LOG=none,affinidi_messaging_sdk=debug,ping=debug,demo=debug

# no "did://" prefix for examples
export MEDIATOR_DID=<MEDIATOR_DID>

cargo run --example message_pickup -- --mediator-did $MEDIATOR_DID
# default, local mediator endpoint
export MEDIATOR_ENDPOINT=https://localhost:7037/mediator/v1
# relative path to local mediator cert file
export MEDIATOR_SSL_CERTIFICATES="../affinidi-messaging-mediator/conf/keys/client.chain"

# send a trust ping
cargo run --example ping -- --mediator-did $MEDIATOR_DID
cargo run --example ping -- \
--network-address $MEDIATOR_ENDPOINT \
--ssl-certificates $MEDIATOR_SSL_CERTIFICATES \
--mediator-did $MEDIATOR_DID

# send message to another DID
cargo run --example send_message_to_me -- --mediator-did $MEDIATOR_DID
cargo run --example message_pickup -- \
--network-address $MEDIATOR_ENDPOINT \
--ssl-certificates $MEDIATOR_SSL_CERTIFICATES \
--mediator-did $MEDIATOR_DID

cargo run --example send_message -- --mediator-did $MEDIATOR_DID
# send message to another DID
cargo run --example send_message_to_me -- \
--network-address $MEDIATOR_ENDPOINT \
--ssl-certificates $MEDIATOR_SSL_CERTIFICATES \
--mediator-did $MEDIATOR_DID

cargo run --example send_message -- \
--network-address $MEDIATOR_ENDPOINT \
--ssl-certificates $MEDIATOR_SSL_CERTIFICATES \
--mediator-did $MEDIATOR_DID
```

## WebSocket and HTTPS support
Expand All @@ -50,7 +66,7 @@ A custom Websocket URL can be provided via `ConfigBuilder::with_atm_websocket_ap

**_NOTE:_** Default action is to take the `ConfigBuilder::with_atm_api()` and convert to a valid WebSocket address

E.g. `https://atm.affinidi.com/atm/v1` would become `wss://atm.affinidi.com/atm/v1/ws`
E.g. `https://localhost:7037/mediator/v1` would become `wss://localhost:7037/mediator/v1/ws`

While you can disable the WebSocket, you can also start and close the WebSocket manually via:

Expand Down
20 changes: 0 additions & 20 deletions affinidi-messaging-sdk/certs/mediator-key.pem

This file was deleted.

20 changes: 7 additions & 13 deletions affinidi-messaging-sdk/examples/message_pickup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use tracing_subscriber::filter;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// network address if running in network mode (ws://127.0.0.1:8080/did/v1/ws)
#[arg(short, long)]
network_address: Option<String>,
network_address: String,
#[arg(short, long)]
ssl_certificates: String,
#[arg(short, long)]
mediator_did: String,
}
Expand Down Expand Up @@ -52,17 +53,10 @@ async fn main() -> Result<(), ATMError> {

let mut config = Config::builder().with_my_did(my_did).with_atm_did(atm_did);

if let Some(address) = &args.network_address {
println!("Running in network mode with address: {}", address);
config = config
.with_ssl_certificates(&mut vec!["./certs/mediator-key.pem".into()])
.with_atm_api(address);
} else {
println!("Running in local mode.");
config = config.with_ssl_certificates(&mut vec![
"../affinidi-messaging-mediator/conf/keys/client.chain".into(),
]);
}
println!("Running with address: {}", &args.network_address);
config = config
.with_atm_api(&args.network_address)
.with_ssl_certificates(&mut vec![args.ssl_certificates.into()]);

// Create a new ATM Client
let mut atm = ATM::new(config.build()?).await?;
Expand Down
20 changes: 7 additions & 13 deletions affinidi-messaging-sdk/examples/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ use tracing_subscriber::filter;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// network address if running in network mode (https://localhost:7037/atm/v1)
#[arg(short, long)]
network_address: Option<String>,
network_address: String,
#[arg(short, long)]
ssl_certificates: String,
#[arg(short, long)]
mediator_did: String,
}
Expand Down Expand Up @@ -62,17 +63,10 @@ async fn main() -> Result<(), ATMError> {
.with_websocket_disabled()
.with_external_did_resolver(&did_resolver);

if let Some(address) = &args.network_address {
println!("Running in network mode with address: {}", address);
config = config
.with_ssl_certificates(&mut vec!["./certs/mediator-key.pem".into()])
.with_atm_api(address);
} else {
config = config.with_ssl_certificates(&mut vec![
"../affinidi-messaging-mediator/conf/keys/client.chain".into(),
]);
println!("Running in local mode.");
}
println!("Running with address: {}", &args.network_address);
config = config
.with_atm_api(&args.network_address)
.with_ssl_certificates(&mut vec![args.ssl_certificates.into()]);

// Create a new ATM Client
let mut atm = ATM::new(config.build()?).await?;
Expand Down
20 changes: 7 additions & 13 deletions affinidi-messaging-sdk/examples/send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ use uuid::Uuid;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// network address if running in network mode (https://localhost:7037/atm/v1)
#[arg(short, long)]
network_address: Option<String>,
network_address: String,
#[arg(short, long)]
ssl_certificates: String,
#[arg(short, long)]
mediator_did: String,
}
Expand Down Expand Up @@ -68,17 +69,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
.with_atm_did(atm_did)
.with_websocket_disabled();

if let Some(address) = &args.network_address {
println!("Running in network mode with address: {}", address);
config = config
.with_ssl_certificates(&mut vec!["./certs/mediator-key.pem".into()])
.with_atm_api(address);
} else {
println!("Running in local mode.");
config = config.with_ssl_certificates(&mut vec![
"../affinidi-messaging-mediator/conf/keys/client.chain".into(),
]);
}
println!("Running with address: {}", &args.network_address);
config = config
.with_atm_api(&args.network_address)
.with_ssl_certificates(&mut vec![args.ssl_certificates.into()]);

// Create a new ATM Client
let mut atm = ATM::new(config.build()?).await?;
Expand Down
20 changes: 7 additions & 13 deletions affinidi-messaging-sdk/examples/send_message_to_me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ use uuid::Uuid;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// network address if running in network mode (https://localhost:7037/atm/v1)
#[arg(short, long)]
network_address: Option<String>,
network_address: String,
#[arg(short, long)]
ssl_certificates: String,
#[arg(short, long)]
mediator_did: String,
}
Expand Down Expand Up @@ -68,17 +69,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
.with_atm_did(atm_did)
.with_websocket_disabled();

if let Some(address) = &args.network_address {
println!("Running in network mode with address: {}", address);
config = config
.with_ssl_certificates(&mut vec!["./certs/mediator-key.pem".into()])
.with_atm_api(address);
} else {
println!("Running in local mode.");
config = config.with_ssl_certificates(&mut vec![
"../affinidi-messaging-mediator/conf/keys/client.chain".into(),
]);
}
println!("Running with address: {}", &args.network_address);
config = config
.with_atm_api(&args.network_address)
.with_ssl_certificates(&mut vec![args.ssl_certificates.into()]);

// Create a new ATM Client
let mut atm = ATM::new(config.build()?).await?;
Expand Down
2 changes: 1 addition & 1 deletion affinidi-messaging-sdk/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl ConfigBuilder {
atm_url
} else {
// TODO: Change this to the production URL
"https://localhost:7037/atm/v1".to_string()
"https://localhost:7037/mediator/v1".to_string()
};

// convert the ATM API URL to a WebSocket URL
Expand Down
5 changes: 1 addition & 4 deletions affinidi-messaging-sdk/src/messages/well_known_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ impl<'c> ATM<'c> {
debug!("Sending well_known_did request");

let well_known_did_atm_api = format!("{}/.well-known/did", self.config.clone().atm_api);
debug!(
"API well_known_did_api({})",
format!("{}/.well-known/did", well_known_did_atm_api)
);
debug!("API well_known_did_api({})", well_known_did_atm_api);

let res = self
.client
Expand Down

0 comments on commit 438b8d3

Please sign in to comment.