Skip to content

Commit 4c26e36

Browse files
committed
Refactor setup
1 parent 3a8c6b9 commit 4c26e36

File tree

2 files changed

+56
-49
lines changed

2 files changed

+56
-49
lines changed

Diff for: app/index.js

+4-31
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,20 @@
11
const config = require("config");
2-
32
const clfdate = require("helper/clfdate");
43
const email = require("helper/email");
5-
const scheduler = require("./scheduler");
64
const setup = require("./setup");
75
const server = require("./server");
8-
const flush = require("documentation/tools/flush-cache");
9-
const configureLocalBlogs = require("./configure-local-blogs");
10-
11-
console.log(clfdate(), `Starting server`);
126

7+
console.log(clfdate(), `Starting server env=${config.environment}`);
138
setup(async (err) => {
149
if (err) throw err;
1510

16-
// Flush the cache of the public site and documentation
17-
flush();
18-
19-
// This is the master process
20-
if (config.master) {
21-
// Launch scheduler for background tasks, like backups, emails
22-
scheduler();
23-
24-
// Run any initialization that clients need
25-
// Google Drive will renew any webhooks, e.g.
26-
for (const { init, display_name } of Object.values(require("clients"))) {
27-
if (init) {
28-
console.log(clfdate(), display_name + " client:", "Initializing");
29-
init();
30-
}
31-
}
32-
}
33-
34-
// Send an email notification if the server starts or restarts
35-
email.SERVER_START(null, { container: config.container });
36-
3711
console.log(clfdate(), "Finished setting up server");
3812

3913
// Open the server to handle requests
4014
server.listen(config.port, function () {
4115
console.log(clfdate(), `Server listening`);
42-
43-
if (config.environment === "development") {
44-
configureLocalBlogs();
45-
}
16+
17+
// Send an email notification if the server starts or restarts
18+
email.SERVER_START(null, { container: config.container });
4619
});
4720
});

Diff for: app/setup.js

+52-18
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
const config = require("config");
22
const fs = require("fs-extra");
33

4-
const redis = require("models/redis");
5-
const client = new redis();
4+
const client = require("models/client");
65
const documentation = require("./documentation/build");
76
const templates = require("./templates");
87
const folders = require("./templates/folders");
98
const async = require("async");
109
const clfdate = require("helper/clfdate");
10+
const scheduler = require("./scheduler");
11+
const flush = require("documentation/tools/flush-cache");
12+
const configureLocalBlogs = require("./configure-local-blogs");
1113

12-
const log = (...arguments) =>
13-
console.log.apply(null, [clfdate(), "Setup:", ...arguments]);
14+
const log = (...args) =>
15+
console.log.apply(null, [clfdate(), "Setup:", ...args]);
1416

1517
function main(callback) {
1618
async.series(
@@ -72,25 +74,57 @@ function main(callback) {
7274

7375
async function () {
7476
// The docker build stage for production runs this script ahead of time
75-
if (config.environment === "development") {
76-
await documentation({ watch: true });
77-
} else {
78-
log("Skipping documentation build");
79-
}
77+
if (config.environment !== "development") return;
78+
await documentation({ watch: true });
8079
},
8180

8281
async function () {
83-
if (config.environment === "production" && config.master) {
84-
log("Building folders");
85-
try {
86-
await folders();
87-
log("Built folders");
88-
} catch (e) {
89-
log("Error building folders", e);
82+
if (config.environment !== "production") return;
83+
if (!config.master) return;
84+
85+
log("Building folders");
86+
try {
87+
await folders();
88+
log("Built folders");
89+
} catch (e) {
90+
log("Error building folders", e);
91+
}
92+
},
93+
94+
function (callback) {
95+
if (!config.master) return callback();
96+
97+
// Launch scheduler for background tasks, like backups, emails
98+
scheduler();
99+
callback();
100+
},
101+
102+
function (callback) {
103+
if (!config.master) return callback();
104+
105+
// Run any initialization that clients need
106+
// Google Drive will renew any webhooks, e.g.
107+
for (const { init, display_name } of Object.values(
108+
require("clients")
109+
)) {
110+
if (init) {
111+
console.log(clfdate(), display_name + " client:", "Initializing");
112+
init();
90113
}
91-
} else {
92-
log("Skipping folder build");
93114
}
115+
callback();
116+
},
117+
118+
function (callback) {
119+
// Flush the cache of the public site and documentation
120+
flush();
121+
122+
callback();
123+
},
124+
125+
function (callback) {
126+
if (config.environment !== "development") return callback();
127+
configureLocalBlogs();
94128
},
95129
],
96130
callback

0 commit comments

Comments
 (0)