Files
ignis/server/index.js
2026-03-10 22:31:01 +01:00

74 lines
2.4 KiB
JavaScript

const express = require("express");
const path = require("path");
const config = require("./config");
const { setupWebSocket } = require("./ws");
const app = express();
app.use(express.json({ limit: "50mb" }));
// --- Request logging ---
app.use((req, res, next) => {
const start = Date.now();
const origEnd = res.end;
res.end = function (...args) {
const duration = Date.now() - start;
const status = res.statusCode;
const color =
status >= 500 ? "\x1b[31m" : status >= 400 ? "\x1b[33m" : "\x1b[32m";
const reset = "\x1b[0m";
const path =
req.originalUrl.length > 80
? req.originalUrl.slice(0, 80) + "..."
: req.originalUrl;
console.log(
`${color}${req.method} ${status}${reset} ${path} (${duration}ms)`,
);
origEnd.apply(this, args);
};
next();
});
// --- Routes ---
const fsRoutes = require("./routes/fs");
const vaultRoutes = require("./routes/vault");
app.use("/api/fs", fsRoutes);
app.use("/api/vault", vaultRoutes);
// Serve vault files for resource URLs (images, attachments, etc.)
// Vault ID is the first path segment: /vault-files/<vault-id>/path/to/file
app.use("/vault-files", (req, res, next) => {
// Extract vault ID from the first path segment
const parts = req.path.split("/").filter(Boolean);
if (parts.length === 0)
return res.status(400).json({ error: "Missing vault ID" });
const vaultId = decodeURIComponent(parts[0]);
const vaultPath = config.getVaultPath(vaultId);
if (!vaultPath) return res.status(404).json({ error: "Vault not found" });
// Rewrite req.url to strip the vault ID prefix, then serve statically
req.url = "/" + parts.slice(1).join("/");
express.static(vaultPath)(req, res, next);
});
// --- Static serving ---
// dist/ has shim-loader.js + patched index.html (dev mode).
// In Docker, these live inside the obsidian assets dir instead.
app.use(express.static(path.join(__dirname, "..", "dist")));
// Serve obsidian assets (app.js, app.css, libs, fonts, etc.)
app.use(express.static(config.obsidianAssetsPath));
// --- Start ---
const server = app.listen(config.port, () => {
console.log(
`[obsidian-bridge] Server running on http://localhost:${config.port}`,
);
console.log(`[obsidian-bridge] Vault root: ${config.vaultRoot}`);
console.log(
`[obsidian-bridge] Vaults: ${Object.keys(config.vaults).join(", ")}`,
);
});
setupWebSocket(server);