Add server service, update returned info

This commit is contained in:
rijkvanzanten
2020-09-01 13:08:52 -04:00
parent 41d5f131d3
commit d7d3de86d2
5 changed files with 58 additions and 5 deletions

View File

@@ -1,8 +1,14 @@
import { Router } from 'express';
import ServerService from '../services/server';
const router = Router();
router.get('/ping', (req, res) => res.send('pong'));
router.get('/info', (req, res) => res.json({ data: process.versions }));
router.get('/info', (req, res) => {
const service = new ServerService({ accountability: req.accountability });
const data = service.serverInfo();
res.json({ data });
});
export default router;

View File

@@ -0,0 +1,39 @@
import ItemsService from './items';
import { AbstractServiceOptions, Accountability } from '../types';
import Knex from 'knex';
import database from '../database';
import os from 'os';
import { ForbiddenException } from '../exceptions';
import { version } from '../../package.json';
export default class ServerService {
knex: Knex;
accountability: Accountability | null;
constructor(options?: AbstractServiceOptions) {
this.knex = options?.knex || database;
this.accountability = options?.accountability || null;
}
serverInfo() {
if (this.accountability?.admin !== true) {
throw new ForbiddenException();
}
return {
directus: {
version,
},
node: {
version: process.versions.node,
uptime: Math.round(process.uptime()),
},
os: {
type: os.type(),
version: os.release(),
uptime: Math.round(os.uptime()),
totalmen: os.totalmem(),
}
}
}
}

View File

@@ -11,6 +11,7 @@
"lib": [
"es2019"
],
"skipLibCheck": true
"skipLibCheck": true,
"resolveJsonModule": true,
}
}