Skip to content

Commit

Permalink
feat: support per-module configuration
Browse files Browse the repository at this point in the history
Issue: bnjbvr#26
  • Loading branch information
ahal committed Feb 11, 2023
1 parent 069c6d4 commit 96edc7d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use matrix_sdk::{
use notify::{RecursiveMode, Watcher};
use room_resolver::RoomResolver;
use serde::Deserialize;
use std::{env, fs, path::PathBuf, sync::Arc};
use std::{collections::HashMap, env, fs, path::PathBuf, sync::Arc};
use tokio::{
sync::Mutex,
time::{sleep, Duration},
Expand All @@ -43,6 +43,8 @@ pub struct BotConfig {
pub redb_path: String,
/// the admin user id for the bot.
pub admin_user_id: OwnedUserId,
/// module specific configuration to forward to corresponding handler.
pub modules: Option<HashMap<String, HashMap<String, String>>>,
}

impl BotConfig {
Expand Down Expand Up @@ -98,6 +100,7 @@ impl BotConfig {
matrix_store_path,
admin_user_id,
redb_path,
modules: None,
})
}
}
Expand All @@ -108,6 +111,7 @@ pub(crate) type ShareableDatabase = Arc<redb::Database>;
struct AppCtx {
modules: WasmModules,
modules_path: PathBuf,
modules_config: HashMap<String, HashMap<String, String>>,
needs_recompile: bool,
admin_user_id: OwnedUserId,
db: ShareableDatabase,
Expand All @@ -121,13 +125,15 @@ impl AppCtx {
pub fn new(
client: Client,
modules_path: PathBuf,
modules_config: HashMap<String, HashMap<String, String>>,
db: ShareableDatabase,
admin_user_id: OwnedUserId,
) -> anyhow::Result<Self> {
let room_resolver = RoomResolver::new(client);
Ok(Self {
modules: WasmModules::new(db.clone(), &modules_path)?,
modules: WasmModules::new(db.clone(), &modules_path, &modules_config)?,
modules_path,
modules_config,
needs_recompile: false,
admin_user_id,
db,
Expand All @@ -150,7 +156,7 @@ impl AppCtx {
ptr.lock().await
});

match WasmModules::new(ptr.db.clone(), &ptr.modules_path) {
match WasmModules::new(ptr.db.clone(), &ptr.modules_path, &ptr.modules_config) {
Ok(modules) => {
ptr.modules = modules;
tracing::info!("successful hot reload!");
Expand Down Expand Up @@ -468,6 +474,8 @@ pub async fn run(config: BotConfig) -> anyhow::Result<()> {
.context("writing new device_id into the database")?;
}

let modules_config = config.modules.unwrap_or(HashMap::new());

client
.user_id()
.context("impossible state: missing user id for the logged in bot?")?;
Expand All @@ -484,6 +492,7 @@ pub async fn run(config: BotConfig) -> anyhow::Result<()> {
AppCtx::new(
client_copy,
"./modules/target/wasm32-unknown-unknown/release/".into(),
modules_config,
db,
config.admin_user_id,
)
Expand Down
11 changes: 9 additions & 2 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod module {

mod apis;

use std::path::Path;
use std::{collections::HashMap, path::Path};

use matrix_sdk::ruma::{RoomId, UserId};
use wasmtime::AsContextMut;
Expand All @@ -25,6 +25,7 @@ pub(crate) struct GuestState {

pub(crate) struct Module {
name: String,
config: Option<HashMap<String, String>>,
exports: module::Interface,
_instance: wasmtime::component::Instance,
}
Expand Down Expand Up @@ -81,7 +82,11 @@ impl WasmModules {
/// Create a new collection of wasm modules.
///
/// Must be called from a blocking context.
pub fn new(db: ShareableDatabase, modules_path: &Path) -> anyhow::Result<Self> {
pub fn new(
db: ShareableDatabase,
modules_path: &Path,
modules_config: &HashMap<String, HashMap<String, String>>,
) -> anyhow::Result<Self> {
tracing::debug!("setting up wasm context...");

let mut config = wasmtime::Config::new();
Expand Down Expand Up @@ -136,9 +141,11 @@ impl WasmModules {
tracing::debug!("calling module's init function...");
exports.init(&mut store)?;

let module_config = modules_config.get(&name).cloned();
tracing::debug!("great success!");
compiled_modules.push(Module {
name,
config: module_config,
exports,
_instance: instance,
});
Expand Down

0 comments on commit 96edc7d

Please sign in to comment.