forked from Sunbird-inQuiry/editor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
120 lines (111 loc) · 4.31 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var express = require('express'),
http = require('http');
bodyParser = require('body-parser'),
proxy = require('express-http-proxy'),
urlHelper = require('url');
const latexService = require('./latexService.js')
const dotenv = require('dotenv');
dotenv.config();
const BASE_URL = process.env.BASE_URL || "dev.inquiry.sunbird.org";
const API_AUTH_TOKEN = process.env.AUTH_API_TOKEN;
const USER_TOKEN = process.env.USER_API_TOKEN;
const PORTAL_COOKIES= ""
var app = express();
app.set('port', 3000);
app.use(express.json())
app.get("/latex/convert", latexService.convert)
app.post("/latex/convert", bodyParser.json({ limit: '1mb' }), latexService.convert);
app.use(express.static(__dirname + '/web-component-examples/vanilla-js'));
const decoratePublicRequestHeaders = function () {
return function (proxyReqOpts, srcReq) {
proxyReqOpts.headers['authorization'] = `Bearer ${API_AUTH_TOKEN}`;
proxyReqOpts.headers['x-authenticated-user-token'] = USER_TOKEN;
return proxyReqOpts;
}
};
app.post(["/action/asset/v1/upload/*"], proxy(BASE_URL, {
https: true,
parseReqBody: false,
proxyReqPathResolver: function (req) {
console.log('proxyReqPathResolver ::', req.originalUrl);
let originalUrl = req.originalUrl.replace("/action/", "/api/");
return urlHelper.parse(originalUrl).path;
},
proxyReqOptDecorator: decoratePublicRequestHeaders()
})
);
app.all(['/api/framework/v1/read/*',
'/learner/framework/v1/read/*',
'/api/channel/v1/read/*',
'/api/question/v2/list'], proxy(BASE_URL, {
https: true,
proxyReqPathResolver: function(req) {
console.log('proxyReqPathResolver ', urlHelper.parse(req.url).path);
return urlHelper.parse(req.url).path;
},
proxyReqOptDecorator: decoratePublicRequestHeaders()
}));
app.use(['/action/questionset/v2/*',
'/action/question/v2/*',
'/action/collection/v1/*',
'/action/object/category/definition/v1/*',
'/action/collection/v1/*'
], proxy(BASE_URL, {
https: true,
limit: '30mb',
proxyReqPathResolver: function (req) {
let originalUrl = req.originalUrl.replace('/action/', '/api/')
console.log('proxyReqPathResolver questionset', originalUrl, require('url').parse(originalUrl).path);
return require('url').parse(originalUrl).path;
},
proxyReqOptDecorator: decoratePublicRequestHeaders()
}));
app.use(['/action/composite/v3/search'
], proxy(BASE_URL, {
https: true,
limit: '30mb',
proxyReqPathResolver: function (req) {
let originalUrl = req.originalUrl.replace('/action/composite/v3/', '/api/composite/v1/')
console.log('proxyReqPathResolver questionset', originalUrl, require('url').parse(originalUrl).path);
return require('url').parse(originalUrl).path;
},
proxyReqOptDecorator: decoratePublicRequestHeaders()
}));
app.use(['/action/program/v1/*',
'/action/question/v2/bulkUpload',
'/action/question/v2/bulkUploadStatus'
], proxy(BASE_URL, {
https: true,
limit: '30mb',
proxyReqPathResolver: function (req) {
let originalUrl = req.originalUrl.replace('/action/', '/api/')
console.log('proxyReqPathResolver questionset', originalUrl, require('url').parse(originalUrl).path);
return require('url').parse(originalUrl).path;
},
proxyReqOptDecorator: decoratePublicRequestHeaders()
}));
app.use(['/api','/assets','/action'], proxy(BASE_URL, {
https: true,
limit: '30mb',
proxyReqPathResolver: function(req) {
console.log('proxyReqPathResolver ', urlHelper.parse(req.url).path);
return urlHelper.parse(req.url).path;
},
proxyReqOptDecorator: decoratePublicRequestHeaders()
}));
app.use(['/action/content/*'], proxy(BASE_URL, {
https: true,
proxyReqPathResolver: function (req) {
let originalUrl = req.originalUrl.replace('/api/', '/api/')
console.log('proxyReqPathResolver questionset', originalUrl, require('url').parse(originalUrl).path);
return require('url').parse(originalUrl).path;
},
proxyReqOptDecorator: decoratePublicRequestHeaders()
}));
app.use(['/assets/public/*'], proxy(BASE_URL, {
https: true,
proxyReqPathResolver: function(req) {
return require('url').parse(`https://${BASE_URL}` + req.originalUrl).path
}
}));
http.createServer(app).listen(app.get('port'), 3000);