Fix routing.

Fix routing again..
This commit is contained in:
Leon Meier 2023-05-01 00:07:13 +02:00
parent 18a4393765
commit ed361cc5eb
10 changed files with 125 additions and 82 deletions

View File

@ -1 +1,4 @@
# Assetflow
## Theme?
https://bootswatch.com/darkly/

View File

@ -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<string> = JSON.parse(fs.readFileSync("allowedStaticPaths.json", "utf8")).allowedStaticFiles;
const recordedURLs: Array<string> = [];
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);

View File

@ -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;

View File

@ -1,4 +1,5 @@
import express, { Request, Response } from 'express';
export default (req: Request, res: Response) => {
res.status(200).send("Test Successful!");
res.status(200).send('API Test Successful!');
};

14
src/routes/dev/index.ts Normal file
View File

@ -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;

View File

@ -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)');
};

View File

@ -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;

View File

@ -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!");
};

View File

@ -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.
};

View File

@ -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<string> = JSON.parse(fs.readFileSync('allowedStaticPaths.json', 'utf8')).allowedStaticFiles;
const recordedURLs: Array<string> = [];
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;