-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2ae331
commit db0404f
Showing
3 changed files
with
51 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ dist | |
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,50 @@ | ||
const applyConfig = (config) => { | ||
import superagent from 'superagent'; | ||
import cookie from 'react-cookie'; | ||
import { parse as parseUrl } from 'url'; | ||
|
||
import { settings } from '~/config'; | ||
|
||
export const getAPIResourceWithAuth = (req) => | ||
new Promise((resolve) => { | ||
const internalApiUrl = parseUrl( | ||
settings.internalApiPath || settings.apiPath, | ||
); | ||
const apiUrl = parseUrl(settings.apiPath); | ||
|
||
const scheme = apiUrl.protocol.slice(0, apiUrl.protocol.length - 1); | ||
|
||
const path = `/VirtualHostBase/${scheme}/${apiUrl.hostname}:${apiUrl.port}${apiUrl.path}/VirtualHostRoot${req.path}`; | ||
const url = `${internalApiUrl.hostname}:${internalApiUrl.port}${path}`; | ||
const request = superagent.get(url).responseType('blob'); | ||
const authToken = cookie.load('auth_token'); | ||
if (authToken) { | ||
request.set('Authorization', `Bearer ${authToken}`); | ||
} | ||
request.end((error, res = {}) => { | ||
if (error) { | ||
resolve(res || error); | ||
} else { | ||
resolve(res); | ||
} | ||
}); | ||
}); | ||
|
||
export default (config) => { | ||
if (__SERVER__) { | ||
const express = require('express'); | ||
const middleware = express.Router(); | ||
|
||
middleware.all('/**/@@rdf', function (req, res, next) { | ||
getAPIResourceWithAuth(req).then((resource) => { | ||
res.send(resource.body); | ||
}); | ||
}); | ||
middleware.id = 'rdf-proxy-middleware'; | ||
|
||
config.settings.expressMiddleware = [ | ||
...config.settings.expressMiddleware, | ||
middleware, | ||
]; | ||
} | ||
return config; | ||
}; | ||
|
||
export default applyConfig; |