Skip to content

Commit

Permalink
chore: disable proxy conditionally mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kernoeb committed Dec 12, 2024
1 parent c3c3a00 commit e18e53a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
9 changes: 9 additions & 0 deletions config/custom-environment-variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @param {string} key
*/
const jsonEnv = (key) => ({ __name: key, __format: 'json' })

module.exports = {
port: 'PORT',
allowProxy: jsonEnv('ALLOW_PROXY')
}
3 changes: 2 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
port: 5600
port: 5600,
allowProxy: true
}
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<body class="openapi-viewer">
<div id="app"></div>
<script>
// [DYNAMIC]
window.iFrameResizer = {
heightCalculationMethod: 'taggedElement'
}
Expand Down
24 changes: 14 additions & 10 deletions public/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<md-button @click.native="refresh">
<md-icon>autorenew</md-icon>
</md-button>
<md-checkbox v-model="useProxy" title="Allows to bypass CORS restrictions">Use proxy</md-checkbox>
<md-checkbox v-if="allowProxy" v-model="useProxy" title="Allows to bypass CORS restrictions">Use proxy</md-checkbox>
</md-layout>
</md-layout>
</md-toolbar>
Expand Down Expand Up @@ -65,15 +65,19 @@ export default {
components: {
OpenApi
},
data: () => ({
api: null,
error: null,
useProxy: proxyParam === 'true' || false,
url: urlParam || 'https://koumoul.com/s/geocoder/api/v1/api-docs.json',
headers: (getParameterByName('headers') && JSON.parse(decodeURIComponent(getParameterByName('headers')))) || {},
queryParams : (getParameterByName('query-params') && JSON.parse(decodeURIComponent(getParameterByName('query-params')))) || {},
showToolbar: !(getParameterByName('hide-toolbar') === 'true')
}),
data() {
const allowProxy = typeof window !== 'undefined' && window.CONFIG && window.CONFIG.allowProxy
return {
api: null,
error: null,
allowProxy,
useProxy: allowProxy ? (proxyParam === 'true' || false) : false,
url: urlParam || 'https://koumoul.com/s/geocoder/api/v1/api-docs.json',
headers: (getParameterByName('headers') && JSON.parse(decodeURIComponent(getParameterByName('headers')))) || {},
queryParams: (getParameterByName('query-params') && JSON.parse(decodeURIComponent(getParameterByName('query-params')))) || {},
showToolbar: !(getParameterByName('hide-toolbar') === 'true')
}
},
mounted() {
this.refresh()
},
Expand Down
33 changes: 20 additions & 13 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ const config = require('config')
const express = require('express')
const path = require('node:path')
const axios = require('axios').default
const fs = require('node:fs')

let app = module.exports = express()

// Business routers
app.get('/proxy', (req, res) => {
if (!req.query.url || typeof req.query.url !== 'string') {
return res.status(400).send('url query parameter is required')
}
if (config.allowProxy === true) {
app.get('/proxy', (req, res) => {
if (!req.query.url || typeof req.query.url !== 'string') {
return res.status(400).send('url query parameter is required')
}

axios.get(req.query.url, {
responseType: 'stream'
}).then((response) => {
response.data.pipe(res)
}).catch((err) => {
res.status(500).send(err.message)
axios.get(req.query.url, {
responseType: 'stream'
}).then((response) => {
response.data.pipe(res)
}).catch((err) => {
res.status(500).send(err.message)
})
})
})
}

// Static routing
const oneWeek = 7 * 24 * 60 * 60
Expand All @@ -31,9 +33,14 @@ const staticOptions = {
app.use('/assets', express.static(path.join(__dirname, '../public/assets'), staticOptions))
app.use('/bundles', express.static(path.join(__dirname, '../public/bundles'), staticOptions))

const htmlPath = path.join(__dirname, '../public/index.html')
const htmlContent = fs.readFileSync(htmlPath, 'utf8')
.replaceAll('// [DYNAMIC]', 'window.CONFIG = ' + JSON.stringify({ allowProxy: config.allowProxy }))

app.use('/*', (req, res) => {
res.setHeader('Cache-Control', 'public, max-age=0')
res.sendFile(path.join(__dirname, '../public/index.html'))
res.setHeader('Content-Type', 'text/html')
res.send(htmlContent)
})

// Error handling to complement express default error handling. TODO do something useful of errors.
Expand Down

0 comments on commit e18e53a

Please sign in to comment.