Skip to content

Commit 0a0d3f0

Browse files
committed
Merge pull request #53 from nicola/commandLine
Solving command line issue
2 parents e72f5f9 + 1f4d484 commit 0a0d3f0

19 files changed

Lines changed: 370 additions & 259 deletions

‎Makefile‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
# see also JSON validator http://jsonformatter.curiousconcept.com for the package.json file
22

33
run-for-test:
4-
node bin/ldnode.js -p 3456 -v --uriBase http://localhost:3456/test/ --fileBase `pwd`/test/
4+
node bin/ldnode.js -p 3456 -v --uri http://localhost:3456/test/ --base `pwd`/test/
55

66
run-for-test-login:
7-
node bin/ldnode.js -p 3456 -v --webid --uriBase https://localhost:3456/test/ --fileBase `pwd`/test/
7+
node bin/ldnode.js -p 3456 -v --webid --uri https://localhost:3456/test/ --base `pwd`/test/
88

99
run-for-live-test:
1010
NODE_PATH=.:../tabulator-firefox/content/js/rdf/dist/ node server.js \
11-
p 3456 -v --live --uriBase http://localhost:3456/test/ --fileBase `pwd`/test/
11+
p 3456 -v --live --uri http://localhost:3456/test/ --base `pwd`/test/
1212

1313
run-for-test-special:
1414
NODE_PATH=.:../tabulator-firefox/content/js/rdf/dist/ node server.js \
15-
-p 3456 -v --uriBase http://localhost:3456/test/ --fileBase `pwd`/test/
15+
-p 3456 -v --uri http://localhost:3456/test/ --base `pwd`/test/
1616

1717
# use --debug to run or --debug-brk to break immediately
1818
run-for-test-special-debug:
1919
NODE_PATH=.:../tabulator-firefox/content/js/rdf/dist/ node --debug-brk server.js \
20-
-p 3456 -v --uriBase http://localhost:3456/test/ --fileBase `pwd`/test/
20+
-p 3456 -v --uri http://localhost:3456/test/ --base `pwd`/test/
2121
# Then run node-inspector and open Chrome as it suggests
2222
# (If necessary, npm install -g node-inspector)
2323

‎README.md‎

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ npm install
3131
var ldnode = require('ldnode')
3232

3333
var ldp = ldnode.createServer({
34-
uriBase: "http://example.com/test/",
35-
fileBase: __dirname + '/test/'
34+
uri: "http://example.com/test/",
35+
base: __dirname + '/test/'
3636
})
3737
ldp.listen(1234, function() {
3838
// Started Linked Data Platform
@@ -70,29 +70,20 @@ $ ldnode -v
7070

7171
The command line tool has the following options
7272

73-
usage: ldnode [path] [options]
74-
75-
options:
76-
--uriBase Address, port, and default path of the server. (Example: http://localhost:3000/test/)
77-
--fileBase Base location to serve resources. Requests whose paths do not have fileBase as a prefix will be ignored
78-
--live Offer and support live updates
79-
-p Port to use
80-
-v Log messages to console
81-
--changesSuffix The suffix that will be used to identify the requests that will subscribe to changes to the object requested. Defaults to ,changes
82-
--cors Enable CORS via the 'Access-Control-Allow-Origin' header
83-
-c Set cache time (in seconds). e.g. -c10 for 10 seconds.
84-
To disable caching, use -c-1.
85-
--changesSuffix sss Change the URI suffix used for the URI of a change stream
86-
--SSESuffix sss Change the URI suffix used for the URI of a SSE stream
87-
88-
-S --ssl Enable https.
89-
-C --cert Path to ssl cert file (default: cert.pem).
90-
-K --key Path to ssl key file (default: key.pem).
91-
92-
--webid Enable WebID authentication
93-
--privateKey Path to the private key used to enable webid authentication
94-
--cert Path to the private key used to enable webid authentication
95-
-h --help Print this list and exit.
73+
Usage: ldnode [options]
74+
75+
Options:
76+
-v, --verbose Print the logs to console
77+
--version Print current ldnode version
78+
-u, --uri Default address of the server (e.g. http[s]://host:port/path)
79+
-b, --base Base location to serve resources
80+
-p, --port Port to use
81+
-c, --cache Set cache time (in seconds), 0 for no cache
82+
-K, --key Path to the ssl key
83+
-C, --cert Path to the ssl cert
84+
--webid Enable WebID+TLS authentication
85+
-s, --secret HTTP Session secret key (e.g. "your secret phrase")
86+
-s, --no-live Disable live support through WebSockets
9687

9788
## Tests
9889

‎acl.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ function allow(mode, req, res) {
2222

2323
var accessType = "accessTo";
2424

25-
var filepath = file.uriToFilename(req.path, options.fileBase);
26-
var relativePath = file.uriToRelativeFilename(req.path, options.fileBase);
25+
var filepath = file.uriToFilename(req.path, options.base);
26+
var relativePath = file.uriToRelativeFilename(req.path, options.base);
2727
var depth = relativePath.split('/');
2828

2929
//Handle glob requests
@@ -37,8 +37,8 @@ function allow(mode, req, res) {
3737
for (var i = 0; i < depth.length; i++) {
3838
var pathAcl = S(filepath).endsWith(aclExtension) ?
3939
filepath : filepath + aclExtension;
40-
var pathUri = file.filenameToBaseUri(filepath, options.uriBase, options.fileBase);
41-
relativePath = path.relative(options.fileBase, filepath);
40+
var pathUri = file.filenameToBaseUri(filepath, options.uri, options.base);
41+
relativePath = path.relative(options.base, filepath);
4242

4343
debug("Checking " + accessType + "<" + mode + "> to " +
4444
pathUri + " for WebID: " + req.session.userId);
@@ -203,19 +203,19 @@ function allow(mode, req, res) {
203203
accessType = "defaultForNew";
204204
if (i === 0) {
205205
if (path.dirname(path.dirname(relativePath)) == '.') {
206-
filepath = options.fileBase;
206+
filepath = options.base;
207207
} else if (S(relativePath).endsWith("/")) {
208-
filepath = options.fileBase + path.dirname(path.dirname(relativePath));
208+
filepath = options.base + path.dirname(path.dirname(relativePath));
209209
} else {
210-
filepath = options.fileBase + path.dirname(relativePath);
210+
filepath = options.base + path.dirname(relativePath);
211211
}
212212
} else {
213213
if (relativePath.length === 0) {
214214
break;
215215
} else if (path.dirname(path.dirname(relativePath)) === '.') {
216-
filepath = options.fileBase;
216+
filepath = options.base;
217217
} else {
218-
filepath = options.fileBase + path.dirname(path.dirname(relativePath));
218+
filepath = options.base + path.dirname(path.dirname(relativePath));
219219
}
220220
}
221221

‎bin/ldnode.js‎

Lines changed: 79 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,84 @@
11
#!/bin/env node
22

3-
var argv = require('optimist')
4-
.boolean('cors')
5-
.boolean('v')
6-
.boolean('live')
7-
.argv;
8-
9-
if (argv.h || argv.help || argv['?']) {
10-
console.log([
11-
"usage: ldnode [path] [options]",
12-
"",
13-
"options:",
14-
" --uriBase Address, port, and default path of the server. (Example: http://localhost:3000/test/)",
15-
" --fileBase Base location to serve resources. Requests whose paths do not have fileBase as a prefix will be ignored",
16-
" --live Offer and support live updates",
17-
" -p Port to use",
18-
" -v Log messages to console",
19-
" --changesSuffix The suffix that will be used to identify the requests that will subscribe to changes to the object requested. Defaults to ,changes",
20-
" --cors Enable CORS via the 'Access-Control-Allow-Origin' header",
21-
" -c Set cache time (in seconds). e.g. -c10 for 10 seconds.",
22-
" To disable caching, use -c-1.",
23-
" --changesSuffix sss Change the URI suffix used for the URI of a change stream",
24-
" --SSESuffix sss Change the URI suffix used for the URI of a SSE stream",
25-
"",
26-
" -S --ssl Enable https.",
27-
" -C --cert Path to ssl cert file (default: cert.pem).",
28-
" -K --key Path to ssl key file (default: key.pem).",
29-
"",
30-
" --webid Enable WebID authentication",
31-
" --privateKey Path to the private key used to enable webid authentication",
32-
" --cert Path to the private key used to enable webid authentication",
33-
" -h --help Print this list and exit."
34-
].join('\n'));
35-
process.exit();
3+
var fs = require('fs');
4+
var path = require('path');
5+
6+
var argv = require('nomnom')
7+
.script('ldnode')
8+
.option('verbose', {
9+
abbr: 'v',
10+
flag: true,
11+
help: 'Print the logs to console'
12+
})
13+
.option('version', {
14+
flag: true,
15+
help: 'Print current ldnode version',
16+
callback: function () {
17+
fs.readFile(path.resolve(__dirname, '../package.json'), 'utf-8', function(err, file) {
18+
console.log(JSON.parse(file).version);
19+
});
20+
}
21+
})
22+
.option('uri', {
23+
abbr: 'u',
24+
help: 'Default address of the server (e.g. http[s]://host:port/path)'
25+
})
26+
.option('base', {
27+
abbr: 'b',
28+
full: 'base',
29+
help: 'Base location to serve resources'
30+
})
31+
.option('port', {
32+
abbr: 'p',
33+
help: 'Port to use'
34+
})
35+
.option('cache', {
36+
abbr: 'c',
37+
help: 'Set cache time (in seconds), 0 for no cache'
38+
})
39+
.option('key', {
40+
help: 'Path to the ssl key',
41+
abbr: 'K',
42+
full: 'key'
43+
})
44+
.option('cert', {
45+
full: 'cert',
46+
help: 'Path to the ssl cert',
47+
abbr: 'C'
48+
})
49+
.option('webid', {
50+
help: 'Enable WebID+TLS authentication',
51+
full: 'webid',
52+
flag: true
53+
})
54+
.option('secret', {
55+
help: 'HTTP Session secret key (e.g. "your secret phrase")',
56+
abbr: 's'
57+
})
58+
.option('noLive', {
59+
full: 'no-live',
60+
help: 'Disable live support through WebSockets',
61+
abbr: 's',
62+
flag: true
63+
})
64+
.parse();
65+
66+
// Print version and leave
67+
if (argv.version) {
68+
return;
3669
}
3770

38-
process.env.DEBUG = argv.v ? 'ldnode:*' : false;
71+
// Set up --no-*
72+
argv.live = !argv.noLive;
73+
74+
// Set up debug environment
75+
process.env.DEBUG = argv.verbose ? 'ldnode:*' : false;
3976
var debug = require('../logging').server;
40-
var ldnode = require('../index');
4177

42-
// Signal handling
78+
// Set up port
79+
argv.port = argv.port || 3456;
80+
81+
// Signal handling (e.g. CTRL+C)
4382
if (process.platform !== 'win32') {
4483
// Signal handlers don't work on Windows.
4584
process.on('SIGINT', function() {
@@ -48,9 +87,10 @@ if (process.platform !== 'win32') {
4887
});
4988
}
5089

51-
// Starting ldnode
90+
// Finally starting ldnode
91+
var ldnode = require('../index');
5292
var app = ldnode.createServer(argv);
53-
app.listen(argv.p, function() {
54-
debug('LDP started on port ' + argv.p);
93+
app.listen(argv.port, function() {
94+
debug('LDP started on port ' + argv.port);
5595
});
5696

‎container.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var addUriTriple = function(kb, s, o, p) {
1919
var turtleExtension = ".ttl";
2020

2121
function createRootContainer(options) {
22-
if (!metadata.hasContainerMetadata(options.fileBase)) {
22+
if (!metadata.hasContainerMetadata(options.base)) {
2323
var rootContainer = $rdf.graph();
2424
addUriTriple(rootContainer, options.pathStart,
2525
rdfVocab.type, ldpVocab.Container);
@@ -30,13 +30,13 @@ function createRootContainer(options) {
3030
'"Root Container"');
3131
var serializedContainer = $rdf.serialize(undefined, rootContainer,
3232
options.pathStart, 'text/turtle');
33-
metadata.writeContainerMetadata(options.fileBase,
33+
metadata.writeContainerMetadata(options.base,
3434
serializedContainer, function(err) {
3535
if (err) {
3636
//TODO handle error
3737
debug("Could not write root container");
3838
} else {
39-
debug("Wrote root container to " + options.fileBase);
39+
debug("Wrote root container to " + options.base);
4040
}
4141
});
4242
}
@@ -74,10 +74,10 @@ function releaseResourceUri(options, uri) {
7474
}
7575

7676
function createNewResource(options, resourcePath, resourceGraph, callback) {
77-
var resourceURI = path.relative(options.fileBase, resourcePath);
77+
var resourceURI = path.relative(options.base, resourcePath);
7878
//TODO write files with relative URIS.
7979
var rawResource = $rdf.serialize(undefined,
80-
resourceGraph, options.uriBase + resourceURI, 'text/turtle');
80+
resourceGraph, options.uri + resourceURI, 'text/turtle');
8181
debug("Writing new resource to " + resourcePath);
8282
debug("Resource:\n" + rawResource);
8383
fs.writeFile(resourcePath, rawResource, writeResourceCallback);
@@ -103,7 +103,7 @@ function createNewContainer(options, containerPath, containerGraph, callback) {
103103
return callback(err);
104104
} else {
105105
var rawContainer = $rdf.serialize(undefined, containerGraph,
106-
options.uriBase, 'text/turtle');
106+
options.uri, 'text/turtle');
107107
debug("rawContainer " + rawContainer);
108108
metadata.writeContainerMetadata(containerPath, rawContainer,
109109
writeContainerCallback);

‎examples/.meta‎

Whitespace-only changes.

‎examples/simple-ldp-server.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var express = require('express')
2+
var ldnode = require('../') // or require('ldnode')
3+
4+
// Starting our express app
5+
var app = express()
6+
7+
// My routes
8+
app.get('/', function (req, res) {
9+
console.log(req)
10+
res.send('Welcome to my server!')
11+
})
12+
13+
// Mounting ldnode on /ldp
14+
var ldp = ldnode({
15+
uri: 'http://localhost:3000/ldp'
16+
})
17+
app.use('/ldp', ldp)
18+
19+
// Starting server
20+
app.listen(3000, function () {
21+
console.log('Server started on port 3000!')
22+
})
23+

‎fileStore.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ var fs = require('fs');
55
var path = require('path');
66
var S = require('string');
77

8-
function uriToFilename(uri, fileBase) {
9-
var filename = path.join(fileBase, uri);
8+
function uriToFilename(uri, base) {
9+
var filename = path.join(base, uri);
1010
// Make sure filename ends with '/' if filename exists and is a directory.
1111
try {
1212
var fileStats = fs.statSync(filename);
@@ -19,15 +19,15 @@ function uriToFilename(uri, fileBase) {
1919
return filename;
2020
}
2121

22-
function uriToRelativeFilename(uri, fileBase) {
23-
var filename = uriToFilename(uri, fileBase);
24-
var relative = path.relative(fileBase, filename);
22+
function uriToRelativeFilename(uri, base) {
23+
var filename = uriToFilename(uri, base);
24+
var relative = path.relative(base, filename);
2525
return relative;
2626
}
2727

28-
function filenameToBaseUri(filename, uriBase, fileBase) {
29-
var uriPath = S(filename).strip(fileBase).toString();
30-
return uriBase + uriPath;
28+
function filenameToBaseUri(filename, uri, base) {
29+
var uriPath = S(filename).strip(base).toString();
30+
return uri + uriPath;
3131
}
3232

3333
exports.uriToFilename = uriToFilename;

‎handlers/delete.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var metadata = require('../metadata.js');
1010
function handler(req, res) {
1111
var options = req.app.locals.ldp;
1212
debug('DELETE -- ' + req.path);
13-
var filename = file.uriToFilename(req.path, options.fileBase);
13+
var filename = file.uriToFilename(req.path, options.base);
1414
fs.stat(filename, function(err, stats) {
1515
if (err) {
1616
debug("DELETE -- unlink() error: " + err);

0 commit comments

Comments
 (0)