forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.js
More file actions
142 lines (124 loc) · 3.61 KB
/
start.js
File metadata and controls
142 lines (124 loc) · 3.61 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict'
const options = require('./options')
const fs = require('fs')
const { cyan, red, bold } = require('colorette')
module.exports = function (program, server) {
const start = program
.command('start')
.description('run the Solid server')
options
.filter((option) => !option.hide)
.forEach((option) => {
let name = '--' + option.name
if (!option.flag) {
name += ' [value]'
}
start.option(name, option.help)
})
start.option('-v, --verbose', 'Print the logs to console')
start.action((opts) => {
let argv = Object.assign({}, opts, { version: program.version() })
let configFile = argv['configFile'] || './config.json'
fs.readFile(configFile, (err, file) => {
// No file exists, not a problem
if (err) {
console.log(cyan(bold('TIP')), 'create a config.json: `$ solid init`')
} else {
// Use flags with priority over config file
const config = JSON.parse(file)
Object.keys(config).forEach((option) => {
argv[option] = argv[option] || config[option]
})
}
bin(argv, server)
})
})
}
function bin (argv, server) {
if (!argv.email) {
argv.email = {
host: argv['emailHost'],
port: argv['emailPort'],
secure: true,
auth: {
user: argv['emailAuthUser'],
pass: argv['emailAuthPass']
}
}
delete argv['emailHost']
delete argv['emailPort']
delete argv['emailAuthUser']
delete argv['emailAuthPass']
}
// Set up --no-*
argv.live = !argv.noLive
// Set up debug environment
if (argv.verbose) {
process.env.DEBUG = 'solid:*'
}
// Set up port
argv.port = argv.port || 3456
// Webid to be default in command line
if (argv.webid !== false) {
argv.webid = true
}
// Signal handling (e.g. CTRL+C)
if (process.platform !== 'win32') {
// Signal handlers don't work on Windows.
process.on('SIGINT', function () {
console.log('\nSolid stopped.')
process.exit()
})
}
// Overwrite root .acl if owner is specified
if (argv.owner) {
let rootPath = argv.root
if (!rootPath) {
rootPath = process.cwd()
}
if (!(rootPath.endsWith('/'))) {
rootPath += '/'
}
rootPath += (argv.suffixAcl || '.acl')
const defaultAcl = `@prefix n0: <http://www.w3.org/ns/auth/acl#>.
@prefix n2: <http://xmlns.com/foaf/0.1/>.
<#owner>
a n0:Authorization;
n0:accessTo <./>;
n0:agent <${argv.owner}>;
n0:defaultForNew <./>;
n0:mode n0:Control, n0:Read, n0:Write.
<#everyone>
a n0:Authorization;
n0: n2:Agent;
n0:accessTo <./>;
n0:defaultForNew <./>;
n0:mode n0:Read.`
fs.writeFileSync(rootPath, defaultAcl)
}
// // Finally starting solid
const solid = require('../../')
let app
try {
app = solid.createServer(argv, server)
} catch (e) {
if (e.code === 'EACCES') {
if (e.syscall === 'mkdir') {
console.log(red(bold('ERROR')), `You need permissions to create '${e.path}' folder`)
} else {
console.log(red(bold('ERROR')), 'You need root privileges to start on this port')
}
return 1
}
if (e.code === 'EADDRINUSE') {
console.log(red(bold('ERROR')), 'The port ' + argv.port + ' is already in use')
return 1
}
console.log(red(bold('ERROR')), e.message)
return 1
}
app.listen(argv.port, function () {
console.log(`Solid server (${argv.version}) running on \u001b[4mhttps://localhost:${argv.port}/\u001b[0m`)
console.log('Press <ctrl>+c to stop')
})
}