140 lines
3.4 KiB
TypeScript
140 lines
3.4 KiB
TypeScript
|
import { Signale } from 'signale';
|
||
|
import ConfigHandler from './assets/configHandler';
|
||
|
import express, { Request, Response } from 'express';
|
||
|
import * as Eta from 'eta';
|
||
|
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'
|
||
|
|
||
|
// Get app directory.
|
||
|
const __path = process.argv[1];
|
||
|
|
||
|
const logger_settings = {
|
||
|
disabled: false,
|
||
|
logLevel: 'info',
|
||
|
scope: 'Core',
|
||
|
stream: process.stdout,
|
||
|
displayFilename: true
|
||
|
};
|
||
|
|
||
|
const coreLogger = new Signale(logger_settings);
|
||
|
const log = {
|
||
|
core: coreLogger,
|
||
|
db: coreLogger.scope('DB'),
|
||
|
web: coreLogger.scope('WEB')
|
||
|
};
|
||
|
|
||
|
// Create a new config instance.
|
||
|
const config = new ConfigHandler(__path + '/config.json', {
|
||
|
db_connection_string: 'mysql://USER:PASSWORD@HOST:3306/DATABASE',
|
||
|
http_listen_address: '127.0.0.1',
|
||
|
http_port: 3000
|
||
|
});
|
||
|
|
||
|
const prisma = new PrismaClient({
|
||
|
datasources: {
|
||
|
db: {
|
||
|
url: config.global.db_connection_string
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
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)');
|
||
|
});
|
||
|
|
||
|
app.get('/:id', (req, res) => {
|
||
|
// retrieve data from database using id from url
|
||
|
prisma.item
|
||
|
.findFirst({
|
||
|
where: {
|
||
|
SKU: req.params.id
|
||
|
}
|
||
|
})
|
||
|
.then((item) => {
|
||
|
if (item) {
|
||
|
Eta.renderFile(__path + '/src/frontend/publicInfoPage.eta.html', item).then((html) => {
|
||
|
res.send(html);
|
||
|
});
|
||
|
} else {
|
||
|
res.send('Item not found');
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
|
||
|
// 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);
|
||
|
|
||
|
app.listen(config.global.http_port, config.global.http_listen_address, () => {
|
||
|
log.web.info(`Listening at http://${config.global.http_listen_address}:${config.global.http_port}`);
|
||
|
});
|