-
-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runtime): proxy cloud storage & website hosting request to minio (…
…#1560) * feat(runtime): proxy cloud storage to minio * fix(runtime): fix minio proxy * feat(runtime): add request_id for function required * chore: expose port for container * chore(runtime): change ctx field name
- Loading branch information
Showing
18 changed files
with
383 additions
and
128 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import express from 'express' | ||
import Config from './config' | ||
import { logger } from './support/logger' | ||
import { DatabaseAgent } from './db' | ||
import './support/cloud-sdk' | ||
import { WebsiteHostingChangeStream } from './support/database-change-stream/website-hosting-change-stream' | ||
import proxy from 'express-http-proxy' | ||
import axios from 'axios' | ||
|
||
const app = express() | ||
|
||
DatabaseAgent.accessor.ready.then(() => { | ||
WebsiteHostingChangeStream.initialize() | ||
}) | ||
|
||
const tryPath = (bucket: string, path: string) => { | ||
const testPaths = path.endsWith('/') | ||
? [path + 'index.html', '/index.html'] | ||
: [path, path + '/index.html', '/index.html'] | ||
return testPaths.map((v) => `/${bucket}${v}`) | ||
} | ||
|
||
app.use( | ||
proxy(Config.OSS_INTERNAL_ENDPOINT, { | ||
preserveHostHdr: true, | ||
parseReqBody: false, | ||
proxyReqOptDecorator: function (proxyReqOpts, srcReq) { | ||
// patch for | ||
if ('content-length' in srcReq.headers) { | ||
proxyReqOpts.headers['content-length'] = | ||
srcReq.headers['content-length'] | ||
} | ||
if ('connection' in srcReq.headers) { | ||
proxyReqOpts.headers['connection'] = srcReq.headers['connection'] | ||
} | ||
return proxyReqOpts | ||
}, | ||
proxyReqPathResolver: async function (req) { | ||
// check if is website hosting | ||
const websiteHosting = WebsiteHostingChangeStream.websiteHosting.find( | ||
(item) => req.hostname === item.domain, | ||
) | ||
if (!websiteHosting) { | ||
return req.url | ||
} | ||
|
||
// req.url doesn't have hostname | ||
const minioUrl = new URL(req.url, Config.OSS_INTERNAL_ENDPOINT) | ||
const paths = tryPath(websiteHosting.bucketName, req.path) | ||
const getUrl = () => minioUrl.pathname + minioUrl.search | ||
|
||
for (const [idx, path] of paths.entries()) { | ||
minioUrl.pathname = path | ||
|
||
if (idx === paths.length - 1) { | ||
return getUrl() | ||
} | ||
|
||
try { | ||
await axios.head(minioUrl.toString()) | ||
return getUrl() | ||
} catch (err) { | ||
if (err.response.status === 404) { | ||
continue | ||
} | ||
return getUrl() | ||
} | ||
} | ||
}, | ||
}), | ||
) | ||
|
||
const storageServer = app.listen(Config.STORAGE_PORT, () => | ||
logger.info( | ||
`storage server ${process.pid} listened on ${Config.STORAGE_PORT}`, | ||
), | ||
) | ||
|
||
export default storageServer |
27 changes: 27 additions & 0 deletions
27
runtimes/nodejs/src/support/database-change-stream/conf-change-stream.ts
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { CONFIG_COLLECTION } from '../../constants' | ||
import { DatabaseAgent } from '../../db' | ||
import { DatabaseChangeStream } from '.' | ||
|
||
export class ConfChangeStream { | ||
static initialize() { | ||
DatabaseChangeStream.onStreamChange( | ||
CONFIG_COLLECTION, | ||
this.updateEnvironments, | ||
) | ||
} | ||
|
||
private static async updateEnvironments() { | ||
const conf = await DatabaseAgent.db | ||
.collection(CONFIG_COLLECTION) | ||
.findOne({}) | ||
|
||
if (!conf) { | ||
return | ||
} | ||
|
||
const environments = conf.environments || [] | ||
for (const env of environments) { | ||
process.env[env.name] = env.value | ||
} | ||
} | ||
} |
Oops, something went wrong.