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 cd ../server/ui
yarn yarn
yarn build yarn build
# Build back end
cd ../service cd ../service
cargo run --release cargo run --release

View File

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

View File

@ -4,6 +4,8 @@
"description": "A simple UI for Torrents.csv", "description": "A simple UI for Torrents.csv",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"prestart": "node set_env.js",
"prebuild": "node set_env.js",
"start": "node fuse dev", "start": "node fuse dev",
"build": "node fuse prod" "build": "node fuse prod"
}, },
@ -16,6 +18,7 @@
"engineStrict": true, "engineStrict": true,
"dependencies": { "dependencies": {
"classcat": "^1.1.3", "classcat": "^1.1.3",
"dotenv": "^6.1.0",
"inferno": "^6.3.1", "inferno": "^6.3.1",
"inferno-router": "^6.3.1", "inferno-router": "^6.3.1",
"moment": "^2.22.2" "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" resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"
integrity sha1-+hN8S9aY7fVc1c0CrFWfkaTEups= 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: ecc-jsbn@~0.1.1:
version "0.1.2" version "0.1.2"
resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz#3a83a904e54353287874c564b7549386849a98c9" resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz#3a83a904e54353287874c564b7549386849a98c9"