From a2fa677514eb0356e0cd1c3a86998657e4d29218 Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Tue, 7 Apr 2020 18:43:49 +0800 Subject: [PATCH] use lazy_static for config --- src/lib.rs | 4 ++++ src/sn.rs | 20 ++++++++++---------- src/utils.rs | 4 ---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1c19174..483125c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,10 @@ lazy_static! { static ref ROUTER: router::Router = { build_routes() }; + + pub static ref CONFIG: utils::Config = { + serde_json::from_str(std::include_str!("../config.json")).unwrap() + }; } fn build_routes() -> router::Router { diff --git a/src/sn.rs b/src/sn.rs index 9b74b43..03cc476 100644 --- a/src/sn.rs +++ b/src/sn.rs @@ -1,6 +1,7 @@ // Interface for Standard Notes (Actions) +use crate::CONFIG; use crate::router::Router; -use crate::utils::{self, Error, MyResult}; +use crate::utils::{Error, MyResult}; use serde::{Serialize, Serializer}; use std::vec::Vec; use web_sys::*; @@ -10,27 +11,26 @@ pub fn build_routes(router: &mut Router) { } macro_rules! verify_secret { - ($url:expr, $params:ident, $config:ident) => { - let $config = utils::get_config(); + ($url:expr, $params:ident) => { let $params = UrlSearchParams::new_with_str(&$url.search()) .map_err(|_| Error::BadRequest("Failed to parse query string".into()))?; if !$params.has("secret") { 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())); } }; } async fn get_actions(_req: Request, url: Url) -> MyResult { - verify_secret!(url, params, config); + verify_secret!(url, params); let origin = url.origin(); let mut actions = vec![]; actions.push(Action { label: "Publish".into(), - url: format!("{}/post?secret={}", origin, config.secret.clone()), + url: format!("{}/post?secret={}", origin, CONFIG.secret.clone()), verb: Verb::Post, context: Context::Item, content_types: vec![ContentType::Note], @@ -38,10 +38,10 @@ async fn get_actions(_req: Request, url: Url) -> MyResult { }); let info = ActionsExtension { - identifier: config.plugin_identifier.clone(), - name: config.title.clone(), - description: format!("Standard Notes plugin for {}", config.title.clone()), - url: format!("{}/actions?secret={}", origin, config.secret.clone()), + identifier: CONFIG.plugin_identifier.clone(), + name: CONFIG.title.clone(), + description: format!("Standard Notes plugin for {}", CONFIG.title.clone()), + url: format!("{}/actions?secret={}", origin, CONFIG.secret.clone()), content_type: ContentType::Extension, supported_types: vec![ContentType::Note], actions diff --git a/src/utils.rs b/src/utils.rs index c4978da..ed59f34 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -72,8 +72,4 @@ pub struct Config { pub title: String, // Plugin identifier used for Standard Notes pub plugin_identifier: String -} - -pub fn get_config() -> Config { - serde_json::from_str(std::include_str!("../config.json")).unwrap() } \ No newline at end of file