-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
170 lines (144 loc) · 5.03 KB
/
server.js
File metadata and controls
170 lines (144 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const http = require("http");
const fs = require("fs");
const path = require("path");
const readline = require("readline");
const PORT = 8080;
const MESSAGE_DIR = "./messages";
async function loadMessages() {
try {
// Check if directory exists
if (!fs.existsSync(MESSAGE_DIR)) {
console.warn(`Directory ${MESSAGE_DIR} does not exist. Creating it...`);
fs.mkdirSync(MESSAGE_DIR, { recursive: true });
return []
}
const files = fs.readdirSync(MESSAGE_DIR)
const messages = []
for (const file of files) {
const filePath = path.join(MESSAGE_DIR, file)
try {
const rl = readline.createInterface({
input: fs.createReadStream(filePath),
crlfDelay: Infinity
})
for await (const line of rl) {
try {
messages.push(JSON.parse(line))
} catch (e) {
console.warn(`Failed to parse line in ${file}:`, e.message)
}
}
} catch (e) {
console.error(`Error reading file ${filePath}:`, e.message)
}
}
return messages
} catch (error) {
console.error("Error loading messages:", error.message)
return []
}
}
function renderHTML() {
return `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Chat Viewer</title>
<style>
body { font-family: monospace; background:#111; color:#eee; padding:20px }
input { margin:5px; padding:5px }
button { padding:5px 10px }
.msg { margin-bottom:5px }
.time { color:#888 }
.name { color:#6cf }
.channel { color:#fc6 }
</style>
</head>
<body>
<h2>Chat Viewer</h2>
<input id="name" placeholder="name">
<input id="channel" placeholder="channel">
<input id="search" placeholder="search">
<select id="sort">
<option value="asc">Oldest</option>
<option value="desc">Newest</option>
</select>
<button onclick="load()">Search</button>
<hr>
<div id="results"></div>
<script>
async function load() {
const name = document.getElementById("name").value
const channel = document.getElementById("channel").value
const search = document.getElementById("search").value
const sort = document.getElementById("sort").value
const params = new URLSearchParams({name, channel, search, sort})
const res = await fetch("/api?" + params.toString())
const data = await res.json()
const container = document.getElementById("results")
container.innerHTML = ""
for (const m of data) {
const div = document.createElement("div")
div.className = "msg"
div.innerHTML =
"<span class='time'>[" + m.timestamp + "]</span> " +
"<span class='name'>" + m.name + "</span> " +
"<span class='channel'>(" + m.channel + ")</span>: " +
m.message
container.appendChild(div)
}
}
</script>
</body>
</html>
`
}
const server = http.createServer(async (req, res) => {
try {
const parsedUrl = new URL(req.url, `http://${req.headers.host}`)
const pathname = parsedUrl.pathname
const searchParams = parsedUrl.searchParams
if (pathname === "/") {
res.writeHead(200, {"Content-Type":"text/html"})
res.end(renderHTML())
return
}
if (pathname === "/api") {
try {
let messages = await loadMessages()
const name = searchParams.get("name")
const channel = searchParams.get("channel")
const search = searchParams.get("search")
const sort = searchParams.get("sort")
messages = messages.filter(m => {
if (name && m.name !== name) return false
if (channel && m.channel !== channel) return false
if (search && !m.message.toLowerCase().includes(search.toLowerCase())) return false
return true
})
messages.sort((a,b)=>
new Date(a.timestamp) - new Date(b.timestamp)
)
if (sort === "desc")
messages.reverse()
res.writeHead(200, {"Content-Type":"application/json"})
res.end(JSON.stringify(messages.slice(0, 1000)))
} catch (error) {
console.error("Error in /api endpoint:", error.message)
res.writeHead(500, {"Content-Type":"application/json"})
res.end(JSON.stringify({error: "Failed to load messages"}))
}
return
}
res.writeHead(404, {"Content-Type":"application/json"})
res.end(JSON.stringify({error: "Not found"}))
} catch (error) {
console.error("Unhandled server error:", error.message)
res.writeHead(500, {"Content-Type":"application/json"})
res.end(JSON.stringify({error: "Internal server error"}))
}
})
server.listen(PORT, () => {
console.log("Running on http://localhost:" + PORT)
})