-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (49 loc) · 2.06 KB
/
Copy pathserver.js
File metadata and controls
59 lines (49 loc) · 2.06 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
import "dotenv/config";
import express from "express";
import { existsSync } from "fs";
import path from "path";
import { createJobsApiApp } from "./jobsApiApp.js";
const PORT = Number(process.env.PORT || 3001);
// When running inside Electron, main.js sets ELECTRON_OUTPUT_DIR to a
// writable user-data path. Fall back to the CWD-relative folder for dev/CLI.
const outputDir = process.env.ELECTRON_OUTPUT_DIR
? process.env.ELECTRON_OUTPUT_DIR
: path.resolve(process.cwd(), "output");
const app = createJobsApiApp({ outputDir });
app.set("trust proxy", 1);
async function registerSwaggerDocs() {
try {
const [{ default: swaggerUi }, { default: swaggerSpec }] = await Promise.all([
import("swagger-ui-express"),
import("./swagger.js"),
]);
app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
} catch (error) {
// eslint-disable-next-line no-console
console.warn("Swagger desabilitado:", error instanceof Error ? error.message : error);
}
}
async function startServer() {
await registerSwaggerDocs();
// ── Electron static-file serving ─────────────────────────────────────────
// When ELECTRON_STATIC_DIR is set, serve the React production build so that
// the Electron window can load everything from http://localhost:<PORT>.
const staticDir = process.env.ELECTRON_STATIC_DIR;
if (staticDir && existsSync(staticDir)) {
app.use(express.static(staticDir));
// SPA catch-all: return index.html for any non-API route so that React
// Router (if used in future) works correctly.
app.use((req, res) => {
if (req.path.startsWith("/api")) {
return res.status(404).json({ error: "Rota não encontrada." });
}
res.sendFile(path.join(staticDir, "index.html"));
});
}
app.listen(PORT, () => {
// eslint-disable-next-line no-console
console.log(`API de vagas rodando em http://localhost:${PORT}`);
console.log(`Documentação da API em http://localhost:${PORT}/docs`);
});
}
void startServer();