Skip to content

Commit

Permalink
feat: useheaders.hostinstead of env SERVER_HOST
Browse files Browse the repository at this point in the history
- Make SERVER_HOST only using for `http.listen`
- Use `express.request.headers.host` to get host of request
  • Loading branch information
Chinlinlee committed Aug 23, 2022
1 parent 4044c5d commit 3a74081
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
10 changes: 7 additions & 3 deletions api/dicom-xml/contoller/uploadXml.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ async function storeFile (filestream , writestream , storeFileName) {
});
});
}
/**
* @param {import("express").Request} req
* @param {import("express").Response} res
*/
module.exports = async function (req , res) {
let files = await uploadFile(req);
try {
Expand All @@ -75,9 +79,9 @@ module.exports = async function (req , res) {
let filename = files.filename[key];
let storeFileName = `${__dirname}/../upload_xml/${filename}`;
let xmlWS = fs.createWriteStream(storeFileName);
let storeFileStatu = await storeFile(files.files[key] , xmlWS , storeFileName);
if (storeFileStatu) {
result.push(`http://${process.env.SERVER_HOST}:${process.env.SERVER_PORT}/xml2dcm/${storeFileStatu}`);
let storeFileStatus = await storeFile(files.files[key] , xmlWS , storeFileName);
if (storeFileStatus) {
result.push(`${req.protocol}://${req.headers.host}/xml2dcm/${storeFileStatus}`);
}
}
res.send(result);
Expand Down
22 changes: 19 additions & 3 deletions plugins/oauth/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const oauthPlugin = pluginsConfig.oauth;
// 透過OAuth驗證
// 參考 https://blog.yorkxin.org/posts/oauth2-6-bearer-token.html
// Server=https://github.com/pedroetb/node-oauth2-server-example

/**
*
* @param {import("express").Request} req
Expand Down Expand Up @@ -58,6 +59,11 @@ module.exports.isOAuthLogin = async function (req, res, next) {
}
};

/**
*
* @param {import("express").Request} req
* @returns
*/
async function verifyOAuthAccessToken(req) {
// Token驗證是否通過
let tokenValidation = false;
Expand Down Expand Up @@ -110,6 +116,11 @@ async function verifyOAuthAccessToken(req) {
return tokenValidation;
}

/**
*
* @param {import("express").Request} req
* @param {import("express").Response} res
*/
async function requestOAuthToken(req, res) {
// 重新導回的網址
let theUrl = req.originalUrl;
Expand All @@ -127,7 +138,7 @@ async function requestOAuthToken(req, res) {
method: "POST",
code: req.query.code,
session_state: req.query.session_state,
redirect_uri: `${oauthPlugin.http}://${process.env.SERVER_HOST}${theUrl}`
redirect_uri: `${oauthPlugin.http}://${req.headers.host}${theUrl}`
});

const tokenOptions = {
Expand Down Expand Up @@ -178,18 +189,23 @@ async function requestOAuthToken(req, res) {
});
}

/**
*
* @param {import("express").Request} req
* @param {*} res
*/
async function redirectToOAuthLoginPage(req, res) {
// 可能keycloak有點bug,會遺失掉放在網址的參數,我們這邊從先把query的Parameters存在session...。
let theUrl = req.originalUrl.split("?")[0];
console.log(
"OAuth2轉址位址:" +
`${oauthPlugin.http}://${process.env.SERVER_HOST}${theUrl}`
`${oauthPlugin.http}://${req.headers.host}${theUrl}`
);
req.session.oriQuery = req.query;

// 導向至登入畫面...
res.redirect(
`${oauthPlugin.http}://${oauthPlugin.host}/${oauthPlugin.auth_path}?client_id=${oauthPlugin.client_id}&grant_type=authorization_code&response_type=code&redirect_uri=${oauthPlugin.http}://${process.env.SERVER_HOST}${theUrl}`
`${oauthPlugin.http}://${oauthPlugin.host}/${oauthPlugin.auth_path}?client_id=${oauthPlugin.client_id}&grant_type=authorization_code&response_type=code&redirect_uri=${oauthPlugin.http}://${req.headers.host}${theUrl}`
);
}

Expand Down
17 changes: 13 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ app.use((req, res, next) => {
require("routes.js")(app);
app.engine('html', require('ejs').renderFile);
app.set("views", "public");
//
http.createServer(app).listen(port, function () {
console.log(`http server is listening on port:${port}`);
});

// If host is present, the server will accept connection on specific host, when
// host is omitted, use IPv6 (::) or IPv4 (0.0.0.0)
if (process.env.SERVER_HOST) {
http.createServer(app).listen(port, process.env.SERVER_HOST, function () {
console.log(`http server is listening on port:${port}`);
});
} else {
http.createServer(app).listen(port, function () {
console.log(`http server is listening on port:${port}`);
});
}


let osPlatform = os.platform().toLocaleLowerCase();
if (osPlatform.includes('linux')) {
Expand Down

0 comments on commit 3a74081

Please sign in to comment.