Using environment variables for resource locations. Fixes #24.

This commit is contained in:
Dessalines 2018-12-01 22:15:54 -07:00
parent b4a6b3779a
commit 21016e9429
5 changed files with 34 additions and 7 deletions

View File

@ -1,5 +1,14 @@
# Optionally use environment variables
# export TORRENTS_CSV_ENDPOINT=http://0.0.0.0:8080
# export TORRENTS_CSV_FILE=`pwd`/../../torrents.csv
# export TORRENTS_CSV_FRONT_END_DIR=`pwd`/../ui/dist
# Build front end
cd ../server/ui
yarn
yarn build
# Build back end
cd ../service
cargo run --release

View File

@ -12,10 +12,12 @@ use std::error::Error;
use grep::regex::RegexMatcherBuilder;
use grep::searcher::sinks::Lossy;
use grep::searcher::{BinaryDetection, SearcherBuilder};
use std::env;
use std::fs::File;
fn main() {
server::new(|| {
App::new()
.route("/service/search", http::Method::GET, search)
@ -23,7 +25,7 @@ fn main() {
.resource("/", |r| r.f(index))
.handler(
"/static",
fs::StaticFiles::new("../ui/dist/")
fs::StaticFiles::new(front_end_dir())
.unwrap()
// .index_file("index.html"),
)
@ -34,11 +36,19 @@ fn main() {
}
fn index(_req: &HttpRequest) -> Result<NamedFile, actix_web::error::Error> {
Ok(NamedFile::open("../ui/dist/index.html")?)
Ok(NamedFile::open(front_end_dir() + "/index.html")?)
}
fn favicon(_req: &HttpRequest) -> Result<NamedFile, actix_web::error::Error> {
Ok(NamedFile::open("../ui/src/favicon.ico")?)
Ok(NamedFile::open(front_end_dir() + "/favicon.ico")?)
}
fn front_end_dir() -> String {
env::var("TORRENTS_CSV_FRONT_END_DIR").unwrap_or("../ui/dist".to_string())
}
fn torrents_csv_file() -> String {
env::var("TORRENTS_CSV_FILE").unwrap_or("../../torrents.csv".to_string())
}
#[derive(Deserialize)]
@ -59,7 +69,7 @@ fn ripgrep(query: Query<SearchQuery>) -> String {
let page = query.page.unwrap_or(1);
let size = query.size.unwrap_or(10);
let offset = size * (page - 1);
let csv_file = File::open("../../torrents.csv");
let csv_file = File::open(torrents_csv_file());
println!("query = {} , page = {}, size = {}", query.q, page, size);
@ -110,7 +120,7 @@ mod tests {
fn test() {
let start = PreciseTime::now();
let results =
super::search_file(File::open("../../torrents.csv").unwrap(), "sherlock").unwrap();
super::search_file(File::open(super::torrents_csv_file()).unwrap(), "sherlock").unwrap();
assert!(results.len() > 2);
let end = PreciseTime::now();
println!("Query took {} seconds.", start.to(end));

View File

@ -4,6 +4,8 @@
"description": "A simple UI for Torrents.csv",
"main": "index.js",
"scripts": {
"prestart": "node set_env.js",
"prebuild": "node set_env.js",
"start": "node fuse dev",
"build": "node fuse prod"
},
@ -16,6 +18,7 @@
"engineStrict": true,
"dependencies": {
"classcat": "^1.1.3",
"dotenv": "^6.1.0",
"inferno": "^6.3.1",
"inferno-router": "^6.3.1",
"moment": "^2.22.2"

View File

@ -612,6 +612,11 @@ detect-libc@^1.0.2:
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"
integrity sha1-+hN8S9aY7fVc1c0CrFWfkaTEups=
dotenv@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.1.0.tgz#9853b6ca98292acb7dec67a95018fa40bccff42c"
integrity sha512-/veDn2ztgRlB7gKmE3i9f6CmDIyXAy6d5nBq+whO9SLX+Zs1sXEgFLPi+aSuWqUuusMfbi84fT8j34fs1HaYUw==
ecc-jsbn@~0.1.1:
version "0.1.2"
resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz#3a83a904e54353287874c564b7549386849a98c9"