Skip to content

Commit

Permalink
feat(all): register plugins endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed Sep 29, 2023
1 parent a00addb commit 6dae7d4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
4 changes: 3 additions & 1 deletion backend/src/plugins/minecraft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ struct McData {

static mut ACCESS_ADDRESS: String = String::new();

pub fn register(app: &mut tide::Server<State>, access_address: String) {
pub fn register(app: &mut tide::Server<State>, access_address: String) -> Vec<String> {
println!("Enabling Minecraft plugin");

unsafe {
ACCESS_ADDRESS = access_address;
}

app.at("/api/minecraft").get(minecraft);

vec![String::from("/api/minecraft")]
}

pub async fn minecraft(_req: tide::Request<State>) -> Result<tide::Response, tide::Error> {
Expand Down
40 changes: 38 additions & 2 deletions backend/src/plugins/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
use serde::Serialize;
use crate::State;

mod minecraft;

#[derive(Serialize, Clone)]
struct Plugin {
name: String,
endpoints: Vec<String>,
}

static mut PLUGINS: Vec<Plugin> = vec![];

pub fn parse_enable_plugins(app: &mut tide::Server<State>, plugins: Option<String>, address: String) {
if plugins.is_none() {
return;
Expand All @@ -14,11 +23,38 @@ pub fn parse_enable_plugins(app: &mut tide::Server<State>, plugins: Option<Strin
}

let plugins = plugins_string.split(",").collect::<Vec<&str>>();
let mut registered_endpoints = vec![];

for plugin in plugins {
match plugin {
let endpoints = match plugin {
"minecraft" => minecraft::register(app, address.clone()),
_ => {}
_ => {vec![]}
};

for endpoint in endpoints {
println!("Registered endpoint: {}", endpoint);
registered_endpoints.push(Plugin {
name: plugin.to_string(),
endpoints: vec![endpoint],
});
}
}

unsafe {
PLUGINS = registered_endpoints;
}

app.at("/api/plugins").get(plugins_route);
}

async fn plugins_route(_req: tide::Request<State>) -> Result<tide::Response, tide::Error> {
let mut res = tide::Response::new(200);

unsafe {
res.set_body(serde_json::to_vec(&PLUGINS).unwrap());
}

res.set_content_type("application/json");

Ok(res)
}

0 comments on commit 6dae7d4

Please sign in to comment.