Skip to content

Commit

Permalink
feature: making paths configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Nov 25, 2018
1 parent b28f15d commit abf631d
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/lib/preset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ pub trait Preset<T> {
/// The following are just aliases
///
pub type RewriteFns = Vec<fn(&str, &RewriteContext) -> String>;
pub type ResourceDef<'a> = (&'a str, Method, fn(&HttpRequest<AppState>) -> HttpResponse);
pub type AsyncResourceDef<'a> = (&'a str, Method, fn(&HttpRequest<AppState>) -> FutResp);
pub type ResourceDef = (String, Method, fn(&HttpRequest<AppState>) -> HttpResponse);
pub type AsyncResourceDef = (String, Method, fn(&HttpRequest<AppState>) -> FutResp);
8 changes: 8 additions & 0 deletions src/lib/presets/m2/handlers/config_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use app_state::AppState;
use presets::m2::preset_m2::FutResp;
use proxy_utils::apply_to_proxy_body;
use rjs::RequireJsClientConfig;
use actix_web::http::Method;
use preset::ResourceDef;
use preset::AsyncResourceDef;

///
/// This handler will record the incoming string from the Magento-generated
Expand All @@ -26,3 +29,8 @@ pub fn handle(original_request: &HttpRequest<AppState>) -> FutResp {
b
})
}

pub fn register(path: Option<String>) -> AsyncResourceDef {
let p = path.unwrap();
(p.clone(), Method::GET, handle)
}
32 changes: 0 additions & 32 deletions src/lib/presets/m2/handlers/config_post.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/lib/presets/m2/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod build;
pub mod config;
pub mod config_capture;
pub mod config_post;
pub mod err_response;
pub mod loaders;
pub mod req_capture;
Expand Down
8 changes: 8 additions & 0 deletions src/lib/presets/m2/handlers/serve_r_js.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use actix_web::HttpRequest;
use actix_web::HttpResponse;
use app_state::AppState;
use presets::m2::opts::M2PresetOptions;
use actix_web::http::Method;
use preset::ResourceDef;

const INSTRUMENTED_REQUIRE_JS: &'static str = include_str!("../static/requirejs.js");

Expand All @@ -13,3 +16,8 @@ pub fn handle(_req: &HttpRequest<AppState>) -> HttpResponse {
.content_type("application/javascript")
.body(INSTRUMENTED_REQUIRE_JS)
}

pub fn register(path: Option<String>) -> ResourceDef {
let p = path.unwrap();
(p.clone(), Method::GET, handle)
}
10 changes: 10 additions & 0 deletions src/lib/presets/m2/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ use serde_json;

#[derive(Deserialize, Debug)]
pub struct M2PresetOptions {

#[serde(default = "default_require_path")]
pub require_path: Option<String>,

#[serde(default = "default_require_conf_path")]
pub require_conf_path: Option<String>,

pub bundle_config: Option<String>,
pub auth_basic: Option<AuthBasic>,
pub module_blacklist: Option<Vec<String>>,
Expand All @@ -14,10 +19,15 @@ fn default_require_path() -> Option<String> {
Some("/static/{version}/frontend/{vendor}/{theme}/{locale}/requirejs/require.js".into())
}

fn default_require_conf_path() -> Option<String> {
Some("/static/{version}/frontend/{vendor}/{theme}/{locale}/requirejs-config.js".into())
}

impl Default for M2PresetOptions {
fn default() -> Self {
M2PresetOptions {
require_path: None,
require_conf_path: None,
bundle_config: None,
auth_basic: None,
module_blacklist: None,
Expand Down
25 changes: 7 additions & 18 deletions src/lib/presets/m2/preset_m2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ impl M2Preset {
}
}

const PATH_REQUIRE_JS: &'static str =
"/static/{version}/frontend/{vendor}/{theme}/{locale}/requirejs/require.js";
const PATH_REQUIRE_CNF: &'static str =
"/static/{version}/frontend/{vendor}/{theme}/{locale}/requirejs-config.js";
const PATH_CONF_POST: &'static str = "/__bs/post";

///
/// The M2Preset adds some middleware, resources and
/// rewrites
Expand All @@ -52,25 +46,20 @@ impl Preset<AppState> for M2Preset {
// which is suitable for most routes.
//
let http_responders: Vec<ResourceDef> = vec![
(PATH_REQUIRE_JS, Method::GET, handlers::serve_r_js::handle),
("/__bs/reqs.json", Method::GET, handlers::requests::handle),
("/__bs/config.json", Method::GET, handlers::config::handle),
("/__bs/build.json", Method::GET, handlers::build::handle),
("/__bs/loaders.js", Method::GET, handlers::loaders::handle),
("/__bs/seed.json", Method::GET, handlers::seed::handle),
handlers::serve_r_js::register(self.options.require_path.clone()),
("/__bs/reqs.json".to_string(), Method::GET, handlers::requests::handle),
("/__bs/config.json".to_string(), Method::GET, handlers::config::handle),
("/__bs/build.json".to_string(), Method::GET, handlers::build::handle),
("/__bs/loaders.js".to_string(), Method::GET, handlers::loaders::handle),
("/__bs/seed.json".to_string(), Method::GET, handlers::seed::handle),
];

//
// Async Responders are needed when there's additional
// work to be done in a handler.
//
let http_async_responders: Vec<AsyncResourceDef> = vec![
(PATH_CONF_POST, Method::POST, handlers::config_post::handle),
(
PATH_REQUIRE_CNF,
Method::GET,
handlers::config_capture::handle,
),
handlers::config_capture::register(self.options.require_conf_path.clone())
];

let app = http_responders
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"name": "m2",
"options": {
"bundle_config": "file:test/fixtures/bundle-config.json",
"require_path": "/static/{version}/frontend/{vendor}/{theme}/{locale}/requirejs/require.js",
"auth_basic": {
"username": "acme",
"password": "acmepw"
Expand Down
34 changes: 0 additions & 34 deletions tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,40 +109,6 @@ fn test_build_json_without_config() {
});
}

#[test]
fn test_capture_requirejs() {
let args = vec!["config-gen", "http://example.com"];
let path = "/__bs/post";
let get = "/__bs/build.json";
run_with_args(args, move |result: RunResult| {
let (_sys, url) = result.expect("system started");
let api1 = format!("{}{}", url, path);
let client = reqwest::Client::new();

client
.post(&api1)
.body(include_str!(
"../test/fixtures/requirejs-config-generated.js"
)).send()
.expect("POST sent");

let api2 = format!("{}{}", url, get);
let mut res2 = reqwest::get(api2.as_str()).expect("call build.json api endpoint");
let actual: RequireJsBuildConfig =
serde_json::from_str(&res2.text().expect("res.text")).expect("serde_unwrap");
let expected: RequireJsBuildConfig =
serde_json::from_str(include_str!("../test/fixtures/rjs-config-expected.json"))
.expect("serde expected");

assert_eq!(actual.deps, expected.deps);
assert_eq!(actual.paths, expected.paths);
assert_eq!(actual.map, expected.map);
assert_eq!(actual.modules, expected.modules);
assert_eq!(actual.optimize, expected.optimize);
assert_eq!(actual.shim, expected.shim);
});
}

#[test]
fn test_build_json_with_seed_without_config() {
let args = vec![
Expand Down

0 comments on commit abf631d

Please sign in to comment.