Skip to content

Commit

Permalink
feat: Controller for supporting plugin pre/post
Browse files Browse the repository at this point in the history
- Refactor every controller
  - Extend `Controller`

1. do pre-process
2. do main process
3. do post-process
  • Loading branch information
Chinlinlee committed Jan 4, 2023
1 parent 7e57425 commit 6248724
Show file tree
Hide file tree
Showing 18 changed files with 879 additions and 498 deletions.
47 changes: 47 additions & 0 deletions api/controller.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { pluginGroup, LocalPlugin } = require("../plugins/plugin.class");

class Controller {

/**
*
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
*/
constructor(req, res) {
this.request = req;
this.response = res;
}

async preProcess() {
let currentRouterPlugin = pluginGroup.findLocalPlugin(this.request.originalUrl, this.request.method) || new LocalPlugin();

try {

for(let preFn of currentRouterPlugin.preFns) {
await preFn(this.request, this.response);
}

} catch(e) {
throw new Error(`pre process error in path "${this.request.originalUrl}", ${e.message}`);
}

}

async mainProcess() {}

async postProcess() {
let currentRouterPlugin = pluginGroup.findLocalPlugin(this.request.url, this.request.method) || new LocalPlugin();

try {

for(let postFn of currentRouterPlugin.postFns) {
await postFn(this.request, this.response);
}

} catch(e) {
throw new Error(`post process error in path "${this.request.originalUrl}", ${e.message}`);
}
}
}

module.exports.Controller = Controller;
45 changes: 35 additions & 10 deletions api/dicom-web/controller/QIDO-RS/queryAllInstances.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,48 @@ const {
QidoRsService
} = require("./service/QIDO-RS.service");
const { ApiLogger } = require("../../../../utils/logs/api-logger");
const { Controller } = require("../../../controller.class");

class QueryAllInstancesController extends Controller {
constructor(req, res) {
super(req, res);
}

async mainProcess() {
let apiLogger = new ApiLogger(this.request, "QIDO-RS");

apiLogger.info("[Query all instances]");

try {
let qidoRsService = new QidoRsService(this.request, this.response, "instance");

await qidoRsService.getAndResponseDicomJson();
} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
apiLogger.error(errorStr);

this.response.writeHead(500, {
"Content-Type": "application/dicom+json"
});
this.response.end(JSON.stringify({
code: 500,
message: errorStr
}));
}
}
}

/**
*
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
*/
module.exports = async function (req, res) {
let apiLogger = new ApiLogger(req, "QIDO-RS");
let controller = new QueryAllInstancesController(req, res);

apiLogger.info("[Query all instances]");

try {
let qidoRsService = new QidoRsService(req, res, "instance");
await controller.preProcess();

await qidoRsService.getAndResponseDicomJson();
} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
apiLogger.error(errorStr);
}
await controller.mainProcess();

controller.postProcess();
};
47 changes: 36 additions & 11 deletions api/dicom-web/controller/QIDO-RS/queryAllSeries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,49 @@ const {
QidoRsService
} = require("./service/QIDO-RS.service");
const { ApiLogger } = require("../../../../utils/logs/api-logger");
const { Controller } = require("../../../controller.class");

class QueryAllSeriesController extends Controller {
constructor(req, res) {
super(req, res);
}

async mainProcess() {
let apiLogger = new ApiLogger(this.request, "QIDO-RS");

apiLogger.info("[Query all series]");

try {

let qidoRsService = new QidoRsService(this.request, this.response, "series");

await qidoRsService.getAndResponseDicomJson();

} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
apiLogger.error(errorStr);

this.response.writeHead(500, {
"Content-Type": "application/dicom+json"
});
this.response.end(JSON.stringify({
code: 500,
message: errorStr
}));
}
}
}
/**
*
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
*/
module.exports = async function (req, res) {
let apiLogger = new ApiLogger(req, "QIDO-RS");
let controller = new QueryAllSeriesController(req, res);

apiLogger.info("[Query all series]");

try {

let qidoRsService = new QidoRsService(req, res, "series");
await controller.preProcess();

await qidoRsService.getAndResponseDicomJson();
await controller.mainProcess();

} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
apiLogger.error(errorStr);
}
controller.postProcess();
};
49 changes: 37 additions & 12 deletions api/dicom-web/controller/QIDO-RS/queryAllStudies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,51 @@ const {
QidoRsService
} = require("./service/QIDO-RS.service");
const { ApiLogger } = require("../../../../utils/logs/api-logger");
const { Controller } = require("../../../controller.class");

class QueryAllStudiesController extends Controller {
constructor(req, res) {
super(req, res);
}

async mainProcess() {
let apiLogger = new ApiLogger(this.request, "QIDO-RS");

apiLogger.info(`[Query All Studies]`);

try {

let qidoRsService = new QidoRsService(this.request, this.response, "study");

await qidoRsService.getAndResponseDicomJson();

} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
apiLogger.error(errorStr);

this.response.writeHead(500, {
"Content-Type": "application/dicom+json"
});
this.response.end(JSON.stringify({
code: 500,
message: errorStr
}));
}
}
}

/**
*
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
*/
module.exports = async function (req, res) {
let apiLogger = new ApiLogger(req, "QIDO-RS");

apiLogger.info(`[Query All Studies]`);

try {
let controller = new QueryAllStudiesController(req, res);

let qidoRsService = new QidoRsService(req, res, "study");
await controller.preProcess();

await qidoRsService.getAndResponseDicomJson();

} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
apiLogger.error(errorStr);
}
await controller.mainProcess();

controller.postProcess();
};

45 changes: 35 additions & 10 deletions api/dicom-web/controller/QIDO-RS/queryStudies-Instances.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,49 @@ const {
QidoRsService
} = require("./service/QIDO-RS.service");
const { ApiLogger } = require("../../../../utils/logs/api-logger");
const { Controller } = require("../../../controller.class");

class QueryInstancesOfStudiesController extends Controller {
constructor(req, res) {
super(req, res);
}

async mainProcess() {
let apiLogger = new ApiLogger(this.request, "QIDO-RS");

apiLogger.info(`[Query instances in study, Study UID: ${this.request.params.studyUID}]`);

try {

let qidoRsService = new QidoRsService(this.request, this.response, "instance");

await qidoRsService.getAndResponseDicomJson();
} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
apiLogger.error(errorStr);

this.response.writeHead(500, {
"Content-Type": "application/dicom+json"
});
this.response.end(JSON.stringify({
code: 500,
message: errorStr
}));
}
}
}

/**
*
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
*/
module.exports = async function (req, res) {
let apiLogger = new ApiLogger(req, "QIDO-RS");
let controller = new QueryInstancesOfStudiesController(req, res);

apiLogger.info(`[Query instances in study, Study UID: ${req.params.studyUID}]`);
await controller.preProcess();

try {
await controller.mainProcess();

let qidoRsService = new QidoRsService(req, res, "instance");

await qidoRsService.getAndResponseDicomJson();
} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
apiLogger.error(errorStr);
}
controller.postProcess();
};
47 changes: 36 additions & 11 deletions api/dicom-web/controller/QIDO-RS/queryStudies-Series-Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,50 @@ const {
QidoRsService
} = require("./service/QIDO-RS.service");
const { ApiLogger } = require("../../../../utils/logs/api-logger");
const { Controller } = require("../../../controller.class");

class QueryInstancesOfSeriesOfStudiesController extends Controller {
constructor(req, res) {
super(req, res);
}

async mainProcess() {
let apiLogger = new ApiLogger(this.request, "QIDO-RS");

apiLogger.info(`[Query instance Level, Study UID: ${this.request.params.studyUID}, Series UID: ${this.request.params.seriesUID}]`);

try {

let qidoRsService = new QidoRsService(this.request, this.response, "instance");

await qidoRsService.getAndResponseDicomJson();

} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
apiLogger.error(errorStr);

this.response.writeHead(500, {
"Content-Type": "application/dicom+json"
});
this.response.end(JSON.stringify({
code: 500,
message: errorStr
}));
}
}
}

/**
*
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
*/
module.exports = async function (req, res) {
let apiLogger = new ApiLogger(req, "QIDO-RS");
let controller = new QueryInstancesOfSeriesOfStudiesController(req, res);

apiLogger.info(`[Query instance Level, Study UID: ${req.params.studyUID}, Series UID: ${req.params.seriesUID}]`);

try {

let qidoRsService = new QidoRsService(req, res, "instance");
await controller.preProcess();

await qidoRsService.getAndResponseDicomJson();
await controller.mainProcess();

} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
apiLogger.error(errorStr);
}
controller.postProcess();
};
Loading

0 comments on commit 6248724

Please sign in to comment.