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