Fix routing.
This commit is contained in:
parent
18a4393765
commit
30c2cd4732
80
src/index.ts
80
src/index.ts
@ -6,7 +6,7 @@ import { PrismaClient } from '@prisma/client';
|
|||||||
import { Status, Category } from '@prisma/client';
|
import { Status, Category } from '@prisma/client';
|
||||||
import * as Path from 'path';
|
import * as Path from 'path';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import routes from './routes/index.js'
|
import routes from './routes/index.js';
|
||||||
|
|
||||||
// Get app directory.
|
// Get app directory.
|
||||||
const __path = process.argv[1];
|
const __path = process.argv[1];
|
||||||
@ -20,7 +20,7 @@ const logger_settings = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const coreLogger = new Signale(logger_settings);
|
const coreLogger = new Signale(logger_settings);
|
||||||
const log = {
|
export const log = {
|
||||||
core: coreLogger,
|
core: coreLogger,
|
||||||
db: coreLogger.scope('DB'),
|
db: coreLogger.scope('DB'),
|
||||||
web: coreLogger.scope('WEB')
|
web: coreLogger.scope('WEB')
|
||||||
@ -33,7 +33,7 @@ const config = new ConfigHandler(__path + '/config.json', {
|
|||||||
http_port: 3000
|
http_port: 3000
|
||||||
});
|
});
|
||||||
|
|
||||||
const prisma = new PrismaClient({
|
export const prisma = new PrismaClient({
|
||||||
datasources: {
|
datasources: {
|
||||||
db: {
|
db: {
|
||||||
url: config.global.db_connection_string
|
url: config.global.db_connection_string
|
||||||
@ -41,57 +41,9 @@ const prisma = new PrismaClient({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const app = express();
|
export 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) => {
|
app.get('/:id', (req, res) => {
|
||||||
// retrieve data from database using id from url
|
// retrieve data from database using id from url
|
||||||
prisma.item
|
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);
|
routes(app);
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
const Router = express.Router();
|
|
||||||
|
|
||||||
|
// Route imports
|
||||||
import testRoute from './test.js';
|
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;
|
export default Router;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import express, { Request, Response } from 'express';
|
import express, { Request, Response } from 'express';
|
||||||
|
|
||||||
export default (req: Request, res: Response) => {
|
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
14
src/routes/dev/index.ts
Normal 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;
|
52
src/routes/dev/setDemoData.ts
Normal file
52
src/routes/dev/setDemoData.ts
Normal 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)');
|
||||||
|
};
|
@ -1,8 +1,11 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
const Router = express.Router();
|
|
||||||
|
|
||||||
|
// Route imports
|
||||||
import testRoute from './test.js';
|
import testRoute from './test.js';
|
||||||
|
|
||||||
|
// Router base is '/'
|
||||||
|
const Router = express.Router();
|
||||||
|
|
||||||
Router.use("/test", testRoute)
|
Router.use("/test", testRoute)
|
||||||
|
|
||||||
export default Router;
|
export default Router;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import express, { Request, Response } from 'express';
|
import express, { Request, Response } from 'express';
|
||||||
export default (req: Request, res: Response) => {
|
export default (req: Request, res: Response) => {
|
||||||
res.status(200).send("Test Successful!");
|
res.status(200).send("Frontend Test Successful!");
|
||||||
};
|
};
|
@ -1,8 +1,14 @@
|
|||||||
import { Express } from 'express';
|
import { Express } from 'express';
|
||||||
|
|
||||||
|
// Route imports
|
||||||
import frontend_routes from './frontend/index.js';
|
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) => {
|
export default (app: Express) => {
|
||||||
app.use('/', frontend_routes);
|
app.use('/', frontend_routes);
|
||||||
|
app.use('/static', static_routes);
|
||||||
app.use('/api', api_routes);
|
app.use('/api', api_routes);
|
||||||
|
app.use('/dev', dev_routes); // This is just for development. ToDo: Add check if we are in devmode.
|
||||||
};
|
};
|
||||||
|
30
src/routes/static/index.ts
Normal file
30
src/routes/static/index.ts
Normal 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;
|
Loading…
Reference in New Issue
Block a user