improve: tell user that the WebUI is available #631

Merged
Sainan merged 2 commits from improve-startup into main 2024-12-23 13:48:17 -08:00
3 changed files with 23 additions and 22 deletions

View File

@ -13,11 +13,6 @@ import { customRouter } from "@/src/routes/custom";
import { dynamicController } from "@/src/routes/dynamic"; import { dynamicController } from "@/src/routes/dynamic";
import { statsRouter } from "@/src/routes/stats"; import { statsRouter } from "@/src/routes/stats";
import { webuiRouter } from "@/src/routes/webui"; import { webuiRouter } from "@/src/routes/webui";
import { connectDatabase } from "@/src/services/mongoService";
import { registerLogFileCreationListener } from "@/src/utils/logger";
void registerLogFileCreationListener();
void connectDatabase();
const app = express(); const app = express();

View File

@ -7,21 +7,29 @@ import https from "https";
import fs from "node:fs"; import fs from "node:fs";
import { app } from "./app"; import { app } from "./app";
import { config } from "./services/configService"; import { config } from "./services/configService";
//const morgan = require("morgan"); import { connectDatabase } from "@/src/services/mongoService";
//const bodyParser = require("body-parser"); import { registerLogFileCreationListener } from "@/src/utils/logger";
registerLogFileCreationListener();
void (async (): Promise<void> => {
await connectDatabase();
const httpPort = config.httpPort || 80;
const httpsPort = config.httpsPort || 443;
coderabbitai[bot] commented 2024-12-23 12:19:29 -08:00 (Migrated from github.com)
Review

💡 Codebase verification

Based on the code analysis, I can now provide a final response:

Log file creation handling needs improvement
The current implementation of registerLogFileCreationListener only logs successful file creation/rotation events but lacks error handling. Consider adding error event listeners to handle potential file system issues:

  • Add error event handlers for both errorLog and combinedLog transports
  • Handle scenarios like permission issues or disk space problems
  • Consider implementing a fallback mechanism when file logging fails

Example:

errorLog.on('error', error => logger.error('Error with error log file:', error));
combinedLog.on('error', error => logger.error('Error with combined log file:', error));
🔗 Analysis chain

Consider verifying log file creation.
The function call “registerLogFileCreationListener()” looks fine. As a good practice, ensure any potential errors or edge cases (e.g., restricted file permissions) are handled or at least logged appropriately.


Let me search for the implementation of registerLogFileCreationListener to verify how it handles errors and edge cases.


Let me check how errorLog and combinedLog are initialized and if they handle file system errors properly.


Let me search for error handling around the log file creation.


Let me check how the logger is configured and if there's any error handling at the transport level.

🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Search for the implementation of registerLogFileCreationListener
ast-grep --pattern 'function registerLogFileCreationListener() {
  $$$
}'

# Also search using ripgrep in case it's defined differently
rg "registerLogFileCreationListener" -A 10

Length of output: 1795


Script:

#!/bin/bash
# Search for errorLog and combinedLog initialization and error handling
rg "const (errorLog|combinedLog)" -B 5 -A 10 src/utils/logger.ts

# Also look for any try-catch blocks or error handling around these logs
ast-grep --pattern 'try {
  $$$
} catch ($_) {
  $$$
}'

Length of output: 82850


Script:

#!/bin/bash
# Search for error handling around log file initialization
rg "errorLog|combinedLog" -B 5 -A 5 src/utils/logger.ts

# Also look for any try-catch blocks around log file operations
rg "new transports.DailyRotateFile" -B 5 -A 5 src/utils/logger.ts

Length of output: 1793


Script:

#!/bin/bash
# Look for logger configuration and error handling
rg "const logger = " -B 10 -A 10 src/utils/logger.ts

# Check if there are any error events being handled
rg "\.on\('error'" src/utils/logger.ts

Length of output: 709

_:bulb: Codebase verification_ Based on the code analysis, I can now provide a final response: **Log file creation handling needs improvement** The current implementation of `registerLogFileCreationListener` only logs successful file creation/rotation events but lacks error handling. Consider adding error event listeners to handle potential file system issues: - Add error event handlers for both `errorLog` and `combinedLog` transports - Handle scenarios like permission issues or disk space problems - Consider implementing a fallback mechanism when file logging fails Example: ```typescript errorLog.on('error', error => logger.error('Error with error log file:', error)); combinedLog.on('error', error => logger.error('Error with combined log file:', error)); ``` <details> <summary>🔗 Analysis chain</summary> **Consider verifying log file creation.** The function call “registerLogFileCreationListener()” looks fine. As a good practice, ensure any potential errors or edge cases (e.g., restricted file permissions) are handled or at least logged appropriately. --- Let me search for the implementation of `registerLogFileCreationListener` to verify how it handles errors and edge cases. --- Let me check how `errorLog` and `combinedLog` are initialized and if they handle file system errors properly. --- Let me search for error handling around the log file creation. --- Let me check how the logger is configured and if there's any error handling at the transport level. </details> <details> <summary>🏁 Scripts executed</summary> The following scripts were executed for the analysis: Error: No shell scripts found --- Script: ```shell #!/bin/bash # Search for the implementation of registerLogFileCreationListener ast-grep --pattern 'function registerLogFileCreationListener() { $$$ }' # Also search using ripgrep in case it's defined differently rg "registerLogFileCreationListener" -A 10 ``` Length of output: 1795 --- Script: ```shell #!/bin/bash # Search for errorLog and combinedLog initialization and error handling rg "const (errorLog|combinedLog)" -B 5 -A 10 src/utils/logger.ts # Also look for any try-catch blocks or error handling around these logs ast-grep --pattern 'try { $$$ } catch ($_) { $$$ }' ``` Length of output: 82850 --- Script: ```shell #!/bin/bash # Search for error handling around log file initialization rg "errorLog|combinedLog" -B 5 -A 5 src/utils/logger.ts # Also look for any try-catch blocks around log file operations rg "new transports.DailyRotateFile" -B 5 -A 5 src/utils/logger.ts ``` Length of output: 1793 --- Script: ```shell #!/bin/bash # Look for logger configuration and error handling rg "const logger = " -B 10 -A 10 src/utils/logger.ts # Check if there are any error events being handled rg "\.on\('error'" src/utils/logger.ts ``` Length of output: 709 </details> <!-- This is an auto-generated comment by CodeRabbit -->
const options = { const options = {
key: fs.readFileSync("static/certs/key.pem"), key: fs.readFileSync("static/certs/key.pem"),
cert: fs.readFileSync("static/certs/cert.pem") cert: fs.readFileSync("static/certs/cert.pem")
}; };
const httpPort = config.httpPort || 80; http.createServer(app).listen(httpPort, () => {
const httpsPort = config.httpsPort || 443; logger.info("HTTP server started on port " + httpPort);
https.createServer(options, app).listen(httpsPort, () => {
logger.info("HTTPS server started on port " + httpsPort);
// const server = http.createServer(app).listen(80); logger.info(
http.createServer(app).listen(httpPort, () => logger.info("HTTP server started on port " + httpPort)); "Access the WebUI in your browser at http://localhost" + (httpPort == 80 ? "" : ":" + httpPort)
const server = https.createServer(options, app); );
server.listen(httpsPort, () => logger.info("HTTPS server started on port " + httpsPort)); });
});
// server.keepAliveTimeout = 60 * 1000 + 1000; })();
// server.headersTimeout = 60 * 1000 + 2000;

View File

@ -8,7 +8,7 @@ if (url === undefined) {
throw new Error("MONGODB_URL not found. Make sure you have a .env file in the root of the project!"); throw new Error("MONGODB_URL not found. Make sure you have a .env file in the root of the project!");
} }
const connectDatabase = async () => { export const connectDatabase = async (): Promise<void> => {
try { try {
await mongoose.connect(`${url}`); await mongoose.connect(`${url}`);
logger.info("Connected to MongoDB"); logger.info("Connected to MongoDB");
@ -18,5 +18,3 @@ const connectDatabase = async () => {
} }
} }
}; };
export { connectDatabase };