@@ -4,6 +4,8 @@ const path = require("node:path");
4
4
5
5
const mime = require ( "mime" ) ;
6
6
7
+ const logger = require ( "./logger" ) ;
8
+
7
9
const DEFAULT_INDEX_FILE = process . env . DEFAULT_INDEX_FILE || "index.html" ;
8
10
const FALLBACK_FILE = process . env . FALLBACK_FILE || null ;
9
11
const PORT = process . env . PORT || 8080 ;
@@ -14,7 +16,7 @@ function getFile(filePath) {
14
16
const file = fs . readFileSync ( filePath ) ;
15
17
return file ;
16
18
} catch {
17
- console . error ( `Could not find file "${ filePath } "` ) ;
19
+ logger . error ( `Could not find file "${ filePath } "` ) ;
18
20
return ;
19
21
}
20
22
}
@@ -44,20 +46,21 @@ function createFilePath(domain, filePath) {
44
46
function handleRequest ( req , res ) {
45
47
const domain = getDomainFromRequest ( req ) ;
46
48
const filePath = getFilePathFromRequest ( req ) ;
47
- console . debug ( " Requesting" , domain , filePath ) ;
49
+ logger . debug ( ` Requesting ${ domain } ${ filePath } ` ) ;
48
50
49
51
const actualFilePath = createFilePath ( domain , filePath ) ;
50
52
let file = getFile ( actualFilePath ) ;
51
53
52
54
if ( ! file && FALLBACK_FILE ) {
53
55
const fallbackFilePath = createFilePath ( domain , FALLBACK_FILE ) ;
54
- console . debug ( " Serving fallback file:" , fallbackFilePath ) ;
56
+ logger . debug ( ` Serving fallback file: ${ fallbackFilePath } ` ) ;
55
57
file = getFile ( fallbackFilePath ) ;
56
58
}
57
59
58
60
if ( ! file ) {
59
61
res . writeHead ( 404 ) ;
60
62
res . end ( ) ;
63
+ logger . accessLog ( req , res ) ;
61
64
return ;
62
65
}
63
66
@@ -66,9 +69,10 @@ function handleRequest(req, res) {
66
69
res . setHeader ( "Content-Type" , mimeType ) ;
67
70
res . writeHead ( 200 ) ;
68
71
res . end ( file ) ;
72
+ logger . accessLog ( req , res ) ;
69
73
}
70
74
71
75
const server = http . createServer ( handleRequest ) ;
72
76
server . listen ( PORT ) ;
73
77
74
- console . info ( `Listening on port ${ PORT } ` ) ;
78
+ logger . info ( `Listening on port ${ PORT } ` ) ;
0 commit comments