sample-pilet-service
Version:
Piral: Sample pilet feed service.
73 lines • 3.84 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runApp = runApp;
const express = require("express");
const responseTime = require("response-time");
const cors = require("cors");
const busboy = require("connect-busboy");
const auth_1 = require("./auth");
const resolvers_1 = require("./resolvers");
const db_1 = require("./db");
const snapshot_1 = require("./db/snapshot");
const middleware_1 = require("./middleware");
const endpoints_1 = require("./endpoints");
const constants_1 = require("./constants");
function getUrl(port) {
const protocol = process.env.HTTP_X_FORWARDED_PROTO || constants_1.defaultProtocol;
const host = process.env.WEBSITE_HOSTNAME || `localhost:${port}`;
return `${protocol}://${host}`;
}
function runApp() {
return __awaiter(this, arguments, void 0, function* ({ filePath = constants_1.defaultFilePath, piletPath = constants_1.defaultPiletPath, authPath = constants_1.defaultAuthPath, loginPath = constants_1.defaultLoginPath, port = constants_1.defaultPort, apiKeys = auth_1.defaultKeys, snapshotDir = constants_1.defaultSnapshotDir, rootUrl = getUrl(port), } = {}) {
const app = express();
const authUrl = `${rootUrl}${authPath}`;
const loginUrl = `${rootUrl}${loginPath}`;
const snapshot = (0, snapshot_1.useSnapshot)(snapshotDir);
app.use(cors({
origin: '*',
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
optionsSuccessStatus: 200,
}));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(responseTime());
app.use(busboy({
highWaterMark: 2 * 1024 * 1024, // Set 2MiB buffer
limits: {
fileSize: 32 * 1024 * 1024, // Set 32MiB limit
},
}));
app.get(loginPath, (0, middleware_1.checkAuthRequestId)(), (0, endpoints_1.getLoginPage)());
app.post(loginPath, (0, middleware_1.checkAuthRequestId)(), (0, endpoints_1.finishLogin)());
app.get(authPath, (0, middleware_1.checkAuthRequestId)(), (0, endpoints_1.getAuthStatus)(apiKeys));
app.post(authPath, (0, endpoints_1.createAuthRequest)(authUrl, loginUrl));
app.get(piletPath, (0, endpoints_1.getLatestPilets)());
app.post(piletPath, (0, middleware_1.checkAuth)(apiKeys, authUrl, 'publish-pilet'), (0, endpoints_1.publishPilet)(rootUrl, snapshot));
app.get(filePath, (0, endpoints_1.getFiles)());
yield (0, resolvers_1.withGql)(app);
yield snapshot.read(db_1.piletData);
return app.listen(port, () => {
console.info(`Pilet feed fervice started on port ${port}.`);
console.info(``);
console.info(` URL for uploading pilets:`);
console.info(``);
console.info(` ${rootUrl}${piletPath}`);
console.info(``);
console.info(` API keys for publishing:`);
console.info(``);
console.info(` ${apiKeys.join('\n ')}`);
console.info(``);
});
});
}
//# sourceMappingURL=app.js.map