From 30c2cd4732cf4d2e2a24ecdcb043646bc63fcc4b Mon Sep 17 00:00:00 2001 From: Spacelord Date: Mon, 1 May 2023 00:07:13 +0200 Subject: [PATCH] Fix routing. --- README.md | 5 ++- src/index.ts | 80 +++-------------------------------- src/routes/api/index.ts | 6 ++- src/routes/api/test.ts | 5 ++- src/routes/dev/index.ts | 14 ++++++ src/routes/dev/setDemoData.ts | 52 +++++++++++++++++++++++ src/routes/frontend/index.ts | 5 ++- src/routes/frontend/test.ts | 2 +- src/routes/index.ts | 8 +++- src/routes/static/index.ts | 30 +++++++++++++ 10 files changed, 125 insertions(+), 82 deletions(-) create mode 100644 src/routes/dev/index.ts create mode 100644 src/routes/dev/setDemoData.ts create mode 100644 src/routes/static/index.ts diff --git a/README.md b/README.md index db4ad25..e753842 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ -# Assetflow \ No newline at end of file +# Assetflow + +## Theme? +https://bootswatch.com/darkly/ diff --git a/src/index.ts b/src/index.ts index 70e7b58..42d912c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import { PrismaClient } from '@prisma/client'; import { Status, Category } from '@prisma/client'; import * as Path from 'path'; import * as fs from 'fs'; -import routes from './routes/index.js' +import routes from './routes/index.js'; // Get app directory. const __path = process.argv[1]; @@ -20,7 +20,7 @@ const logger_settings = { }; const coreLogger = new Signale(logger_settings); -const log = { +export const log = { core: coreLogger, db: coreLogger.scope('DB'), web: coreLogger.scope('WEB') @@ -33,7 +33,7 @@ const config = new ConfigHandler(__path + '/config.json', { http_port: 3000 }); -const prisma = new PrismaClient({ +export const prisma = new PrismaClient({ datasources: { db: { url: config.global.db_connection_string @@ -41,57 +41,9 @@ const prisma = new PrismaClient({ } }); -const app = express(); - -app.get('/dev/fillWithDemoData', (req, res) => { - // fill database with demo data - prisma.StorageBuilding.create({ - data: { - name: "Test Storage Building", - street: "Test Street", - houseNumber: "1", - zipCode: "12345", - city: "Test City", - country: "Test Country", - } - }).then(() => { - prisma.StorageLocation.create({ - data: { - name: "Test Storage Location", - StorageBuilding: { - connect: { - id: 1 - } - } - } - }).then(() => { - prisma.item - .create({ - data: { - SKU: 'ee189749', - Amount: 1, - name: 'Test Item', - manufacturer: 'Test Manufacturer', - category: Category.Other, - status: Status.normal, - storageLocation: { - connect: { - id: 1 - } - }, - } - }) - .then(() => { - res.send('Demo data added'); - }) - .catch((err) => { - res.send('Error adding demo data: ' + err); - }); - }) - }) - res.send('Demo data added (not)'); -}); +export const app = express(); +/* app.get('/:id', (req, res) => { // retrieve data from database using id from url prisma.item @@ -110,27 +62,7 @@ app.get('/:id', (req, res) => { } }); }); - -// Load from allowsStaticPaths.json file -const allowedURLs: Array = JSON.parse(fs.readFileSync("allowedStaticPaths.json", "utf8")).allowedStaticFiles; -const recordedURLs: Array = []; -const debugMode: boolean = JSON.parse(fs.readFileSync("allowedStaticPaths.json", "utf8")).debugMode; - -app.use('/static/*', function handleModuleFiles(req: Request, res: Response) { - if(debugMode) { - res.sendFile(Path.join(__dirname, 'node_modules', req.params[0])); - recordedURLs.push(req.params[0]); - log.web.debug(recordedURLs); - } else { - if (allowedURLs.indexOf(req.params[0]) > -1) { - res.sendFile(Path.join(__dirname, 'node_modules', req.params[0])); - } else { - log.web.warn('Attempt to access restricted asset file ' + req.params[0]); - res.status(403).json({ status: 'error', reason: 'Access to restricted asset file denied' }); - } - } - // console.log(recordedURLs) -}); + */ routes(app); diff --git a/src/routes/api/index.ts b/src/routes/api/index.ts index ba70e09..f4603a1 100644 --- a/src/routes/api/index.ts +++ b/src/routes/api/index.ts @@ -1,9 +1,11 @@ import express from 'express'; -const Router = express.Router(); +// Route imports import testRoute from './test.js'; -Router.use("/api/test", testRoute) +// Router base is '/api' +const Router = express.Router(); +Router.use('/test', testRoute); export default Router; diff --git a/src/routes/api/test.ts b/src/routes/api/test.ts index 2342ff1..4490bdf 100644 --- a/src/routes/api/test.ts +++ b/src/routes/api/test.ts @@ -1,4 +1,5 @@ import express, { Request, Response } from 'express'; + export default (req: Request, res: Response) => { - res.status(200).send("Test Successful!"); -}; \ No newline at end of file + res.status(200).send('API Test Successful!'); +}; diff --git a/src/routes/dev/index.ts b/src/routes/dev/index.ts new file mode 100644 index 0000000..e86d444 --- /dev/null +++ b/src/routes/dev/index.ts @@ -0,0 +1,14 @@ +import express from 'express'; + +// Route imports +import setDemoData from './setDemoData.js'; + +// Router base is '/dev' +const Router = express.Router(); + + + +Router.use("/setDemoData", setDemoData) + + +export default Router; diff --git a/src/routes/dev/setDemoData.ts b/src/routes/dev/setDemoData.ts new file mode 100644 index 0000000..a85a1d8 --- /dev/null +++ b/src/routes/dev/setDemoData.ts @@ -0,0 +1,52 @@ +import { Request, Response } from 'express'; +import { prisma } from '../../index.js'; +export default (req: Request, res: Response) => { + /* + // fill database with demo data + prisma.StorageBuilding.create({ + data: { + name: 'Test Storage Building', + street: 'Test Street', + houseNumber: '1', + zipCode: '12345', + city: 'Test City', + country: 'Test Country' + } + }).then(() => { + prisma.StorageLocation.create({ + data: { + name: 'Test Storage Location', + StorageBuilding: { + connect: { + id: 1 + } + } + } + }).then(() => { + prisma.item + .create({ + data: { + SKU: 'ee189749', + Amount: 1, + name: 'Test Item', + manufacturer: 'Test Manufacturer', + category: Category.Other, + status: Status.normal, + storageLocation: { + connect: { + id: 1 + } + } + } + }) + .then(() => { + res.send('Demo data added'); + }) + .catch((err) => { + res.send('Error adding demo data: ' + err); + }); + }); + }); + */ + res.send('Demo data added (not)'); +}; diff --git a/src/routes/frontend/index.ts b/src/routes/frontend/index.ts index 66cd925..0ae37c9 100644 --- a/src/routes/frontend/index.ts +++ b/src/routes/frontend/index.ts @@ -1,8 +1,11 @@ import express from 'express'; -const Router = express.Router(); +// Route imports import testRoute from './test.js'; +// Router base is '/' +const Router = express.Router(); + Router.use("/test", testRoute) export default Router; diff --git a/src/routes/frontend/test.ts b/src/routes/frontend/test.ts index 2342ff1..868dda5 100644 --- a/src/routes/frontend/test.ts +++ b/src/routes/frontend/test.ts @@ -1,4 +1,4 @@ import express, { Request, Response } from 'express'; export default (req: Request, res: Response) => { - res.status(200).send("Test Successful!"); + res.status(200).send("Frontend Test Successful!"); }; \ No newline at end of file diff --git a/src/routes/index.ts b/src/routes/index.ts index 226bc2c..bbca668 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,8 +1,14 @@ import { Express } from 'express'; + +// Route imports import frontend_routes from './frontend/index.js'; -import api_routes from './frontend/index.js'; +import static_routes from './frontend/index.js'; +import api_routes from './api/index.js'; +import dev_routes from './frontend/index.js'; export default (app: Express) => { app.use('/', frontend_routes); + app.use('/static', static_routes); app.use('/api', api_routes); + app.use('/dev', dev_routes); // This is just for development. ToDo: Add check if we are in devmode. }; diff --git a/src/routes/static/index.ts b/src/routes/static/index.ts new file mode 100644 index 0000000..ae8366a --- /dev/null +++ b/src/routes/static/index.ts @@ -0,0 +1,30 @@ +import express, { Request, Response } from 'express'; +import * as fs from 'fs'; +import * as Path from 'path'; +import { log } from '../../index.js'; + +// Router base is '/static' +const Router = express.Router(); + +// Load from allowsStaticPaths.json file +const allowedURLs: Array = JSON.parse(fs.readFileSync('allowedStaticPaths.json', 'utf8')).allowedStaticFiles; +const recordedURLs: Array = []; +const debugMode: boolean = JSON.parse(fs.readFileSync('allowedStaticPaths.json', 'utf8')).debugMode; + +Router.use('/*', (req: Request, res: Response) => { + if (debugMode) { + res.sendFile(Path.join(__dirname, 'node_modules', req.params[0])); + recordedURLs.push(req.params[0]); + log.web.debug(recordedURLs); + } else { + if (allowedURLs.indexOf(req.params[0]) > -1) { + res.sendFile(Path.join(__dirname, 'node_modules', req.params[0])); + } else { + log.web.warn('Attempt to access restricted asset file ' + req.params[0]); + res.status(403).json({ status: 'error', reason: 'Access to restricted asset file denied' }); + } + } + // console.log(recordedURLs) +}); + +export default Router;