|
|
@@ -4,6 +4,7 @@ import { HttpStatusException } from '../../common/lib/http-status.exception';
|
|
|
import { Logger } from '../../common/util/logger.class';
|
|
|
|
|
|
import { Database } from './database.class';
|
|
|
+import { ServerAPIHandler } from './webhdl/server-api-handler.class';
|
|
|
|
|
|
export class Webserver {
|
|
|
private app: Express;
|
|
|
@@ -11,60 +12,8 @@ export class Webserver {
|
|
|
constructor(private port: number, private db: Database) {
|
|
|
this.app = express();
|
|
|
|
|
|
- this.app.get('/server', async (req, res, next) => {
|
|
|
- try {
|
|
|
- const serverConfigs = await this.db.getAllServerConfigs();
|
|
|
- res.send(serverConfigs);
|
|
|
- } catch (err) {
|
|
|
- next(err);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- this.app.get('/server/:serverID/data/types', async (req, res, next) => {
|
|
|
- try {
|
|
|
- const serverID = Number(req.params.serverID);
|
|
|
-
|
|
|
- if (Number.isNaN(serverID)) {
|
|
|
- throw new HttpStatusException(`Not a valid server id: ${req.params.serverID}`, 400);
|
|
|
- }
|
|
|
-
|
|
|
- const serverDataTypes = await this.db.getServerDataTypes(serverID);
|
|
|
- res.send(serverDataTypes);
|
|
|
- } catch (err) {
|
|
|
- next(err);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- this.app.get('/server/:serverID/data', async (req, res, next) => {
|
|
|
- try {
|
|
|
- const serverID = Number(req.params.serverID);
|
|
|
-
|
|
|
- if (Number.isNaN(serverID)) {
|
|
|
- throw new HttpStatusException(`Not a valid server id: ${req.params.serverID}`, 400);
|
|
|
- }
|
|
|
-
|
|
|
- const qStart = (req.query.start || '').toString();
|
|
|
- const qEnd = (req.query.end || '').toString();
|
|
|
- const qType = (req.query.type || '').toString();
|
|
|
-
|
|
|
- if (!qStart || !qEnd || !qType) throw new HttpStatusException("QueryParams 'type', 'start' and 'end' are mandatory.", 400);
|
|
|
-
|
|
|
- const start = new Date(qStart);
|
|
|
- const end = new Date(qEnd);
|
|
|
- if ([start.toString(), end.toString()].includes('Invalid Date')) {
|
|
|
- throw new HttpStatusException("QueryParams 'start' and 'end' must be parseable dates or unix epoch timestamps (ms).", 400);
|
|
|
- }
|
|
|
-
|
|
|
- const data = await this.db.queryServerData(serverID, qType, start, end);
|
|
|
- res.send({
|
|
|
- start,
|
|
|
- end,
|
|
|
- data
|
|
|
- } as QueryResponse<ServerData>);
|
|
|
- } catch (err) {
|
|
|
- next(err);
|
|
|
- }
|
|
|
- });
|
|
|
+ const serverApi = new ServerAPIHandler(db);
|
|
|
+ this.app.use('/server', serverApi.router);
|
|
|
|
|
|
this.app.use('/', express.static(process.env.STATIC_DIR || 'public'));
|
|
|
|