|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +var fs = require('fs') |
| 4 | +var path = require('path') |
| 5 | +var argv = require('nomnom') |
| 6 | + .script('solid') |
| 7 | + .option('version', { |
| 8 | + flag: true, |
| 9 | + help: 'Print current solid version', |
| 10 | + callback: function () { |
| 11 | + fs.readFile(path.resolve(__dirname, '../package.json'), 'utf-8', function (_, file) { |
| 12 | + console.log(JSON.parse(file).version) |
| 13 | + }) |
| 14 | + } |
| 15 | + }) |
| 16 | + .option('verbose', { |
| 17 | + abbr: 'v', |
| 18 | + flag: true, |
| 19 | + help: 'Print the logs to console\n' |
| 20 | + }) |
| 21 | + .option('root', { |
| 22 | + help: 'Root folder to serve (defaut: \'./\')' |
| 23 | + }) |
| 24 | + .option('port', { |
| 25 | + help: 'Port to use' |
| 26 | + }) |
| 27 | + .option('webid', { |
| 28 | + help: 'Enable WebID+TLS authentication (use `--no-webid` for HTTP instead of HTTPS)', |
| 29 | + full: 'webid', |
| 30 | + flag: true |
| 31 | + }) |
| 32 | + .option('owner', { |
| 33 | + help: 'Set the owner of the storage' |
| 34 | + }) |
| 35 | + .option('key', { |
| 36 | + help: 'Path to the SSL private key in PEM format', |
| 37 | + full: 'ssl-key' |
| 38 | + }) |
| 39 | + .option('cert', { |
| 40 | + full: 'ssl-cert', |
| 41 | + help: 'Path to the SSL certificate key in PEM format' |
| 42 | + }) |
| 43 | + .option('idp', { |
| 44 | + help: 'Allow users to register their WebID on subdomains\n', |
| 45 | + full: 'allow-signup', |
| 46 | + flag: true |
| 47 | + }) |
| 48 | + .option('noLive', { |
| 49 | + full: 'no-live', |
| 50 | + help: 'Disable live support through WebSockets', |
| 51 | + flag: true |
| 52 | + }) |
| 53 | + .option('defaultApp', { |
| 54 | + full: 'default-app', |
| 55 | + help: 'URI to use as a default app for resources (default: https://linkeddata.github.io/warp/#/list/)' |
| 56 | + }) |
| 57 | + .option('proxy', { |
| 58 | + full: 'proxy', |
| 59 | + help: 'Use a proxy on example.tld/proxyPath' |
| 60 | + }) |
| 61 | + .option('fileBrowser', { |
| 62 | + full: 'file-browser', |
| 63 | + help: 'URI to use as a default app for resources (default: https://linkeddata.github.io/warp/#/list/)' |
| 64 | + }) |
| 65 | + .option('dataBrowser', { |
| 66 | + full: 'data-browser', |
| 67 | + flag: true, |
| 68 | + help: 'Enable viewing RDF resources using a default data browser application (e.g. mashlib)' |
| 69 | + }) |
| 70 | + .option('suffixAcl', { |
| 71 | + full: 'suffix-acl', |
| 72 | + help: 'Suffix for acl files (default: \'.acl\')' |
| 73 | + }) |
| 74 | + .option('suffixMeta', { |
| 75 | + full: 'suffix-meta', |
| 76 | + help: 'Suffix for metadata files (default: \'.meta\')' |
| 77 | + }) |
| 78 | + .option('secret', { |
| 79 | + help: 'Secret used to sign the session ID cookie (e.g. "your secret phrase")', |
| 80 | + full: 'session-secret' |
| 81 | + }) |
| 82 | + .option('noErrorPages', { |
| 83 | + full: 'no-error-pages', |
| 84 | + flag: true, |
| 85 | + help: 'Disable custom error pages (use Node.js default pages instead)' |
| 86 | + }) |
| 87 | + .option('errorPages', { |
| 88 | + full: 'error-pages', |
| 89 | + help: 'Folder from which to look for custom error pages files (files must be named <error-code>.html -- eg. 500.html)' |
| 90 | + }) |
| 91 | + .option('mount', { |
| 92 | + help: 'Serve on a specific URL path (default: \'/\')' |
| 93 | + }) |
| 94 | + .option('forceUser', { |
| 95 | + help: 'Force a WebID to always be logged in (useful when offline)', |
| 96 | + full: 'force-user' |
| 97 | + }) |
| 98 | + .option('strictOrigin', { |
| 99 | + help: 'Enforce same origin policy in the ACL', |
| 100 | + full: 'strict-origin', |
| 101 | + flag: true |
| 102 | + }) |
| 103 | + .parse() |
| 104 | + |
| 105 | +function bin (argv) { |
| 106 | + // Print version and leave |
| 107 | + if (argv.version) { |
| 108 | + return 0 |
| 109 | + } |
| 110 | + |
| 111 | + // Set up --no-* |
| 112 | + argv.live = !argv.noLive |
| 113 | + |
| 114 | + // Set up debug environment |
| 115 | + process.env.DEBUG = argv.verbose ? 'solid:*' : false |
| 116 | + var debug = require('../lib/debug').server |
| 117 | + |
| 118 | + // Set up port |
| 119 | + argv.port = argv.port || 3456 |
| 120 | + |
| 121 | + // Webid to be default in command line |
| 122 | + if (argv.webid !== false) { |
| 123 | + argv.webid = true |
| 124 | + } |
| 125 | + |
| 126 | + // Signal handling (e.g. CTRL+C) |
| 127 | + if (process.platform !== 'win32') { |
| 128 | + // Signal handlers don't work on Windows. |
| 129 | + process.on('SIGINT', function () { |
| 130 | + debug('LDP stopped.') |
| 131 | + process.exit() |
| 132 | + }) |
| 133 | + } |
| 134 | + |
| 135 | + if (argv.owner) { |
| 136 | + var rootPath = argv.root |
| 137 | + if (!rootPath) { |
| 138 | + rootPath = process.cwd() |
| 139 | + } |
| 140 | + if (!(rootPath.endsWith('/'))) { |
| 141 | + rootPath += '/' |
| 142 | + } |
| 143 | + rootPath += (argv.suffixAcl || '.acl') |
| 144 | + |
| 145 | + var defaultAcl = `@prefix n0: <http://www.w3.org/ns/auth/acl#>. |
| 146 | + @prefix n2: <http://xmlns.com/foaf/0.1/>. |
| 147 | +
|
| 148 | + <#owner> |
| 149 | + a n0:Authorization; |
| 150 | + n0:accessTo <./>; |
| 151 | + n0:agent <${argv.owner}>; |
| 152 | + n0:defaultForNew <./>; |
| 153 | + n0:mode n0:Control, n0:Read, n0:Write. |
| 154 | + <#everyone> |
| 155 | + a n0:Authorization; |
| 156 | + n0: n2:Agent; |
| 157 | + n0:accessTo <./>; |
| 158 | + n0:defaultForNew <./>; |
| 159 | + n0:mode n0:Read.' > .acl` |
| 160 | + |
| 161 | + fs.writeFileSync(rootPath, defaultAcl) |
| 162 | + } |
| 163 | + |
| 164 | + // Finally starting solid |
| 165 | + var solid = require('../') |
| 166 | + var app |
| 167 | + try { |
| 168 | + app = solid.createServer(argv) |
| 169 | + } catch (e) { |
| 170 | + if (e.code === 'EACCES') { |
| 171 | + console.log('You need root privileges to start on this port') |
| 172 | + return 1 |
| 173 | + } |
| 174 | + if (e.code === 'EADDRINUSE') { |
| 175 | + console.log('The port ' + argv.port + ' is already in use') |
| 176 | + return 1 |
| 177 | + } |
| 178 | + console.log(e.message) |
| 179 | + console.log(e.stack) |
| 180 | + return 1 |
| 181 | + } |
| 182 | + app.listen(argv.port, function () { |
| 183 | + fs.readFile(path.resolve(__dirname, '../package.json'), 'utf-8', function (_, file) { |
| 184 | + console.log('Solid server (solid v' + JSON.parse(file).version + ') running on \u001b[4mhttps://localhost:' + argv.port + '/\u001b[0m') |
| 185 | + console.log('Press <ctrl>+c to stop') |
| 186 | + }) |
| 187 | + }) |
| 188 | +} |
| 189 | + |
| 190 | +bin(argv) |
0 commit comments