50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { readdir } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { SeederRegistry } from "@api/SeederRegistry";
|
|
import { startServer } from "@api/server";
|
|
import { loadConfig } from "@config/Config";
|
|
|
|
const config = loadConfig();
|
|
const registry = new SeederRegistry(config);
|
|
|
|
// Autoload .torrent files from torrentsDir on startup
|
|
if (config.autoLoad) {
|
|
try {
|
|
const files = await readdir(config.torrentsDir);
|
|
const torrents = files.filter((f) => f.endsWith(".torrent"));
|
|
|
|
if (torrents.length > 0) {
|
|
console.log(`Loading ${torrents.length} torrent(s) from ${config.torrentsDir}…`);
|
|
for (const file of torrents) {
|
|
const path = join(config.torrentsDir, file);
|
|
const buf = Buffer.from(await Bun.file(path).arrayBuffer());
|
|
try {
|
|
const state = await registry.addTorrent(buf);
|
|
console.log(` ✓ ${state.name} [${state.infoHashHex.slice(0, 8)}]`);
|
|
} catch (err) {
|
|
console.warn(` ✗ Failed to load ${file}: ${err instanceof Error ? err.message : err}`);
|
|
}
|
|
}
|
|
}
|
|
} catch (err) {
|
|
const code = (err as NodeJS.ErrnoException).code;
|
|
if (code === "ENOENT") {
|
|
// torrentsDir doesn't exist yet — that's fine
|
|
} else {
|
|
console.error(`Failed to read torrents directory (${config.torrentsDir}):`, err);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Graceful shutdown
|
|
async function shutdown() {
|
|
console.log("\nShutting down — sending stopped announces…");
|
|
await registry.stopAll();
|
|
process.exit(0);
|
|
}
|
|
|
|
process.on("SIGINT", shutdown);
|
|
process.on("SIGTERM", shutdown);
|
|
|
|
startServer(registry, config, config.port);
|