knopkem / dicomweb-proxy

A proxy to translate between dicomweb and traditional dicom dimse services (PACS communication)
Other
68 stars 19 forks source link

CORS Problem #54

Closed ferranalma3d closed 2 years ago

ferranalma3d commented 2 years ago

I'm having CORS problem with my viewer on another server

knopkem commented 2 years ago

in src/app.js it initializes fastify-cors There are quite a view options that you can try to set, depending on your specific setup.

knopkem commented 2 years ago

Can you specify how you deploy it?

ferranalma3d commented 2 years ago

const config = require('config'); const shell = require('shelljs'); const fs = require('fs'); const path = require('path'); const fastify = require('fastify')({ logger: false });

// make sure default directories exist shell.mkdir('-p', config.get('logDir')); shell.mkdir('-p', './data');

const utils = require('./utils.js');

fastify.register(require('fastify-static'), { root: path.join(__dirname, '../public'), });

fastify.register(require('fastify-cors'), { origin: "*" });

fastify.register(require('fastify-sensible'));

fastify.register(require('fastify-helmet'), { contentSecurityPolicy: false });

fastify.register(require('fastify-compress'), { global: true });

const logger = utils.getLogger();

// log exceptions process.on('uncaughtException', async (err) => { await logger.error('uncaught exception received:'); await logger.error(err.stack); });

//------------------------------------------------------------------

process.on('SIGINT', async () => { await logger.info('shutting down web server...'); fastify.close().then(async () => { await logger.info('webserver shutdown successfully'); }, (err) => { logger.error('webserver shutdown failed', err); }) if (!config.get('useCget')) { await logger.info('shutting down DICOM SCP server...'); await utils.shutdown(); } });

//------------------------------------------------------------------

fastify.get('/viewer/studies', async (req, reply) => { const tags = utils.studyLevelTags(); const json = await utils.doFind('STUDY', req.query, tags); reply.send(json); });

//------------------------------------------------------------------

fastify.get('/studies', async (req, reply) => { const tags = utils.studyLevelTags(); const json = await utils.doFind('STUDY', req.query, tags); reply.send(json); });

//------------------------------------------------------------------

fastify.get('/studies/:studyInstanceUid/metadata', async (req, reply) => { const { query } = req; query.StudyInstanceUID = req.params.studyInstanceUid; const tags = utils.seriesLevelTags(); const json = await utils.doFind('SERIES', query, tags); reply.send(json); });

//------------------------------------------------------------------

fastify.get('/studies/:studyInstanceUid/series', async (req, reply) => { const tags = utils.seriesLevelTags(); const { query } = req; query.StudyInstanceUID = req.params.studyInstanceUid;

const json = await utils.doFind('SERIES', query, tags); reply.send(json); });

//------------------------------------------------------------------

fastify.get('/studies/:studyInstanceUid/series/:seriesInstanceUid/instances', async (req, reply) => { const tags = utils.imageLevelTags(); const { query } = req; query.StudyInstanceUID = req.params.studyInstanceUid; query.SeriesInstanceUID = req.params.seriesInstanceUid;

const json = await utils.doFind('IMAGE', query, tags); reply.send(json); });

//------------------------------------------------------------------

fastify.get('/studies/:studyInstanceUid/series/:seriesInstanceUid/instances/:instancesUid/frames/:frameInstaceUid', async (req, reply) => { const tags = utils.imageLevelTags(); const { query } = req; query.StudyInstanceUID = req.params.studyInstanceUid; query.SeriesInstanceUID = req.params.seriesInstanceUid;

const json = await utils.doFind('IMAGE', query, tags); reply.send(json); });

//------------------------------------------------------------------

fastify.get('/studies/:studyInstanceUid/series/:seriesInstanceUid/metadata', async (req, reply) => { const tags = utils.imageLevelTags(); const { query } = req; query.StudyInstanceUID = req.params.studyInstanceUid; query.SeriesInstanceUID = req.params.seriesInstanceUid;

const json = await utils.doFind('IMAGE', query, tags); reply.send(json); });

//------------------------------------------------------------------

fastify.get('/wado', async (req, reply) => { const fetchLevel = config.get('useFetchLevel'); const studyUid = req.query.studyUID; const seriesUid = req.query.seriesUID; const imageUid = req.query.objectUID; if (!studyUid || !seriesUid || !imageUid) { const msg = Error missing parameters.; logger.error(msg); reply.code(500); reply.send(msg); return; } const storagePath = config.get('storagePath'); const studyPath = path.join(storagePath, studyUid); const pathname = path.join(studyPath, imageUid);

try { await utils.fileExists(pathname); } catch (error) { try { await utils.waitOrFetchData(studyUid, seriesUid, imageUid, fetchLevel); } catch (e) { logger.error(e); const msg = fetch failed; reply.code(500); reply.send(msg); return; } }

try { await utils.fileExists(pathname); } catch (error) { logger.error(error); const msg = file not found ${pathname}; reply.code(500); reply.send(msg); return; }

try { await utils.compressFile(pathname, studyPath); } catch (error) { logger.error(error); const msg = failed to compress ${pathname}; reply.code(500); reply.send(msg); return; }

// if the file is found, set Content-type and send data reply.header('Content-Type', 'application/dicom');

reply.header('Access-Control-Allow-Origin', '*'); reply.header('Access-Control-Allow-Headers', 'Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Request-Method'); reply.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE'); reply.header('Allow', 'GET, POST, OPTIONS, PUT, DELETE');

// read file from file system fs.readFile(pathname, (err, data) => { if (err) { const msg = Error getting the file: ${err}.; logger.error(msg); reply.setCode(500); reply.send(msg); } reply.send(data); }); });

//------------------------------------------------------------------

const port = config.get('webserverPort'); logger.info('starting...'); fastify.listen(port,'0.0.0.0', async (err, address) => { if (err) { await logger.error(err, address); process.exit(1); } logger.info(web-server listening on port: ${port});

await utils.init();

// if not using c-get, start our scp if (!config.get('useCget')) { utils.startScp(); } utils.sendEcho(); });

//------------------------------------------------------------------

ferranalma3d commented 2 years ago

image

knopkem commented 2 years ago

Hi, I actually meant how you host the dicomweb-proxy. Normally when run directly (npm start) there should be no CORS issues as the app and the requests are from/to the same host (including matching ports). So I assume that the OHIF viewer is hosted from another server, from the screenshot above this must be on port 80, while the proxy runs on 5000. I never had problems with this, just using the cors module from fastify. Can you try again with the latest version. Also specifying how you setup everything will help. Cheers.