Skip to content

Commit 0365bef

Browse files
format
1 parent b272450 commit 0365bef

File tree

6 files changed

+733
-708
lines changed

6 files changed

+733
-708
lines changed

src/fileServer/fileServer.ts

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
1-
import {FileObject} from "../fileSystem/fileObject";
2-
import {Log} from "../lib/Log";
3-
1+
import { FileObject } from "../fileSystem/fileObject";
2+
import { Log } from "../lib/Log";
43

54
class FileServer {
6-
private readonly file: FileObject;
7-
private readonly port: number;
8-
private httpServer:Server | undefined;
9-
private readonly log:Log = new Log("fileServer");
10-
11-
constructor(port:number,file:FileObject) {
12-
this.file = file;
13-
this.port = port;
14-
}
15-
16-
start(onSuccess: (msg: any) => void, onError: (err: any) => void,):void{
17-
this.httpServer = CreateServer(this.port,onSuccess,onError)
18-
19-
// @ts-ignore
20-
httpServer.setOnRequestHandler(this.handleRequest.bind(this));
21-
}
22-
23-
private handleRequest(req: { requestId: string; path: string }): void {
24-
this.log.d("Request received:", req);
25-
// handle file serving logic here
26-
this.log.d("Received request:", req.requestId);
27-
this.log.d("Request Path", req.path);
28-
this.sendText("This is a test",req.requestId,null)
29-
this.log.d("Response sent")
30-
}
31-
32-
private sendText(text:string, id:string, mimeType:string | null | undefined) {
33-
this.httpServer?.send(id, {
34-
status: 200,
35-
body: text,
36-
headers: {
37-
"Content-Type": mimeType || "text/html",
38-
},
39-
},()=>{},this.log.e);
40-
}
41-
42-
43-
}
5+
private readonly file: FileObject;
6+
private readonly port: number;
7+
private httpServer: Server | undefined;
8+
private readonly log: Log = new Log("fileServer");
9+
10+
constructor(port: number, file: FileObject) {
11+
this.file = file;
12+
this.port = port;
13+
}
14+
15+
start(onSuccess: (msg: any) => void, onError: (err: any) => void): void {
16+
this.httpServer = CreateServer(this.port, onSuccess, onError);
17+
18+
// @ts-ignore
19+
httpServer.setOnRequestHandler(this.handleRequest.bind(this));
20+
}
21+
22+
private handleRequest(req: { requestId: string; path: string }): void {
23+
this.log.d("Request received:", req);
24+
// handle file serving logic here
25+
this.log.d("Received request:", req.requestId);
26+
this.log.d("Request Path", req.path);
27+
this.sendText("This is a test", req.requestId, null);
28+
this.log.d("Response sent");
29+
}
30+
31+
private sendText(
32+
text: string,
33+
id: string,
34+
mimeType: string | null | undefined,
35+
) {
36+
this.httpServer?.send(
37+
id,
38+
{
39+
status: 200,
40+
body: text,
41+
headers: {
42+
"Content-Type": mimeType || "text/html",
43+
},
44+
},
45+
() => {},
46+
this.log.e,
47+
);
48+
}
49+
}

0 commit comments

Comments
 (0)