Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed Vagas Full Setup 1.0.0.exe
Binary file not shown.
63 changes: 39 additions & 24 deletions backend/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import express from "express";
import { existsSync } from "fs";
import path from "path";
import { createJobsApiApp } from "./jobsApiApp.js";
import swaggerUi from "swagger-ui-express";
import swaggerSpec from "./swagger.js";

const PORT = Number(process.env.PORT || 3001);

Expand All @@ -16,28 +14,45 @@ const outputDir = process.env.ELECTRON_OUTPUT_DIR

const app = createJobsApiApp({ outputDir });

// ── 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"));
});
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);
}
}
//Swagger documentacao
app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));

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`);
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`);
});
Comment on lines +51 to +55

Copilot AI Apr 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The startup log prints the docs URL unconditionally, even when registerSwaggerDocs() fails and Swagger is disabled (catch path). This makes logs misleading. Consider returning a boolean from registerSwaggerDocs() (or setting a flag) and only logging the docs URL when the route is actually registered.

Copilot uses AI. Check for mistakes.
}

});
void startServer();
49 changes: 38 additions & 11 deletions backend/tests/unit/services/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const mocks = vi.hoisted(() => ({
existsSync: vi.fn(),
staticMiddleware: vi.fn(),
expressStatic: vi.fn(),
swaggerServe: vi.fn(),
swaggerSetup: vi.fn(() => vi.fn()),
}));

mocks.createJobsApiApp.mockReturnValue({
Expand All @@ -18,6 +20,8 @@ mocks.createJobsApiApp.mockReturnValue({

mocks.expressStatic.mockReturnValue(mocks.staticMiddleware);

vi.mock("dotenv/config", () => ({}));

vi.mock("../../../src/jobsApiApp.js", () => ({
createJobsApiApp: mocks.createJobsApiApp,
}));
Expand All @@ -32,6 +36,23 @@ vi.mock("express", () => ({
},
}));

vi.mock("swagger-ui-express", () => ({
default: {
serve: mocks.swaggerServe,
setup: mocks.swaggerSetup,
},
}));

vi.mock("../../../src/swagger.js", () => ({
default: {},
}));

async function importServerEntry() {
await import("../../../src/server.js");
await vi.dynamicImportSettled();
await Promise.resolve();
}

describe("server entry", () => {
beforeEach(() => {
vi.resetModules();
Expand All @@ -44,11 +65,12 @@ describe("server entry", () => {
});

it("inicializa app e chama listen", async () => {
await import("../../../src/server.js");
await importServerEntry();

expect(mocks.createJobsApiApp).toHaveBeenCalledTimes(1);
expect(mocks.listen).toHaveBeenCalledTimes(1);
expect(mocks.listen).toHaveBeenCalledWith(3100, expect.any(Function));
expect(mocks.listen).toHaveBeenCalled();
expect(mocks.listen.mock.calls[0][0]).toBe(3100);
expect(typeof mocks.listen.mock.calls[0][1]).toBe("function");

const onListen = mocks.listen.mock.calls[0][1];
onListen();
Expand All @@ -58,15 +80,16 @@ describe("server entry", () => {
it("usa porta padrao quando PORT nao definido", async () => {
delete process.env.PORT;

await import("../../../src/server.js");
await importServerEntry();

expect(mocks.listen).toHaveBeenCalledWith(3001, expect.any(Function));
expect(mocks.listen).toHaveBeenCalled();
expect(mocks.listen.mock.calls[0][0]).toBe(3001);
});

it("usa ELECTRON_OUTPUT_DIR quando definido", async () => {
process.env.ELECTRON_OUTPUT_DIR = "/tmp/electron-output";

await import("../../../src/server.js");
await importServerEntry();

expect(mocks.createJobsApiApp).toHaveBeenCalledWith({
outputDir: "/tmp/electron-output",
Expand All @@ -77,14 +100,18 @@ describe("server entry", () => {
process.env.ELECTRON_STATIC_DIR = "/tmp/frontend-dist";
mocks.existsSync.mockReturnValue(true);

await import("../../../src/server.js");
await importServerEntry();

expect(mocks.expressStatic).toHaveBeenCalledWith("/tmp/frontend-dist");
expect(mocks.use).toHaveBeenCalledTimes(3);
expect(mocks.use).toHaveBeenNthCalledWith(1, mocks.staticMiddleware);
expect(mocks.use).toHaveBeenNthCalledWith(3, "/docs", expect.anything(), expect.any(Function));

const fallbackHandler = mocks.use.mock.calls[1][0];
const calls = mocks.use.mock.calls;
expect(calls.some(([arg1]) => arg1 === mocks.staticMiddleware)).toBe(true);
expect(calls.some(([arg1]) => arg1 === "/docs")).toBe(true);

const fallbackCall = calls.find(([arg1]) => typeof arg1 === "function" && arg1 !== mocks.staticMiddleware);
expect(fallbackCall).toBeTruthy();

const fallbackHandler = fallbackCall[0];

const apiRes = {
status: vi.fn().mockReturnThis(),
Expand Down
1 change: 0 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ function App() {
meta={meta}
filteredJobs={filteredJobs}
paginatedJobs={paginatedJobs}
jobs={jobs}
loading={loading || scraping}
error={error}
formatDate={formatDate}
Expand Down
Loading
Loading