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