restaf-server
Version:
Custom app server for Viya apps
295 lines (250 loc) • 8.22 kB
JavaScript
/*
* ------------------------------------------------------------------------------------
* * Copyright (c) SAS Institute Inc.
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------------------------
*
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _server = _interopRequireDefault(require("./server"));
var _handlers = require("./handlers");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
var debug = require('debug');
var debugSetup = debug('setup');
var os = require('os');
function iService(uTable, useDefault, asset, rootHandler, allAppEnv) {
process.env.APPHOST = process.env.APPHOST === '*' ? os.hostname() : process.env.APPHOST;
var appName = '/' + process.env.APPNAME;
var auth1 = {};
var auth2 = false;
var defaultMaxBytes = 10485760;
var maxBytes;
if (isNaN(process.env.PAYLOADMAXBYTES)) {
maxBytes = defaultMaxBytes;
} else {
maxBytes = Number(process.env.PAYLOADMAXBYTES);
}
if (process.env.PROXYSERVER === 'YES') {
process.env.OAUTH2 = 'YES';
}
console.log("appName ".concat(appName));
console.log("asset ".concat(asset, " "));
console.log("uTable ".concat(uTable));
console.log(allAppEnv);
var getAppEnv =
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(req, h) {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
return _context.abrupt("return", allAppEnv);
case 1:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function getAppEnv(_x, _x2) {
return _ref.apply(this, arguments);
};
}();
if (process.env.OAUTH2 === 'YES') {
auth1 = {
mode: 'required',
strategy: 'sas'
};
} else {
auth1 = false;
} // see if appenv was overridden
var hasAppEnv = false;
if (uTable !== null) {
hasAppEnv = uTable.find(function (u) {
return u.path === '/appenv';
});
}
console.log(hasAppEnv); // end temp patch
var defaultTable = [{
method: ['GET'],
path: "".concat(appName),
config: {
auth: auth1,
handler: _handlers.getApp
}
}, {
method: ['GET'],
path: "".concat(appName, "/{param*}"),
config: {
auth: auth2,
handler: getApp2
}
}, {
method: ['GET'],
path: "".concat(appName, "/callback").concat(appName),
config: {
handler: _handlers.appCallback
}
}, {
method: ['GET'],
path: "/shared/{param*}",
config: {
auth: false,
handler: getShared
}
/*, {
method: [ 'GET' ],
path : `/reDirector`,
config: {
auth : false,
handler: reDirector
}
}*/
}];
if (hasAppEnv === false) {
console.log('Setting default /appenv');
defaultTable.push({
method: ['GET'],
path: '/appenv',
config: {
auth: false,
handler: getAppEnv
}
});
} else {
console.log('Setting custom /appenv');
} // Tried payload.parse = false -- way too much code to handle payload
if (process.env.PROXYSERVER === 'YES') {
var handleOthers = [{
method: ['PUT', 'POST', 'PATCH'],
path: '/{params*}',
config: {
auth: auth1,
payload: {
maxBytes: maxBytes
},
handler: _handlers.handleProxy
}
}, {
method: ['GET'],
path: '/{params*}',
config: {
auth: auth1,
handler: _handlers.handleProxy
}
}];
defaultTable = [].concat(_toConsumableArray(defaultTable), handleOthers);
} else {
var _handleOthers = {
method: ['GET'],
path: '/{param*}',
config: {
auth: false,
handler: getApp2
}
};
defaultTable = [].concat(_toConsumableArray(defaultTable), [_handleOthers]);
}
var userRouterTable;
if (uTable !== null) {
if (useDefault === true) {
userRouterTable = [].concat(_toConsumableArray(defaultTable), _toConsumableArray(uTable));
} else {
userRouterTable = _toConsumableArray(uTable);
}
} else {
userRouterTable = _toConsumableArray(defaultTable);
}
debugSetup(console.log(JSON.stringify(userRouterTable, null, 4)));
(0, _server["default"])(userRouterTable, asset);
} //
// get app server files - too small
//
function getIcon(_x3, _x4) {
return _getIcon.apply(this, arguments);
}
function _getIcon() {
_getIcon = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee2(req, h) {
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
return _context2.abrupt("return", h.file('favicon.ico'));
case 1:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
return _getIcon.apply(this, arguments);
}
function getApp2(_x5, _x6) {
return _getApp.apply(this, arguments);
}
function _getApp() {
_getApp = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee3(req, h) {
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
return _context3.abrupt("return", h.file(req.params.param));
case 1:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));
return _getApp.apply(this, arguments);
}
function getShared(_x7, _x8) {
return _getShared.apply(this, arguments);
}
function _getShared() {
_getShared = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee4(req, h) {
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
return _context4.abrupt("return", h.file("shared/".concat(req.params.param)));
case 1:
case "end":
return _context4.stop();
}
}
}, _callee4);
}));
return _getShared.apply(this, arguments);
}
var _default = iService;
exports["default"] = _default;