1 | const chokidar = require('chokidar');
|
2 | const mime = require('mime-types');
|
3 | const path = require('path');
|
4 | const fs = require('fs-extra');
|
5 | const { spawn } = require('child_process');
|
6 | const { promisify } = require('util');
|
7 | const pidusage = require('pidusage');
|
8 | const selfupdate = require('selfupdate');
|
9 | const package = require('../package.json');
|
10 | const server = require('./server.js');
|
11 | const Domain = require('./domain.js');
|
12 | const Plugin = require('./plugins.js');
|
13 | const Parse = require('./parse.js');
|
14 |
|
15 | const default_modules = [
|
16 | {
|
17 | "vars": [
|
18 | "fs",
|
19 | "filesystem"
|
20 | ],
|
21 | "context": true,
|
22 | "path": "fs"
|
23 | },
|
24 | {
|
25 | "context": true,
|
26 | "vars": [
|
27 | "path"
|
28 | ],
|
29 | "path": "path"
|
30 | },
|
31 | {
|
32 | "context": true,
|
33 | "vars": [
|
34 | "include",
|
35 | "require"
|
36 | ],
|
37 | "path": "blaze/include.js"
|
38 | }
|
39 | ];
|
40 |
|
41 | const default_config = {
|
42 | "http": {
|
43 | "ports": [
|
44 | 80
|
45 | ]
|
46 | },
|
47 | "https": {
|
48 | "ports": [
|
49 | 443
|
50 | ]
|
51 | },
|
52 | "orders": [],
|
53 | "plugins": [],
|
54 | "logger": {
|
55 | "maxLogSize": 2000000,
|
56 | "logInterval": 86400000
|
57 | },
|
58 | "form": {
|
59 | "maxFileSize": 2147483648
|
60 | },
|
61 | "label": "Blaze Web Server",
|
62 | "contact": "",
|
63 | "root": "./www",
|
64 | "logs": "./logs"
|
65 | }
|
66 |
|
67 | let WATCHES = [];
|
68 |
|
69 | let processPath = path.join(__dirname, '/instances.json');
|
70 |
|
71 | let instancesFile = fs.existsSync(processPath) ? require('./instances.json') : [];
|
72 |
|
73 | module.exports.reload = () => {
|
74 | Object.keys(require.cache).forEach(key => { delete require.cache[key] });
|
75 | }
|
76 |
|
77 | module.exports.start = async installDir => {
|
78 | process.env.BLAZE_LIBRARY = __dirname;
|
79 | process.env.BLAZE_INSTANCE = installDir;
|
80 |
|
81 | let config = await fs.readJson(path.join(installDir, 'config.json'));
|
82 |
|
83 | Domain.compile(installDir);
|
84 | Plugin.compile(installDir);
|
85 |
|
86 | WATCHES.push(chokidar.watch(path.join(installDir, 'domains'), {
|
87 | persistent: true
|
88 | })
|
89 | .on('all', (event, path) => {
|
90 | Domain.compile(installDir);
|
91 | }));
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | let instance = new server(config);
|
101 | instance.listen();
|
102 | }
|
103 |
|
104 | module.exports.version = async () => {
|
105 | console.log('v' + package.version);
|
106 | }
|
107 |
|
108 | module.exports.uninstall = async installDir => {
|
109 | WATCHES.forEach(WATCH => {
|
110 | WATCH.close();
|
111 | });
|
112 |
|
113 | await fs.remove(installDir);
|
114 | }
|
115 |
|
116 | module.exports.install = async installDir => {
|
117 | let validDir = true;
|
118 | if (fs.existsSync(installDir)) {
|
119 | let files = fs.readdirSync(installDir);
|
120 | if (files.length) {
|
121 | console.log('Install directory must be empty');
|
122 | validDir = false;
|
123 | }
|
124 | }
|
125 |
|
126 | if (validDir) {
|
127 | await fs.ensureDir(installDir)
|
128 | .then(() => {
|
129 | let configPath = path.join(installDir, 'config.json');
|
130 | let domainsPath = path.join(installDir, 'domains');
|
131 | let certsPath = path.join(installDir, 'certs');
|
132 | let wwwPath = path.join(installDir, 'www');
|
133 | let logsPath = path.join(installDir, 'logs');
|
134 | let pluginsPath = path.join(installDir, 'plugins');
|
135 | let modulesPath = path.join(installDir, 'modules');
|
136 | let modulesConfigPath = path.join(modulesPath, 'modules.json');
|
137 |
|
138 | return Promise.all([
|
139 | fs.writeJSON(configPath, default_config),
|
140 | fs.mkdir(domainsPath),
|
141 | fs.mkdir(wwwPath),
|
142 | fs.mkdir(logsPath),
|
143 | fs.mkdir(pluginsPath),
|
144 | fs.mkdir(modulesPath).then(() => {
|
145 | return fs.writeJSON(modulesConfigPath, default_modules)
|
146 | }),
|
147 | fs.mkdir(certsPath)
|
148 | ]);
|
149 | });
|
150 | } else {
|
151 | return null;
|
152 | }
|
153 | }
|
154 |
|
155 | module.exports.help = (...commands) => {
|
156 | console.log('blaze web server v' + package.version + '\n');
|
157 | console.log('Commands:');
|
158 | let strLength = 4 + commands.reduce(function (longestCommand, schema) {
|
159 | return Math.max(longestCommand, schema.command.length);
|
160 | }, 0);
|
161 |
|
162 | commands.forEach(
|
163 | schema => {
|
164 | console.log(' blaze ' + (schema.command.padEnd(strLength)) + schema.help);
|
165 | }
|
166 | );
|
167 | }
|
168 |
|
169 | module.exports.parse = async (installDir, parser, context) => {
|
170 | if (parser) {
|
171 | Parse[parser](context);
|
172 | } else {
|
173 | Parse(context);
|
174 | }
|
175 | } |
\ | No newline at end of file |