fix some routing

This commit is contained in:
DIVISIONSolar 2024-03-14 11:47:23 -04:00
parent c738a7d69d
commit 2f91709179
Signed by: JoshS
GPG Key ID: A37D037686151E97
6 changed files with 83 additions and 14 deletions

View File

@ -1 +1,2 @@
API_KEYS=key1,key2,key3
API_KEYS=key1,key2,key3
#PORT=

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules
.env
src/Assets/urls.json

2
src/Assets/ads.txt Normal file
View File

@ -0,0 +1,2 @@
#AdSense Ads.txt for pub-9097891430264009
google.com, pub-9097891430264009, DIRECT, f08c47fec0942fa0

19
src/Routes/ads.js Normal file
View File

@ -0,0 +1,19 @@
const express = require('express');
const router = express.Router();
const fs = require('fs');
router.get('/ads.txt', (req, res) => {
fs.readFile('src/Assets/ads.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
res.status(500).send('Internal Server Error');
return;
}
res.type('text/plain').send(data);
});
});
module.exports = {
basePath: '/',
router: router
};

21
src/Routes/urls.js Normal file
View File

@ -0,0 +1,21 @@
const express = require('express');
const router = express.Router();
const fs = require('fs');
router.get('/urls.json', (req, res) => {
fs.readFile('src/Assets/urls.json', 'utf8', (err, data) => {
if (err) {
console.error(err);
res.status(500).send('Internal Server Error');
return;
}
const urls = JSON.parse(data);
res.json(urls);
});
});
module.exports = {
basePath: '/',
router: router
};

View File

@ -1,11 +1,10 @@
require('dotenv').config();
const express = require('express');
const fs = require('fs');
const app = express();
const PORT = process.env.PORT || 3000;
const API_KEYS = process.env.API_KEYS.split(',');
const urlsRouter = require('./Routes/urls');
const adsRouter = require('./Routes/ads');
app.use(express.static('public'));
@ -19,19 +18,45 @@ const checkApiKey = (req, res, next) => {
app.use(checkApiKey);
app.get('/urls.json', (req, res) => {
fs.readFile('src/Assets/urls.json', 'utf8', (err, data) => {
if (err) {
console.error(err);
res.status(500).send('Internal Server Error');
return;
}
const allRoutes = [
urlsRouter,
adsRouter
];
const urls = JSON.parse(data);
res.json(urls);
});
allRoutes.forEach(({ basePath, router }) => {
app.use(basePath, router);
});
app.get('/', (req, res) => {
const routes = allRoutes.map(({ basePath, router }) => ({
path: basePath,
routes: listRoutes(router)
}));
res.json(routes);
});
function listRoutes(router) {
const routes = [];
router.stack.forEach((middleware) => {
if (middleware.route) {
let path = middleware.route.path;
if (path instanceof RegExp) {
path = middleware.regexp.toString();
}
if (path.startsWith('^/')) {
path = path.slice(2);
}
routes.push({
file: path.split('/')[1], // Extract file name from the path
methods: Object.keys(middleware.route.methods),
});
} else if (middleware.name === 'router') {
routes.push(...listRoutes(middleware.handle));
}
});
return routes;
}
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});