Path: blob/main/components/gitpod-protocol/src/util/debug-app.ts
2500 views
/**1* Copyright (c) 2021 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import * as http from "http";7import express from "express";8import { injectable, postConstruct } from "inversify";9import { log, LogrusLogLevel } from "./logging";1011export interface SetLogLevelRequest {12level: LogrusLogLevel;13}14export namespace SetLogLevelRequest {15export function is(o: any): o is SetLogLevelRequest {16return typeof o === "object" && "level" in o;17}18}1920@injectable()21export class DebugApp {22protected _app: express.Application;23protected httpServer: http.Server | undefined = undefined;2425@postConstruct()26public ctor() {27this._app = this.create();28}2930create(): express.Application {31const app = express();3233app.use(express.json());34app.use(express.urlencoded({ extended: true }));3536app.post("/debug/logging", (req, res) => {37try {38const levelRequest = req.body;39if (!SetLogLevelRequest.is(levelRequest)) {40res.status(400).end("not a SetLogLevelRequest");41return;42}4344const newLogLevel = levelRequest.level;45log.setLogLevel(newLogLevel);46log.info("set log level", { newLogLevel });47res.status(200).end(JSON.stringify(levelRequest));48} catch (err) {49res.status(500).end(err);50}51});52return app;53}5455public start(port: number = 6060) {56this.httpServer = this._app.listen(port, "localhost", () => {57log.info(`debug server listening on port: ${port}`);58});59}6061public async stop() {62const server = this.httpServer;63if (!server) {64return;65}66return new Promise<void>((resolve) =>67server.close((err: any) => {68if (err) {69log.warn(`error while closing http server`, { err });70}71this.httpServer = undefined;72resolve();73}),74);75}7677public get app(): express.Application {78return this._app;79}80}818283