~shockham/mockd

379d2a8823207f8c6cb3c08c683dddab5f894ba7 — shockham 3 years ago 87fbf66
Add a env var configurable mock directory defaulting to ./mocks
1 files changed, 6 insertions(+), 3 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +6 -3
@@ 4,17 4,20 @@ use graphql_parser::query::{
    OperationDefinition::*,
};
use std::collections::HashMap;
use std::env;
use std::fs;
use warp::Filter;

const MOCK_DIR: &'static str = "./mocks";
fn get_mock_dir() -> String {
    env::var("MOCKD_DIR").unwrap_or("./mocks".into())
}

#[tokio::main]
async fn main() {
    let hello = warp::path::end().map(|| "Mockd");

    let rest = warp::path!("rest" / String).map(|endpoint| {
        let file_path = format!("{}/rest/{}.json", MOCK_DIR, endpoint);
        let file_path = format!("{}/rest/{}.json", get_mock_dir(), endpoint);
        let file = fs::read_to_string(file_path).unwrap_or("".into());
        warp::reply::json(&file)
    });


@@ 39,7 42,7 @@ async fn main() {
                    Fragment(f) => f.name,
                })
                .map(|q| {
                    let file_path = format!("{}/gql/{}.json", MOCK_DIR, q);
                    let file_path = format!("{}/gql/{}.json", get_mock_dir(), q);
                    let file = fs::read_to_string(file_path).unwrap_or("".into());
                    if file.is_empty() {
                        return "".into();