Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
299 lines (264 loc) · 9.22 KB
/
Copy pathserver.js
File metadata and controls
299 lines (264 loc) · 9.22 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import express from 'express';
import cors from 'cors';
import fs from 'fs/promises';
import path from 'path';
import chalk from 'chalk';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const app = express();
// CLI引数の解析
const args = process.argv.slice(2);
let jsonlPath = null;
let port = 3001;
let host = 'localhost';
let enableBackup = true;
let verbose = false;
let quiet = false;
for (let i = 0; i < args.length; i++) {
switch (args[i]) {
case '--jsonl-path':
if (args[i + 1]) {
jsonlPath = path.resolve(args[i + 1]);
i++;
}
break;
case '--port':
if (args[i + 1]) {
port = parseInt(args[i + 1]);
i++;
}
break;
case '--host':
if (args[i + 1]) {
host = args[i + 1];
i++;
}
break;
case '--no-backup':
enableBackup = false;
break;
case '--verbose':
verbose = true;
break;
case '--quiet':
quiet = true;
break;
}
}
// Logger utility
const log = {
info: (msg) => !quiet && console.log(chalk.blue(`[INFO] ${new Date().toISOString()}`), msg),
warn: (msg) => console.warn(chalk.yellow(`[WARN] ${new Date().toISOString()}`), msg),
error: (msg) => console.error(chalk.red(`[ERROR] ${new Date().toISOString()}`), msg),
success: (msg) => !quiet && console.log(chalk.green(`[SUCCESS] ${new Date().toISOString()}`), msg),
verbose: (msg) => verbose && console.log(chalk.gray(`[VERBOSE] ${new Date().toISOString()}`), msg)
};
log.verbose('Starting CLI argument parsing...');
if (jsonlPath) {
log.success(`JSONL path configured: ${chalk.cyan(jsonlPath)}`);
} else {
log.warn('No --jsonl-path specified. Server will operate in limited mode.');
log.info('Usage: node server.js --jsonl-path <path-to-jsonl-file-or-directory>');
}
if (!enableBackup) {
log.info('Automatic backup creation disabled');
}
if (host !== 'localhost') {
log.info(`Host configured: ${chalk.cyan(host)}`);
}
app.use(cors({
origin: ['http://localhost:5173', 'http://localhost:5174', 'http://localhost:3000'],
credentials: true
}));
app.use(express.json());
// 指定されたパスの情報を取得
async function getPathInfo(targetPath) {
log.verbose(`Checking path: ${targetPath}`);
try {
const stat = await fs.stat(targetPath);
if (stat.isDirectory()) {
log.verbose(`Path is directory: ${targetPath}`);
const files = await fs.readdir(targetPath);
const jsonlFiles = files
.filter(file => file.endsWith('.jsonl'))
.map(file => ({
name: file,
path: path.join(targetPath, file),
isFile: true
}));
log.success(`Found ${jsonlFiles.length} JSONL files in directory: ${chalk.cyan(jsonlFiles.map(f => f.name).join(', '))}`);
return { isDirectory: true, files: jsonlFiles };
} else {
log.verbose(`Path is file: ${targetPath}`);
return { isDirectory: false, filePath: targetPath };
}
} catch (error) {
log.error(`Failed to access path: ${chalk.red(targetPath)} - ${error.message}`);
throw new Error(`Path not found: ${targetPath}`);
}
}
// API エンドポイント
app.get('/api/config', async (req, res) => {
log.info('GET /api/config - Client requesting configuration');
if (!jsonlPath) {
log.warn('Config requested but no --jsonl-path specified');
return res.json({ error: 'No --jsonl-path specified' });
}
try {
const pathInfo = await getPathInfo(jsonlPath);
log.success('Configuration sent to client');
res.json({
jsonlPath,
...pathInfo
});
} catch (error) {
log.error(`Config request failed: ${error.message}`);
res.status(404).json({ error: error.message });
}
});
// ファイル一覧取得
app.get('/api/files', async (req, res) => {
log.info('GET /api/files - Client requesting file list');
if (!jsonlPath) {
log.warn('File list requested but no --jsonl-path specified');
return res.status(400).json({ error: 'No --jsonl-path specified' });
}
try {
const pathInfo = await getPathInfo(jsonlPath);
if (pathInfo.isDirectory) {
log.success(`Sent file list: ${pathInfo.files.length} files`);
res.json({ files: pathInfo.files });
} else {
const fileName = path.basename(jsonlPath);
log.success(`Sent single file: ${fileName}`);
res.json({ files: [{ name: fileName, path: jsonlPath, isFile: true }] });
}
} catch (error) {
log.error(`File list request failed: ${error.message}`);
res.status(404).json({ error: error.message });
}
});
// ファイル読み込み
app.get('/api/files/:filename', async (req, res) => {
const filename = req.params.filename;
log.info(`GET /api/files/${filename} - Client requesting file content`);
if (!jsonlPath) {
log.warn(`File read requested for ${filename} but no --jsonl-path specified`);
return res.status(400).json({ error: 'No --jsonl-path specified' });
}
try {
const pathInfo = await getPathInfo(jsonlPath);
let filePath;
if (pathInfo.isDirectory) {
filePath = path.join(jsonlPath, filename);
log.info(`Reading file from directory: ${filePath}`);
} else {
filePath = jsonlPath;
log.info(`Reading single file: ${filePath}`);
}
const content = await fs.readFile(filePath, 'utf-8');
const lines = content.split('\n').length - 1;
log.success(`File read successfully: ${filename} (${content.length} characters, ~${lines} lines)`);
res.json({ content, filePath });
} catch (error) {
log.error(`Failed to read file ${filename}: ${error.message}`);
res.status(404).json({ error: error.message });
}
});
// ファイル保存
app.post('/api/files/:filename', async (req, res) => {
const filename = req.params.filename;
log.info(`POST /api/files/${filename} - Client requesting file save`);
if (!jsonlPath) {
log.warn(`File save requested for ${filename} but no --jsonl-path specified`);
return res.status(400).json({ error: 'No --jsonl-path specified' });
}
try {
const { content } = req.body;
const pathInfo = await getPathInfo(jsonlPath);
let filePath;
if (pathInfo.isDirectory) {
filePath = path.join(jsonlPath, filename);
log.info(`Saving file to directory: ${filePath}`);
} else {
filePath = jsonlPath;
log.info(`Saving to single file: ${filePath}`);
}
// バックアップ作成(有効な場合のみ)
if (enableBackup) {
try {
const existingContent = await fs.readFile(filePath, 'utf-8');
const backupPath = `${filePath}.backup.${Date.now()}`;
await fs.writeFile(backupPath, existingContent, 'utf-8');
log.verbose(`Backup created: ${chalk.gray(backupPath)}`);
} catch (backupError) {
log.warn(`Could not create backup (file may be new): ${backupError.message}`);
}
}
await fs.writeFile(filePath, content, 'utf-8');
const lines = content.split('\n').length - 1;
log.success(`File saved successfully: ${filename} (${content.length} characters, ~${lines} lines)`);
res.json({ success: true, filePath });
} catch (error) {
log.error(`Failed to save file ${filename}: ${error.message}`);
res.status(500).json({ error: error.message });
}
});
// ポート使用可能性チェック
async function isPortAvailable(port) {
return new Promise((resolve) => {
const server = app.listen(port, () => {
server.close(() => resolve(true));
});
server.on('error', () => resolve(false));
});
}
// 利用可能ポート検索
async function findAvailablePort(startPort, maxAttempts = 10) {
for (let i = 0; i < maxAttempts; i++) {
const testPort = startPort + i;
if (await isPortAvailable(testPort)) {
return testPort;
}
}
return null;
}
// サーバー起動
async function startServer() {
try {
// 指定ポートをチェック
const isAvailable = await isPortAvailable(port);
if (!isAvailable) {
log.warn(`Port ${port} is already in use, searching for available port...`);
const availablePort = await findAvailablePort(port);
if (availablePort) {
port = availablePort;
log.info(`Using alternative port: ${chalk.cyan(port)}`);
} else {
log.error('No available ports found in range');
process.exit(1);
}
}
app.listen(port, host, () => {
const displayHost = host === '0.0.0.0' ? 'localhost' : host;
log.success(`Server started on ${chalk.cyan(`http://${displayHost}:${port}`)}`);
if (host === '0.0.0.0') {
log.info(`Network access enabled - available on all interfaces`);
} else if (host !== 'localhost') {
log.info(`Custom host binding: ${chalk.cyan(host)}`);
}
if (jsonlPath) {
log.info(`JSONL path configured: ${chalk.cyan(jsonlPath)}`);
log.info('Server ready to handle file operations');
} else {
log.warn('Server started in limited mode - no file path specified');
log.info('Use --jsonl-path <path> to specify target files');
}
log.verbose('Available endpoints: GET /api/config, GET /api/files, GET /api/files/:filename, POST /api/files/:filename');
});
} catch (error) {
log.error(`Failed to start server: ${error.message}`);
process.exit(1);
}
}
startServer();