2018-06-21 08:29:35 +02:00
|
|
|
const express = require('express')
|
|
|
|
const Webtask = require('webtask-tools')
|
|
|
|
const cors = require('cors')
|
|
|
|
const proxy = require('http-proxy-middleware')
|
|
|
|
|
|
|
|
const server = express()
|
|
|
|
|
2018-06-21 13:11:26 +02:00
|
|
|
const onProxyReq = function (proxyReq, req) {
|
|
|
|
proxyReq.setHeader('Authorization', `Zoho-oauthtoken ${req.webtaskContext.secrets.ZOHO_TOKEN}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const configZohoCRM = {
|
2018-06-21 08:29:35 +02:00
|
|
|
target: 'https://www.zohoapis.com/crm/v2/',
|
2018-06-21 13:11:26 +02:00
|
|
|
pathRewrite: { '^/zoho/crm/': '/' },
|
|
|
|
changeOrigin: true,
|
|
|
|
onProxyReq
|
|
|
|
}
|
|
|
|
|
|
|
|
const configZohoCampaigns = {
|
|
|
|
target: 'https://campaigns.zoho.com/api/',
|
|
|
|
pathRewrite: { '^/zoho/campaigns/': '/' },
|
2018-06-21 08:29:35 +02:00
|
|
|
changeOrigin: true,
|
2018-06-21 13:11:26 +02:00
|
|
|
onProxyReq
|
2018-06-21 08:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
server.use(cors())
|
2018-06-21 13:11:26 +02:00
|
|
|
// server.use('*', proxy(config))
|
|
|
|
server.use(proxy('/zoho/crm/**', configZohoCRM))
|
|
|
|
server.use(proxy('/zoho/campaigns/**', configZohoCampaigns))
|
2018-06-21 08:29:35 +02:00
|
|
|
|
|
|
|
server.listen(4430)
|
|
|
|
|
|
|
|
module.exports = Webtask.fromExpress(server)
|