use lazy_static for config
This commit is contained in:
parent
7c102af848
commit
a2fa677514
|
@ -25,6 +25,10 @@ lazy_static! {
|
||||||
static ref ROUTER: router::Router = {
|
static ref ROUTER: router::Router = {
|
||||||
build_routes()
|
build_routes()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub static ref CONFIG: utils::Config = {
|
||||||
|
serde_json::from_str(std::include_str!("../config.json")).unwrap()
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_routes() -> router::Router {
|
fn build_routes() -> router::Router {
|
||||||
|
|
20
src/sn.rs
20
src/sn.rs
|
@ -1,6 +1,7 @@
|
||||||
// Interface for Standard Notes (Actions)
|
// Interface for Standard Notes (Actions)
|
||||||
|
use crate::CONFIG;
|
||||||
use crate::router::Router;
|
use crate::router::Router;
|
||||||
use crate::utils::{self, Error, MyResult};
|
use crate::utils::{Error, MyResult};
|
||||||
use serde::{Serialize, Serializer};
|
use serde::{Serialize, Serializer};
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
use web_sys::*;
|
use web_sys::*;
|
||||||
|
@ -10,27 +11,26 @@ pub fn build_routes(router: &mut Router) {
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! verify_secret {
|
macro_rules! verify_secret {
|
||||||
($url:expr, $params:ident, $config:ident) => {
|
($url:expr, $params:ident) => {
|
||||||
let $config = utils::get_config();
|
|
||||||
let $params = UrlSearchParams::new_with_str(&$url.search())
|
let $params = UrlSearchParams::new_with_str(&$url.search())
|
||||||
.map_err(|_| Error::BadRequest("Failed to parse query string".into()))?;
|
.map_err(|_| Error::BadRequest("Failed to parse query string".into()))?;
|
||||||
if !$params.has("secret") {
|
if !$params.has("secret") {
|
||||||
return Err(Error::BadRequest("Secret needed".into()));
|
return Err(Error::BadRequest("Secret needed".into()));
|
||||||
} else if $params.get("secret").unwrap() != $config.secret {
|
} else if $params.get("secret").unwrap() != crate::CONFIG.secret {
|
||||||
return Err(Error::Unauthorized("Secret mismatch".into()));
|
return Err(Error::Unauthorized("Secret mismatch".into()));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_actions(_req: Request, url: Url) -> MyResult<Response> {
|
async fn get_actions(_req: Request, url: Url) -> MyResult<Response> {
|
||||||
verify_secret!(url, params, config);
|
verify_secret!(url, params);
|
||||||
|
|
||||||
let origin = url.origin();
|
let origin = url.origin();
|
||||||
let mut actions = vec![];
|
let mut actions = vec![];
|
||||||
|
|
||||||
actions.push(Action {
|
actions.push(Action {
|
||||||
label: "Publish".into(),
|
label: "Publish".into(),
|
||||||
url: format!("{}/post?secret={}", origin, config.secret.clone()),
|
url: format!("{}/post?secret={}", origin, CONFIG.secret.clone()),
|
||||||
verb: Verb::Post,
|
verb: Verb::Post,
|
||||||
context: Context::Item,
|
context: Context::Item,
|
||||||
content_types: vec![ContentType::Note],
|
content_types: vec![ContentType::Note],
|
||||||
|
@ -38,10 +38,10 @@ async fn get_actions(_req: Request, url: Url) -> MyResult<Response> {
|
||||||
});
|
});
|
||||||
|
|
||||||
let info = ActionsExtension {
|
let info = ActionsExtension {
|
||||||
identifier: config.plugin_identifier.clone(),
|
identifier: CONFIG.plugin_identifier.clone(),
|
||||||
name: config.title.clone(),
|
name: CONFIG.title.clone(),
|
||||||
description: format!("Standard Notes plugin for {}", config.title.clone()),
|
description: format!("Standard Notes plugin for {}", CONFIG.title.clone()),
|
||||||
url: format!("{}/actions?secret={}", origin, config.secret.clone()),
|
url: format!("{}/actions?secret={}", origin, CONFIG.secret.clone()),
|
||||||
content_type: ContentType::Extension,
|
content_type: ContentType::Extension,
|
||||||
supported_types: vec![ContentType::Note],
|
supported_types: vec![ContentType::Note],
|
||||||
actions
|
actions
|
||||||
|
|
|
@ -72,8 +72,4 @@ pub struct Config {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
// Plugin identifier used for Standard Notes
|
// Plugin identifier used for Standard Notes
|
||||||
pub plugin_identifier: String
|
pub plugin_identifier: String
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_config() -> Config {
|
|
||||||
serde_json::from_str(std::include_str!("../config.json")).unwrap()
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue