Merge remote-tracking branch 'sedum/feat/ssr-proxy'

This commit is contained in:
lesion 2023-03-19 23:28:44 +01:00
commit 079bcd4af2
No known key found for this signature in database
GPG key ID: 352918250B012177
4 changed files with 59 additions and 0 deletions

View file

@ -67,3 +67,24 @@ list of strings you can override.
> warning "Restart needed"
> Note that a restart is needed when you change user_locale's content.
### Proxy
Proxy outward request from gancio.
Look [here](https://www.npmjs.com/package/https-proxy-agent) for options.
Note: `hostname` option takes precedence over `host`.
```json
"proxy": {
"protocol": "http:",
"hostname": "",
"host": "127.0.0.1",
"port": "8118",
"auth": {
"username": "user",
"password": "password"
},
"headers": {
"X-Proxy-Header": "example"
}
}
```

26
modules/axios-proxy.js Normal file
View file

@ -0,0 +1,26 @@
const HttpsProxyAgent = require("https-proxy-agent"),
axios = require("axios"),
config = require('../server/config')
function axiosProxy (_moduleOptions) {
if (config.proxy && !(!config.proxy.hostname && !config.proxy.host)) {
const httpsAgent = new HttpsProxyAgent({
protocol: config.proxy.protocol,
hostname: config.proxy.hostname,
host: config.proxy.host,
port: config.proxy.port,
auth: config.proxy.auth.username + ':' + config.proxy.auth.password,
headers: config.proxy.headers
})
// Use axios as you normally would, but specify httpsAgent in the default configs
// https://github.com/nuxt-community/axios-module/pull/428#issuecomment-743313813
// Nuxt 2: https://github.com/axios/axios/issues/925#issuecomment-513028175
// Nuxt 3: https://github.com/unjs/ofetch#-adding-https-agent
axios.defaults.httpsAgent = httpsAgent
}
}
module.exports = axiosProxy

View file

@ -56,6 +56,7 @@ module.exports = {
modules: [
// Doc: https://axios.nuxtjs.org/usage
'@nuxtjs/i18n',
'~/modules/axios-proxy.js', // Note: import this before @nuxtjs/axios to override defaults of both instances: `$axios` available in context, and `axios` used in controllers
'@nuxtjs/axios',
'@nuxtjs/auth',
'@nuxtjs/sitemap',

View file

@ -15,6 +15,17 @@ let config = {
db: {},
user_locale: path.resolve(process.env.cwd || '', 'user_locale'),
upload_path: path.resolve(process.env.cwd || '', 'uploads'),
proxy: {
protocol: process.env.GANCIO_PROXY_PROTOCOL || '',
hostname: process.env.GANCIO_PROXY_HOSTNAME || '',
host: process.env.GANCIO_PROXY_HOST || '',
port: process.env.GANCIO_PROXY_PORT || '',
auth: {
username: process.env.GANCIO_PROXY_USERNAME || '',
password: process.env.GANCIO_PROXY_PASSWORD || ''
},
headers: process.env.GANCIO_PROXY_HEADERS && JSON.parse(process.env.GANCIO_PROXY_HEADERS) || {}
},
write (config_path= process.env.config_path || './config.json') {
delete config.status
return fs.writeFileSync(config_path, JSON.stringify(config, null, 2))