Skip to content

Commit 26a9f84

Browse files
committed
Add HTTP server impl
1 parent 6eaa8dc commit 26a9f84

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

agents/src/http_server.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { createServer, ServerResponse, Server, IncomingMessage } from 'http';
2+
3+
const healthCheck = async (res: ServerResponse) => {
4+
res.writeHead(200);
5+
res.end('OK');
6+
};
7+
8+
export class HTTPServer {
9+
host: string;
10+
port: number;
11+
app: Server;
12+
13+
constructor(host: string, port: number) {
14+
this.host = host;
15+
this.port = port;
16+
17+
this.app = createServer((req: IncomingMessage, res: ServerResponse) => {
18+
if (req.url === '/') {
19+
healthCheck(res);
20+
} else {
21+
res.writeHead(404);
22+
res.end('not found');
23+
}
24+
});
25+
}
26+
27+
async run(): Promise<void> {
28+
return new Promise((resolve, reject) => {
29+
this.app.listen(this.port, this.host, (err?: Error) => {
30+
if (err) reject(err);
31+
resolve();
32+
});
33+
});
34+
}
35+
36+
async close(): Promise<void> {
37+
return new Promise((resolve, reject) => {
38+
this.app.close((err?: Error) => {
39+
if (err) reject(err);
40+
resolve();
41+
});
42+
});
43+
}
44+
}

0 commit comments

Comments
 (0)