Skip to content

Commit 8acdc5c

Browse files
committed
Update config methods
1 parent 092ae0e commit 8acdc5c

21 files changed

Lines changed: 150 additions & 75 deletions

README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,64 @@ And the page at `http://localhost:8999/` will point to these two URLs.
251251
<a name="Server_Configuration"></a>
252252
## 4. Server Configuration
253253

254-
### Apache
254+
### 4.1 `conf/config.json`
255+
256+
The variables `$HAPISERVERPATH`, `HAPISERVERHOME`, `$NODEEXE`, and `$PYTHONEXE` can be set in `conf/config.json` or as environment variables. These variables can be used in commands, files, and URLs in the server metadata.
257+
258+
The default configuration file is `conf/config.json` and this location changed using a command line argument, e.g.,
259+
260+
```
261+
./hapiserver -c /tmp/config.json
262+
```
263+
264+
To set variables using environment variables, use, e.g.,
265+
266+
```
267+
PYTHONEXE=/opt/python/bin/python ./hapiserver
268+
```
269+
270+
Variables set as environment variable take precendence over those set in `conf/config.json`.
271+
272+
**`$HAPISERVERPATH`** and **`$HAPISERVERHOME`**
273+
274+
These two variables can be used in metadata to reference a directory. For example,
275+
276+
```
277+
"catalog": "$HAPISERVERHOME/mymetadata/Data.json"
278+
```
279+
280+
By default, `$HAPISERVERPATH` is the installation directory (the directory containing the shell launch script `hapi-server`) and should not be changed as it is referenced in the demonstration metadata files. Modify `$HAPISERVERHOME` in `conf/config.json` to use a custom path.
281+
282+
All relative paths in commands in metadata files are relative to the directory where `hapi-server` was executed.
283+
284+
For example, if
285+
286+
```
287+
/tmp/hapi-server-v0.9.2/hapi-server -f metadata/TestData.json
288+
```
289+
290+
is executed from `/home/username`, the file
291+
292+
```
293+
/home/username/metadata/TestData.json`
294+
```
295+
296+
is read and relative paths in `TestData.json` have `/home/username/` prepended.
297+
298+
**`$PYTHONEXE`**
299+
300+
This is the command used to call Python. By default, it is `python`. If `python` is not in the path, this can be set using a relative or absolute path. Python is used by several of the demonstration catalogs.
301+
302+
```
303+
"command": "$PYTHONEXE $HAPISERVERHOME/mybin/Data.py"
304+
```
305+
306+
**`$NODEEXE`**
307+
308+
This is the command used to call NodeJS. By default, it is the command used to start the server. The start-up script looks for a NodeJS executable in `$HAPISERVERPATH/bin` and then tries `node` and then `nodejs`.
309+
310+
311+
### 4.2 Apache
255312

256313
To expose a URL through Apache, (1) enable `mod_proxy` and `mod_proxy_http`, (2) add the following in a `<VirtualHost>` node in a [Apache Virtual Hosts](https://httpd.apache.org/docs/2.4/vhosts/examples.html) file
257314

@@ -273,7 +330,7 @@ If serving multiple catalogs, use
273330
</VirtualHost>
274331
```
275332

276-
## Nginx
333+
### 4.3 Nginx
277334

278335
For Nginx, add the following to `nginx.conf`
279336

conf/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ WORKDIR /app
33
COPY package.json /app
44
RUN npm install
55
COPY . /app
6-
CMD node index.js
7-
EXPOSE 8081
6+
CMD node server.js
7+
EXPOSE 8999

conf/server.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"HAPISERVER_BIN_DIR":"__ absolute path",
3-
"HAPISERVER_META_DIR":"__ absolute path",
4-
"NODE_EXE":"__ enter shell command (e.g., 'node') or absolute path",
5-
"PYTHON_EXE":"__ enter shell command (e.g., 'python2.7') or absolute path"
2+
"HAPISERVERPATH": "",
3+
"HAPISERVERHOME": "",
4+
"NODE_EXE": "",
5+
"PYTHON_EXE": ""
66
}

lib/metadata.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,48 @@ var is = require('hapi-server-verifier').is;
1515

1616
function ds() {return (new Date()).toISOString() + " [server] ";};
1717

18-
function configVars() {
18+
function configVars(configfile) {
1919

2020
if (configVars.config) {
2121
return configVars.config;
2222
}
2323

24-
configd = {}; // Default configuration
25-
configd["HAPISERVER_BIN_DIR"] = path.normalize(__dirname + "/../bin");
26-
configd["HAPISERVER_META_DIR"] = path.normalize(__dirname + "/../metadata");
27-
configd["NODE_EXE"] = process.execPath;
24+
var env = process.env;
25+
26+
configd = {};
27+
// Set defaults
28+
configd["HAPISERVERPATH"] = path.normalize(__dirname + "/..");
29+
configd["HAPISERVERHOME"] = configd["HAPISERVERPATH"];
30+
configd["NODEEXE"] = process.execPath;
2831
if (commandExistsSync("python")) {
29-
configd["PYTHON_EXE"] = "python";
32+
configd["PYTHONEXE"] = "python";
3033
} else {
31-
configd["PYTHON_EXE"] = path.normalize(__dirname + "/../bin/python");
34+
configd["PYTHONEXE"] = path.normalize(__dirname + "/../bin/python");
3235
}
33-
var configfile = path.normalize(__dirname + "/../conf/server.json");
36+
37+
// Override defaults using configuration file variables
3438
var config = require(configfile);
3539
console.log(ds() + "Reading " + configfile);
3640
for (key in config) {
3741
if (config[key] !== "" && config[key].slice(0,2) !== "__") {
3842
configd[key] = config[key]; // Update default
3943
}
4044
}
45+
46+
// Override defaults and config file with shell environment variables
47+
if (env.HAPISERVERPATH) {
48+
configd["HAPISERVERPATH"] = env.HAPISERVERPATH;
49+
}
50+
if (env.HAPISERVERHOME) {
51+
configd["HAPISERVERHOME"] = env.HAPISERVERHOME;
52+
}
53+
if (env.NODEEXE) {
54+
configd["NODEEXE"] = env.NODEEXE;
55+
}
56+
if (env.PYTHONEXE) {
57+
configd["PYTHONEXE"] = env.PYTHONEXE;
58+
}
59+
4160
configVars.config = configd;
4261
return configd;
4362

@@ -47,11 +66,10 @@ exports.configVars = configVars;
4766
function replaceConfigVars(com) {
4867
config = configVars();
4968
if (typeof(com) === "string") {
50-
com = com.replace("${cwd}", process.cwd());
51-
com = com.replace("$(HAPISERVER_BIN_DIR)", config["HAPISERVER_BIN_DIR"]);
52-
com = com.replace("$(HAPISERVER_META_DIR)", config["HAPISERVER_META_DIR"]);
53-
com = com.replace("$(NODE_EXE)", config["NODE_EXE"]);
54-
com = com.replace("$(PYTHON_EXE)", config["PYTHON_EXE"]);
69+
com = com.replace("$HAPISERVERPATH", config["HAPISERVERPATH"]);
70+
com = com.replace("$HAPISERVERHOME", config["HAPISERVERHOME"]);
71+
com = com.replace("$NODEEXE", config["NODEEXE"]);
72+
com = com.replace("$PYTHONEXE", config["PYTHONEXE"]);
5573
return com;
5674
} else {
5775
for (var i=0;i < com.length; i++) {
@@ -107,7 +125,7 @@ function getmetadata(str) {
107125
}
108126
//console.log(obj)
109127
} else {
110-
// TODO: Try to read as JSON first!
128+
// TODO: Try to read as JSON first.
111129
// Attempt to execute; if failure, assume it is a file.
112130
console.log(ds() + "Trying " + str + " as command line command.");
113131
if (commandExistsSync(str.split(" ")[0])) {
@@ -307,6 +325,7 @@ function prepmetadata(catalogfile,HAPIVERSION,FORCE,VERIFIER,PLOTSERVER) {
307325
// will be ignored. Need to catch this error.
308326

309327
if (json.data.file) {
328+
json.data.file = replaceConfigVars(json.data.file);
310329
if (!fs.existsSync(json.data.file)) {
311330
console.log(ds() + clc.red(json.data.file + " referenced in " + catalogid + " not found."));
312331
process.exit(1);

lib/test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ function ds() {return (new Date()).toISOString() + " [server] ";};
77
function commands(commandarr,catalog) {
88
for (var i = 0;i < commandarr.length;i++) {
99
command = commandarr[i].command;
10-
// TODO: Substitute paths.
11-
//console.log(teststr);
1210
console.log(ds() + "Running " + catalog + " test command " + i + ".");
1311
try {
1412
var out = require('child_process')
@@ -19,8 +17,6 @@ function commands(commandarr,catalog) {
1917
}
2018
commandarr[i].testnumber = i;
2119
commandarr[i].catalog = catalog;
22-
//commandarr[i].command = command;
23-
//console.log(out.toString());
2420
testoutput(commandarr[i],out.toString());
2521
}
2622
}

metadata/AutoplotExample1.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"data": {
3-
"command": "java -Djava.awt.headless=true -cp bin/autoplot.jar org.autoplot.AutoplotDataServer --uri=http://autoplot.org/data/autoplot.cdf?BGSM -f hapi-data 2> >( sed '/Autoplot version/d' >&2 )",
3+
"command": "java -Djava.awt.headless=true -cp $(HAPISERVER_BIN_DIR)/autoplot.jar org.autoplot.AutoplotDataServer --uri=http://autoplot.org/data/autoplot.cdf?BGSM -f hapi-data 2> >( sed '/Autoplot version/d' >&2 )",
44
"contact": "rweigel@gmu.edu",
55
"testcommands": [
6-
{"command": "java -Djava.awt.headless=true -cp bin/autoplot.jar org.autoplot.AutoplotDataServer --uri=http://autoplot.org/data/autoplot.cdf?BGSM -f hapi-data | python lib/subset.py --start 2006-01-01T00:00:00.000Z --stop 2006-01-01T23:00:00.000Z --columns 1,2","Nlines": 23}
6+
{"command": "java -Djava.awt.headless=true -cp $(HAPISERVER_BIN_DIR)/autoplot.jar org.autoplot.AutoplotDataServer --uri=http://autoplot.org/data/autoplot.cdf?BGSM -f hapi-data | python lib/subset.py --start 2006-01-01T00:00:00.000Z --stop 2006-01-01T23:00:00.000Z --columns 1,2","Nlines": 23}
77
],
88
"testurls": [
99
{"url": "${server}/data?id=ACE&parameters=Bx__GSM_&time.min=2006-01-01T00:00:00.000Z&time.max=2006-01-02T00:00:00.000Z&attach=false","Nlines": 24}
@@ -13,7 +13,7 @@
1313
[
1414
{
1515
"id": "ACE",
16-
"info": "java -Djava.awt.headless=true -cp bin/autoplot.jar org.autoplot.AutoplotDataServer --uri='http://autoplot.org/data/autoplot.cdf?BGSM' -f hapi-info"
16+
"info": "java -Djava.awt.headless=true -cp $(HAPISERVER_BIN_DIR)/autoplot.jar org.autoplot.AutoplotDataServer --uri='http://autoplot.org/data/autoplot.cdf?BGSM' -f hapi-info"
1717
}
1818
]
1919
}

metadata/AutoplotExample2.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"data": {
3-
"command": "bash ./bin/AutoplotExample2.sh ${parameters} ${start} ${stop} ${cwd}",
3+
"command": "bash $(HAPISERVER_BIN_DIR)/AutoplotExample2.sh ${parameters} ${start} ${stop} ${cwd}",
44
"formats": ["csv"],
55
"contact": "rweigel@gmu.edu",
66
"testcommands": [
7-
{"command": "bash ./bin/AutoplotExample2.sh Temperature 2018-01-06T00:00:00.000Z 2018-01-07T00:00:00.000Z ${cwd}", "Nlines": 1423}
7+
{"command": "bash $(HAPISERVER_BIN_DIR)/AutoplotExample2.sh Temperature 2018-01-06T00:00:00.000Z 2018-01-07T00:00:00.000Z ${cwd}", "Nlines": 1423}
88
],
99
"testurls": [
1010
{"url": "http://localhost:8999/AutoplotExample2/hapi/data?id=10.CF3744000800&parameters=Temperature&time.min=2018-01-06Z&time.max=2018-01-07T00:00:00.000Z", "Nlines": 1423}

metadata/Example0.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"landing": ""
66
},
77
"data": {
8-
"command": "python ./bin/Example.py",
8+
"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py",
99
"contact": "rweigel@gmu.edu",
1010
"testcommands": [
11-
{"command": "python ./bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
11+
{"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
1212
],
1313
"testurls": [
1414
{"url": "${server}/data/?id=dataset1&parameters=vector&time.min=1970-01-01T00:00Z&time.max=1970-01-01T00:10Z", "Nlines": 10},

metadata/Example1.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"data": {
3-
"command": "python ./bin/Example.py --start ${start} --stop ${stop}",
3+
"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --start ${start} --stop ${stop}",
44
"contact": "rweigel@gmu.edu",
55
"testcommands": [
6-
{"command": "python ./bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
6+
{"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
77
],
88
"testurls": [
99
{"url": "${server}/data/?id=dataset1&parameters=vector&time.min=1970-01-01T00:00Z&time.max=1970-01-01T00:10Z", "Nlines": 10},

metadata/Example2.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"data": {
3-
"command": "python ./bin/Example.py --params ${parameters} --start ${start} --stop ${stop} --fmt ${format}",
3+
"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --params ${parameters} --start ${start} --stop ${stop} --fmt ${format}",
44
"contact": "rweigel@gmu.edu",
55
"formats": ["csv","binary"],
66
"testcommands": [
7-
{"command": "python ./bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
7+
{"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
88
],
99
"testurls": [
1010
{"url": "${server}/data/?id=dataset1&parameters=vector&time.min=1970-01-01T00:00Z&time.max=1970-01-01T00:10Z", "Nlines": 10},

0 commit comments

Comments
 (0)