1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | exports.Server = void 0;
|
23 | const express_1 = __importStar(require("express"));
|
24 | const path_1 = require("path");
|
25 | const webhooks_1 = require("@octokit/webhooks");
|
26 | const get_log_1 = require("../helpers/get-log");
|
27 | const logging_middleware_1 = require("./logging-middleware");
|
28 | const webhook_proxy_1 = require("../helpers/webhook-proxy");
|
29 | const version_1 = require("../version");
|
30 | class Server {
|
31 | constructor(options = {}) {
|
32 | this.version = version_1.VERSION;
|
33 | this.expressApp = express_1.default();
|
34 | this.log = options.log || get_log_1.getLog().child({ name: "server" });
|
35 | this.probotApp = new options.Probot();
|
36 | this.state = {
|
37 | port: options.port,
|
38 | host: options.host,
|
39 | webhookPath: options.webhookPath || "/",
|
40 | webhookProxy: options.webhookProxy,
|
41 | };
|
42 | this.expressApp.use(logging_middleware_1.getLoggingMiddleware(this.log));
|
43 | this.expressApp.use("/probot/static/", express_1.default.static(path_1.join(__dirname, "..", "..", "static")));
|
44 | this.expressApp.use(this.state.webhookPath, webhooks_1.createNodeMiddleware(this.probotApp.webhooks, {
|
45 | path: "/",
|
46 | }));
|
47 | this.expressApp.set("view engine", "hbs");
|
48 | this.expressApp.set("views", path_1.join(__dirname, "..", "..", "views"));
|
49 | this.expressApp.get("/ping", (req, res) => res.end("PONG"));
|
50 | }
|
51 | async load(appFn) {
|
52 | await appFn(this.probotApp, {
|
53 | getRouter: (path) => this.router(path),
|
54 | });
|
55 | }
|
56 | async start() {
|
57 | this.log.info(`Running Probot v${this.version} (Node.js: ${process.version})`);
|
58 | const port = this.state.port || 3000;
|
59 | const { host, webhookPath, webhookProxy } = this.state;
|
60 | const printableHost = host !== null && host !== void 0 ? host : "localhost";
|
61 | this.state.httpServer = (await new Promise((resolve, reject) => {
|
62 | const server = this.expressApp.listen(port, ...(host ? [host] : []), () => {
|
63 | if (webhookProxy) {
|
64 | this.state.eventSource = webhook_proxy_1.createWebhookProxy({
|
65 | logger: this.log,
|
66 | path: webhookPath,
|
67 | port: port,
|
68 | url: webhookProxy,
|
69 | });
|
70 | }
|
71 | this.log.info(`Listening on http://${printableHost}:${port}`);
|
72 | resolve(server);
|
73 | });
|
74 | server.on("error", (error) => {
|
75 | if (error.code === "EADDRINUSE") {
|
76 | error = Object.assign(error, {
|
77 | message: `Port ${port} is already in use. You can define the PORT environment variable to use a different port.`,
|
78 | });
|
79 | }
|
80 | this.log.error(error);
|
81 | reject(error);
|
82 | });
|
83 | }));
|
84 | return this.state.httpServer;
|
85 | }
|
86 | async stop() {
|
87 | if (this.state.eventSource)
|
88 | this.state.eventSource.close();
|
89 | if (!this.state.httpServer)
|
90 | return;
|
91 | const server = this.state.httpServer;
|
92 | return new Promise((resolve) => server.close(resolve));
|
93 | }
|
94 | router(path = "/") {
|
95 | const newRouter = express_1.Router();
|
96 | this.expressApp.use(path, newRouter);
|
97 | return newRouter;
|
98 | }
|
99 | }
|
100 | exports.Server = Server;
|
101 | Server.version = version_1.VERSION;
|
102 |
|
\ | No newline at end of file |