use lazy_static for config

This commit is contained in:
Peter Cai 2020-04-07 18:43:49 +08:00
parent 7c102af848
commit a2fa677514
No known key found for this signature in database
GPG key ID: 71F5FB4E4F3FD54F
3 changed files with 14 additions and 14 deletions

View file

@ -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 {

View file

@ -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<Response> {
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<Response> {
});
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

View file

@ -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()
}