add maintenance binding routes
parent
f76c2c4da3
commit
7e2339cbc8
|
@ -37,6 +37,8 @@ export default class Server {
|
||||||
const route = new (require(`${__dirname}/routes/${routeFile}`).default)(this);
|
const route = new (require(`${__dirname}/routes/${routeFile}`).default)(this);
|
||||||
if (route.conf.deprecated === true) {
|
if (route.conf.deprecated === true) {
|
||||||
route.deprecated();
|
route.deprecated();
|
||||||
|
} else if (route.conf.maintenance === true) {
|
||||||
|
route.maintenance();
|
||||||
} else {
|
} else {
|
||||||
route.bind();
|
route.bind();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ export default class Route {
|
||||||
|
|
||||||
public conf: { path: string, deprecated?: boolean };
|
public conf: { path: string, deprecated?: boolean };
|
||||||
|
|
||||||
constructor(server: Server, conf: { path: string, deprecated?: boolean }) {
|
constructor(server: Server, conf: { path: string, deprecated?: boolean, maintenance?: boolean }) {
|
||||||
this.server = server;
|
this.server = server;
|
||||||
this.router = router();
|
this.router = router();
|
||||||
this.conf = conf;
|
this.conf = conf;
|
||||||
|
@ -19,20 +19,26 @@ export default class Route {
|
||||||
|
|
||||||
public deprecated() {
|
public deprecated() {
|
||||||
this.router.all('*', (_req, res) => {
|
this.router.all('*', (_req, res) => {
|
||||||
res.status(501).json({ code: this.constants.codes.DEPRECATED, message: 'This endpoint is deprecated.' });
|
res.status(501).json({ code: this.constants.codes.DEPRECATED, message: this.constants.messages.DEPRECATED });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public maintenance() {
|
||||||
|
this.router.all('*;', (_req, res) => {
|
||||||
|
res.status(503).json({ code: this.constants.codes.MAINTENANCE_OR_UNAVAILABLE, message: this.constants.messages.MAINTENANCE_OR_UNAVAILABLE });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async authorize(req: Request, res: Response, next: NextFunction) {
|
public async authorize(req: Request, res: Response, next: NextFunction) {
|
||||||
const account = await this.server.security.checkBearer(this.server.security.extractBearer(req));
|
const account = await this.server.security.checkBearer(this.server.security.extractBearer(req));
|
||||||
if (!account) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: 'BEARER_TOKEN_INVALID' });
|
if (!account) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
|
||||||
Object.defineProperty(req, 'account', { value: account, writable: true, enumerable: true, configurable: true });
|
Object.defineProperty(req, 'account', { value: account, writable: true, enumerable: true, configurable: true });
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|
||||||
public handleError(error: Error, res: Response) {
|
public handleError(error: Error, res: Response) {
|
||||||
this.server.client.util.handleError(error);
|
this.server.client.util.handleError(error);
|
||||||
res.status(500).json({ code: this.constants.codes.SERVER_ERROR, message: 'An internal error has occurred, Engineers have been notified.' });
|
res.status(500).json({ code: this.constants.codes.SERVER_ERROR, message: this.constants.messages.SERVER_ERROR });
|
||||||
}
|
}
|
||||||
|
|
||||||
get constants() {
|
get constants() {
|
||||||
|
@ -46,6 +52,15 @@ export default class Route {
|
||||||
CLIENT_ERROR: 1044,
|
CLIENT_ERROR: 1044,
|
||||||
SERVER_ERROR: 105,
|
SERVER_ERROR: 105,
|
||||||
DEPRECATED: 1051,
|
DEPRECATED: 1051,
|
||||||
|
MAINTENANCE_OR_UNAVAILABLE: 1053,
|
||||||
|
},
|
||||||
|
messages: {
|
||||||
|
UNAUTHORIZED: ['CREDENTIALS_INVALID', 'The credentials you supplied are invalid.'],
|
||||||
|
PERMISSION_DENIED: ['PERMISSION_DENIED', 'You do not have valid credentials to access this resource.'],
|
||||||
|
NOT_FOUND: ['NOT_FOUND', 'The resource you requested cannot be located.'],
|
||||||
|
SERVER_ERROR: ['INTERNAL_ERROR', 'An internal error has occurred, Engineers have been notified.'],
|
||||||
|
DEPRECATED: ['ENDPOINT_OR_RESOURCE_DEPRECATED', 'The endpoint or resource you\'re trying to access has been deprecated.'],
|
||||||
|
MAINTENANCE_OR_UNAVAILABLE: ['SERVICE_UNAVAILABLE', 'The endpoint or resource you\'re trying to access is either in maintenance or is not available.'],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue