Skip to content

Commit ae5126a

Browse files
committed
Clean up logging and code importing
1 parent 68ba3c5 commit ae5126a

File tree

10 files changed

+66
-20
lines changed

10 files changed

+66
-20
lines changed

binaries/helper-code/logger.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3+
const SORT_ME_LAST_UNICODE = "\uFFFF";
34
/*
45
* Since the config and server startup functions are a big mix of sync/async, the logging often
56
* gets weird, so this helper class will store the logs during execution, then flush them to
@@ -19,16 +20,34 @@ class Logger {
1920
const seq = this.sequence++;
2021
this.logs.push({ sequence: seq, ...logEntry });
2122
}
23+
// we only want to print some things during startup (not during lint)
24+
printDuringStartup(entry) {
25+
// CHILD_PROCESS is only true during startup
26+
if (process.env.CHILD_PROCESS) {
27+
console.log(entry);
28+
}
29+
}
2230
flushLogs() {
2331
this.logs
2432
.sort((a, b) => {
2533
var _a, _b, _c, _d;
26-
// First, sort by domain
27-
if (((_a = a.domain) !== null && _a !== void 0 ? _a : "\uFFFF") < ((_b = b.domain) !== null && _b !== void 0 ? _b : "\uFFFF"))
34+
/*
35+
* Sorting the logs can be a bit tricky. We want to sort all the log messages by the domain
36+
* name that threw the log.. but many logs are thrown without a specific domain name
37+
* attached. We can sort those too of course, but we always want them to come at the very
38+
* end of the logs, because in practice it makes way more sense to read them this way.
39+
* So the best way I found to do that, maybe it's hacky, but if a domain isn't there we
40+
* sort that log as if it were the very last unicode character, so no domain name could
41+
* ever come after it. Yes, we could probably do an extra if statement and just return
42+
* `1`, but this felt like the most intuitive way to implement and talk about this concept.
43+
*/
44+
if (((_a = a.domain) !== null && _a !== void 0 ? _a : SORT_ME_LAST_UNICODE) <
45+
((_b = b.domain) !== null && _b !== void 0 ? _b : SORT_ME_LAST_UNICODE))
2846
return -1;
29-
if (((_c = a.domain) !== null && _c !== void 0 ? _c : "\uFFFF") > ((_d = b.domain) !== null && _d !== void 0 ? _d : "\uFFFF"))
47+
if (((_c = a.domain) !== null && _c !== void 0 ? _c : SORT_ME_LAST_UNICODE) >
48+
((_d = b.domain) !== null && _d !== void 0 ? _d : SORT_ME_LAST_UNICODE))
3049
return 1;
31-
// If domains are equal or non existent, compare by sequence
50+
// If domains are equal (or both non existent), compare by sequence
3251
return a.sequence - b.sequence;
3352
})
3453
.forEach((log) => {

binaries/helper-code/utilities.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
44
};
55
Object.defineProperty(exports, "__esModule", { value: true });
66
exports.isValidDomain = exports.isDirectoryOrSymlinkDirectory = exports.formatElapsedTime = void 0;
7-
const path_1 = __importDefault(require("path"));
87
const fs_1 = __importDefault(require("fs"));
8+
const path_1 = __importDefault(require("path"));
99
// Helper function to format milliseconds into human-readable format
1010
const formatElapsedTime = (ms) => {
1111
const seconds = Math.floor(ms / 1000);

binaries/server/configure.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ const fs_1 = __importDefault(require("fs"));
3333
const path_1 = __importDefault(require("path"));
3434
const serve_static_1 = __importDefault(require("serve-static"));
3535
const vhost_1 = __importDefault(require("vhost"));
36-
const utilities_1 = require("../helper-code/utilities");
36+
const logger_1 = __importDefault(require("../helper-code/logger"));
3737
const tree_traversal_1 = require("../helper-code/tree-traversal");
38+
const utilities_1 = require("../helper-code/utilities");
3839
const _404_1 = __importDefault(require("../html/404"));
39-
const logger_1 = __importDefault(require("../helper-code/logger"));
4040
// Synchronous function to check for the existence of a direct-child 'html' directory
4141
const containsHtmlDirectory = (pathToDirectory) => {
4242
try {
@@ -103,6 +103,7 @@ const configureHttpServer = async () => {
103103
if (containsHtmlDirectory(pathToDomain)) {
104104
pathToDomain = path_1.default.join(pathToDomain, "html");
105105
}
106+
logger_1.default.printDuringStartup(`found domain ${match.toUpperCase()} - serving from ${pathToDomain}`);
106107
logger_1.default.log({
107108
locationInCode: "configureHttpServer",
108109
domain: match,

binaries/server/ssl.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
66
const fs_1 = __importDefault(require("fs"));
77
const path_1 = __importDefault(require("path"));
88
const vhttps_1 = __importDefault(require("vhttps"));
9-
const tree_traversal_1 = require("../helper-code/tree-traversal");
109
const logger_1 = __importDefault(require("../helper-code/logger"));
10+
const tree_traversal_1 = require("../helper-code/tree-traversal");
1111
const getCertificateFiles = (match) => {
1212
const basePath = path_1.default.join(process.cwd(), `./certificates/${match}`);
1313
let cert;
@@ -33,6 +33,7 @@ const configureSSL = (app) => {
3333
// Loop through directories in `./certificates`, looking for domain names
3434
const matches = (0, tree_traversal_1.findDomainSubdirectories)("./certificates");
3535
matches.forEach((match) => {
36+
logger_1.default.printDuringStartup(`retrieving certificate files for ${match.toUpperCase()}`);
3637
certificates.push({
3738
hostname: match,
3839
...getCertificateFiles(match),

binaries/union.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
66
Object.defineProperty(exports, "__esModule", { value: true });
77
exports.DEFAULT_HTTPS_PORT = exports.DEFAULT_HTTP_PORT = void 0;
88
const pm2_1 = __importDefault(require("pm2"));
9-
const ssl_1 = __importDefault(require("./server/ssl"));
109
const configure_1 = __importDefault(require("./server/configure"));
10+
const ssl_1 = __importDefault(require("./server/ssl"));
1111
const lint_1 = __importDefault(require("./commands/lint"));
12+
const logger_1 = __importDefault(require("./helper-code/logger"));
13+
const help_1 = __importDefault(require("./commands/help"));
1214
const status_1 = __importDefault(require("./commands/status"));
1315
const stop_1 = __importDefault(require("./commands/stop"));
14-
const help_1 = __importDefault(require("./commands/help"));
1516
exports.DEFAULT_HTTP_PORT = 80;
1617
exports.DEFAULT_HTTPS_PORT = 443;
1718
// Check if it's the child process
@@ -48,8 +49,8 @@ switch (args[0]) {
4849
const httpPort = args[1] || exports.DEFAULT_HTTP_PORT;
4950
const httpsApp = (0, ssl_1.default)(httpApp);
5051
const httpsPort = args[2] || exports.DEFAULT_HTTPS_PORT;
51-
httpApp.listen(httpPort);
52-
httpsApp.listen(httpsPort);
52+
httpApp.listen(httpPort, () => logger_1.default.printDuringStartup(`union server (http) is running on port ${httpPort}`));
53+
httpsApp.listen(httpsPort, () => logger_1.default.printDuringStartup(`union server (https) is running on port ${httpsPort}`));
5354
})
5455
.catch((error) => console.error("error configuring app: ", error));
5556
}

source/helper-code/logger.ts

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ class Logger {
4242
this.logs.push({ sequence: seq, ...logEntry });
4343
}
4444

45+
// we only want to print some things during startup (not during lint)
46+
printDuringStartup(entry: any) {
47+
// CHILD_PROCESS is only true during startup
48+
if (process.env.CHILD_PROCESS) {
49+
console.log(entry);
50+
}
51+
}
52+
4553
flushLogs() {
4654
this.logs
4755
.sort((a, b) => {

source/helper-code/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import path from "path";
21
import fs from "fs";
2+
import path from "path";
33

44
// Helper function to format milliseconds into human-readable format
55
export const formatElapsedTime = (ms: number) => {

source/server/configure.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import path from "path";
66
import serveStatic from "serve-static";
77
import vhost from "vhost";
88

9-
import { isDirectoryOrSymlinkDirectory } from "../helper-code/utilities";
9+
import logger from "../helper-code/logger";
1010
import {
1111
findDomainSubdirectories,
1212
findFilesRecursive,
1313
findSingleFile,
1414
} from "../helper-code/tree-traversal";
15+
import { isDirectoryOrSymlinkDirectory } from "../helper-code/utilities";
1516
import errorPageHTML from "../html/404";
16-
import logger from "../helper-code/logger";
1717

1818
// Synchronous function to check for the existence of a direct-child 'html' directory
1919
const containsHtmlDirectory = (pathToDirectory: string): boolean => {
@@ -95,6 +95,10 @@ const configureHttpServer = async (): Promise<express.Express> => {
9595
pathToDomain = path.join(pathToDomain, "html");
9696
}
9797

98+
logger.printDuringStartup(
99+
`found domain ${match.toUpperCase()} - serving from ${pathToDomain}`
100+
);
101+
98102
logger.log({
99103
locationInCode: "configureHttpServer",
100104
domain: match,

source/server/ssl.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import path from "path";
33
import vhttps, { Credential } from "vhttps";
44
import { Express as ExpressType } from "express";
55

6-
import { findDomainSubdirectories } from "../helper-code/tree-traversal";
76
import logger from "../helper-code/logger";
7+
import { findDomainSubdirectories } from "../helper-code/tree-traversal";
88

99
const getCertificateFiles = (
1010
match: string
@@ -36,6 +36,9 @@ const configureSSL = (app: ExpressType) => {
3636
// Loop through directories in `./certificates`, looking for domain names
3737
const matches = findDomainSubdirectories("./certificates");
3838
matches.forEach((match) => {
39+
logger.printDuringStartup(
40+
`retrieving certificate files for ${match.toUpperCase()}`
41+
);
3942
certificates.push({
4043
hostname: match,
4144
...getCertificateFiles(match),

source/union.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
import pm2 from "pm2";
44

5-
import configureSSL from "./server/ssl";
65
import configureHttpServer from "./server/configure";
6+
import configureSSL from "./server/ssl";
77
import lint from "./commands/lint";
8+
import logger from "./helper-code/logger";
9+
import showHelp from "./commands/help";
810
import showStatus from "./commands/status";
911
import stopAllProcesses from "./commands/stop";
10-
import showHelp from "./commands/help";
1112

1213
export const DEFAULT_HTTP_PORT = 80;
1314
export const DEFAULT_HTTPS_PORT = 443;
@@ -50,8 +51,16 @@ switch (args[0]) {
5051
const httpsApp = configureSSL(httpApp);
5152
const httpsPort = args[2] || DEFAULT_HTTPS_PORT;
5253

53-
httpApp.listen(httpPort);
54-
httpsApp.listen(httpsPort);
54+
httpApp.listen(httpPort, () =>
55+
logger.printDuringStartup(
56+
`union server (http) is running on port ${httpPort}`
57+
)
58+
);
59+
httpsApp.listen(httpsPort, () =>
60+
logger.printDuringStartup(
61+
`union server (https) is running on port ${httpsPort}`
62+
)
63+
);
5564
})
5665
.catch((error) => console.error("error configuring app: ", error));
5766
} else {

0 commit comments

Comments
 (0)