forked from stamen/prosthetic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·126 lines (116 loc) · 3.49 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env node
var fs = require("fs"),
url = require("url"),
path = require("path"),
auth = require('http-auth'),
express = require("express"),
DEFAULT_PORT = 8001,
optimist = require("optimist")
.usage([
"Syringe usage:",
"1. with a proxy URL and one or more operation:",
" $0 --proxy URL [operations]",
"2. with a JSON config and proxy URL:",
" $0 -c config.json --proxy URL [options] [operations]",
"3. with a JSON config that includes the 'proxy' option:",
" $0 -c config.json [options] [operations]",
"where [operations] is an optional list of JSON filenames\ncontaining operation specs."
].join("\n\n"))
.option("proxy", {
alias: "p",
describe: "the URL to proxy (e.g. http://example.com)"
})
.option("rewrite", {
alias: "r",
boolean: true,
describe: "whether to rewrite the proxy URL in each response"
})
.option("config", {
alias: "c",
describe: "the config file to load"
})
.option("static", {
alias: "s",
describe: "Where to serve static assets ('/url:local-path')"
})
.option("port", {
alias: "P",
describe: "the port on which to listen (default: " + DEFAULT_PORT + ")",
}),
argv = optimist.argv,
argc = argv._,
prosthetic = require("./index");
var operate,
config = {
proxy: argv.proxy,
rewriteUrls: argv.rewrite,
static: argv.static
};
if (argv.config) {
try {
var conf = JSON.parse(fs.readFileSync(argv.config));
} catch (err) {
console.error("unable to parse config from", argv.config, ":", err);
return;
}
for (var key in conf) {
config[key] = conf[key];
}
}
if (!config.proxy && argc.length) {
config.proxy = argc.shift();
console.warn("no proxy declared, using the first argument:", config.proxy);
}
if (!config.proxy) {
return optimist.showHelp();
} else if (config.proxy.indexOf("://") === -1) {
config.proxy = "http://" + config.proxy;
}
var operate = prosthetic(config);
argc.forEach(function(filename) {
try {
var ops = JSON.parse(fs.readFileSync(filename));
} catch (err) {
console.err("unable to parse config in", filename, ":", err);
return;
}
console.log("+ adding ops from:", filename);
operate.add(ops);
});
var app = express();
if (config.static) {
var staticUrl = "/",
staticPath = "";
switch (typeof config.static) {
case "object":
staticUrl = config.static.url;
staticPath = config.static.path;
break;
case "string":
if (config.static.indexOf(":") > -1) {
var bits = config.static.split(":", 2);
staticUrl = bits[0];
staticPath = bits[1];
}
break;
}
staticPath = path.resolve(process.cwd(), staticPath);
console.warn("serving static assets on:", staticUrl, "from:", staticPath);
app.use(staticUrl, express.static(staticPath));
}
var USER = process.env.USER,
PASSWORD = process.env.PASSWORD;
var basic = auth.basic({
realm: "ACA only."
}, function (username, password, callback) { // Custom authentication method.
callback(username === USER && password === PASSWORD);
}
);
app.use(auth.connect(basic));
app.use(operate);
app.listen(argv.port || process.env.PORT || DEFAULT_PORT, function() {
var addr = this.address(),
base = ["http://", addr.address, ":", addr.port].join("");
operate.base(base);
console.log("++ listening at:", base);
});