Skip to content

Commit

Permalink
adjust plugin manager to load only if available in the configuration …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
thebino committed Jun 27, 2023
1 parent 4f9e8b1 commit 641ee3b
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 18 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ target/
.idea/

# data directory
data/
/data

# config directory
config/
/config

# logs directory
logs/
/logs
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Atomic commits will make it easier to track down regressions. Also, it enables t

## 🐛 How to report a bug

> If you find a security vulnerability, do NOT open an issue. Email [[email protected]] instead.
> If you find a security vulnerability, do NOT open an issue. Email [[email protected]](mailto:[email protected]) instead. See [SECURITY.md](./SECURITY.md) for details.
1. Open the [issues tab](https://github.com/photos-network/core/issues) on github
2. Click on [New issue](https://github.com/photos-network/core/issues/new/choose)
Expand Down
24 changes: 24 additions & 0 deletions crates/core_photos/src/data/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Photos.network · A privacy first photo storage and sharing service for fediverse.
* Copyright (C) 2020 Photos network developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

enum DataAccessError {
NotFound,
#[allow(dead_code)]
TechnicalError,
#[allow(dead_code)]
OtherError,
}
1 change: 1 addition & 0 deletions crates/core_photos/src/data/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod DataAccessError;
38 changes: 38 additions & 0 deletions crates/core_photos/src/data/photo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Photos.network · A privacy first photo storage and sharing service for fediverse.
* Copyright (C) 2020 Photos network developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use std::time::Instant;

#[derive(Default, Serialize)]
#[cfg_attr(test, derive(Deserialize, Eq, PartialEq, Debug, Copy, Clone, Default))]
pub struct Photo {
pub uuid: &'static str,
pub filename: &'static str,
pub date_added: Instant,
pub date_taken: Option<Instant>,
pub date_modified: Option<Instant>,
pub file_is_missing: bool,
pub owner: &'static str,
}

impl Photo {
fn new() -> Self {
Photo {
file_is_missing: true
}
}
}
Binary file modified plugins/libphotos_network_plugin.rlib
Binary file not shown.
Binary file modified plugins/libplugin_metadata.dylib
Binary file not shown.
33 changes: 33 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

use std::fs;

use serde::Deserialize;
use serde_json::Map;
use tracing::{debug, info};

#[derive(Debug, Deserialize, Clone)]
pub struct Configuration {
pub internal_url: String,
pub external_url: String,
pub plugins: Vec<Plugin>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct Plugin {
pub name: String,
pub config: Option<Map<String, serde_json::Value>>
}

impl Configuration {
pub fn new(path: &str) -> Option<Self> {
info!("Load configuration file {}", path);
let data = fs::read_to_string(path).expect("Unable to read configuration file!");
let config: Configuration = serde_json::from_str(&data).expect("Configuration file could not be parsed as JSON!");

debug!("internal: {}", config.internal_url);
debug!("external: {}", config.external_url);
debug!("plugins: {:?}", config.plugins);

Some(config)
}
}
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use photos_network_plugin::{PluginFactory_Ref, PluginId};
use serde::{Deserialize, Serialize};
use tower_http::services::ServeDir;
use tower_http::trace::TraceLayer;
use tracing::{error, info};
use tracing::{error, info, debug};
use tracing_subscriber::{fmt, layer::SubscriberExt};

use config::Configuration;
Expand Down Expand Up @@ -68,6 +68,7 @@ pub async fn start_server() -> Result<()> {

info!("Photos.network core is starting...");

// create mandatory application directories if necessary
fs::create_dir_all("data")?;
fs::create_dir_all("config")?;
fs::create_dir_all("plugins")?;
Expand All @@ -93,8 +94,8 @@ pub async fn start_server() -> Result<()> {
;
app_state.router = Some(router);

let mut plugin_manager =
PluginManager::new(config.clone(), PLUGIN_PATH.to_string(), &mut app_state)?;
// initialize plugin manager
let mut plugin_manager = PluginManager::new(config.clone(), PLUGIN_PATH.to_string(), &mut app_state)?;

match plugin_manager.init().await {
Ok(_) => info!("PluginManager: initialization succed."),
Expand All @@ -104,7 +105,7 @@ pub async fn start_server() -> Result<()> {

// trigger `on_core_init` on all loaded plugins
for (plugin_id, factory) in app_state.plugins {
info!("plugin {} found in AppState.", plugin_id);
info!("Plugin '{}' found in AppState.", plugin_id);

let plugin_constructor = factory.new();

Expand Down
22 changes: 12 additions & 10 deletions src/plugin/plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ impl<'a> PluginManager<'a> {

for configured_plugin in &self.config.plugins {
info!(
"Addon '{}' found in the configuration",
configured_plugin.plugin_domain()
"Plugin '{}' found in the configuration file.",
configured_plugin.name
);

let base_name = configured_plugin.plugin_domain().clone();
let mut base_name = String::from("plugin_").to_owned();
let plugin_name = configured_plugin.name.to_lowercase().to_owned();
base_name.push_str(&plugin_name);
let plugin_dir: PathBuf = self.path.clone().into_::<PathBuf>();

let plugin_path: PathBuf =
RawLibrary::path_in_directory(&plugin_dir, &base_name, LibrarySuffix::NoSuffix);

if plugin_path.exists() {
debug!(
"Addon '{}' also found in the `plugins` directory",
configured_plugin.plugin_domain()
);
debug!("Plugin '{}' also found in the `plugins` directory", plugin_name);

debug!("Try to load plugin...");
debug!("Try to load plugin '{}'...", plugin_name);
let header = lib_header_from_path(&plugin_path)?;
let res = header.init_root_module::<PluginFactory_Ref>();

Expand All @@ -64,10 +64,12 @@ impl<'a> PluginManager<'a> {
};

let mut loaded_libraries = Vec::<PluginId>::new();
loaded_libraries.push(PluginId::from(base_name.clone()));
loaded_libraries.push(PluginId::from(plugin_name.clone()));

// TODO: insert loaded plugin instead?
self.state
.plugins
.insert(PluginId::from(base_name), root_module);
.insert(PluginId::from(plugin_name), root_module);
}
}

Expand Down

0 comments on commit 641ee3b

Please sign in to comment.