mirror of
https://github.com/gnmyt/myspeed.git
synced 2026-02-10 23:58:38 -06:00
98 lines
3.4 KiB
JavaScript
Executable File
98 lines
3.4 KiB
JavaScript
Executable File
import express from 'express';
|
|
import path from 'node:path';
|
|
import fs from 'node:fs';
|
|
import { fileURLToPath } from 'node:url';
|
|
import * as timerTask from './tasks/timer.js';
|
|
import * as integrationTask from './tasks/integrations.js';
|
|
import './util/createFolders.js';
|
|
import './util/loadServers.js';
|
|
import errorHandler from './util/errorHandler.js';
|
|
import errorMiddleware from './middlewares/error.js';
|
|
import configRoutes from './routes/config.js';
|
|
import speedtestsRoutes from './routes/speedtests.js';
|
|
import systemRoutes from './routes/system.js';
|
|
import storageRoutes from './routes/storage.js';
|
|
import recommendationsRoutes from './routes/recommendations.js';
|
|
import nodesRoutes from './routes/nodes.js';
|
|
import integrationsRoutes from './routes/integrations.js';
|
|
import prometheusRoutes from './routes/prometheus.js';
|
|
import opengraphRoutes from './routes/opengraph.js';
|
|
import db from './config/database.js';
|
|
import * as config from './controller/config.js';
|
|
import { initialize as initializeIntegrations } from './controller/integrations.js';
|
|
import { requestInterfaces } from './util/loadInterfaces.js';
|
|
import { load as loadCli } from './util/loadCli.js';
|
|
import { removeOld } from './tasks/speedtest.js';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const devModeHtml = fs.readFileSync(path.join(__dirname, 'templates', 'env.html'), 'utf-8');
|
|
|
|
const app = express();
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
const port = process.env.SERVER_PORT || 5216;
|
|
|
|
process.on('uncaughtException', err => errorHandler(err));
|
|
|
|
app.use(express.json());
|
|
app.use(errorMiddleware);
|
|
|
|
app.use("/api/config", configRoutes);
|
|
app.use("/api/speedtests", speedtestsRoutes);
|
|
app.use("/api/info", systemRoutes);
|
|
app.use("/api/storage", storageRoutes);
|
|
app.use("/api/recommendations", recommendationsRoutes);
|
|
app.use("/api/nodes", nodesRoutes);
|
|
app.use("/api/integrations", integrationsRoutes);
|
|
app.use("/api/prometheus", prometheusRoutes);
|
|
app.use('/api/opengraph', opengraphRoutes);
|
|
app.use("/api*all", (req, res) => res.status(404).json({message: "Route not found"}));
|
|
|
|
let buildPath = path.join(__dirname, '..', 'build');
|
|
let buildExists = fs.existsSync(buildPath);
|
|
|
|
if (!buildExists) {
|
|
buildPath = path.join(process.cwd(), 'build');
|
|
buildExists = fs.existsSync(buildPath);
|
|
}
|
|
|
|
if (buildExists) {
|
|
app.use(express.static(buildPath));
|
|
app.get('*all', (req, res) => res.sendFile(path.join(buildPath, 'index.html')));
|
|
} else {
|
|
app.get("*all", (req, res) => res.status(500).type('html').send(devModeHtml));
|
|
}
|
|
|
|
const run = async () => {
|
|
await db.sync({alter: true, force: false});
|
|
|
|
await initializeIntegrations();
|
|
|
|
await requestInterfaces();
|
|
setInterval(() => requestInterfaces(), 3600000);
|
|
|
|
if (process.env.PREVIEW_MODE !== "true") await loadCli();
|
|
|
|
await config.insertDefaults();
|
|
|
|
timerTask.startTimer(await config.getValue("cron"));
|
|
setInterval(async () => removeOld(), 60000);
|
|
|
|
integrationTask.startTimer();
|
|
if (process.env.RUN_TEST_ON_STARTUP === "true") {
|
|
timerTask.runTask().then(undefined);
|
|
}
|
|
|
|
app.listen(port, () => console.log(`Server listening on port ${port}`));
|
|
}
|
|
|
|
db.authenticate().then(() => {
|
|
console.log("Successfully connected to the database " + (process.env.DB_TYPE === "mysql" ? "server" : "file"));
|
|
run().then(undefined);
|
|
}).catch(err => {
|
|
console.error("Could not open the database file. Maybe it is damaged?: " + err.message);
|
|
process.exit(111);
|
|
}); |