-
Notifications
You must be signed in to change notification settings - Fork 27
Develop #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Develop #67
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| { | ||
| "KEYWORDS": [ | ||
| "Java" | ||
| "Java", | ||
| "Spring", | ||
| "RabbitMQ", | ||
| "Docker" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -8,15 +8,59 @@ import { getConfig } from "./config.js"; | |||
| import { searchJobsWithCache } from "./pipeline/searchJobsWithCache.js"; | ||||
| import { sources } from "./sources/index.js"; | ||||
|
|
||||
| const DEFAULT_ALLOWED_ORIGINS = [ | ||||
| "https://painel-vagas-lake.vercel.app", | ||||
| "http://localhost:5173", | ||||
| "http://localhost:5174", | ||||
| ]; | ||||
|
|
||||
| function parseAllowedOrigins(value) { | ||||
| const configuredOrigins = String(value ?? "") | ||||
| .split(",") | ||||
| .map((origin) => origin.trim()) | ||||
| .filter(Boolean); | ||||
|
|
||||
| return new Set(configuredOrigins.length > 0 ? configuredOrigins : DEFAULT_ALLOWED_ORIGINS); | ||||
| } | ||||
|
|
||||
| /** | ||||
| * @param {{ outputDir?: string }} [options] | ||||
| */ | ||||
| export function createJobsApiApp(options = {}) { | ||||
| const outputDir = options.outputDir ?? path.resolve(process.cwd(), "output"); | ||||
| const allowedOrigins = parseAllowedOrigins(process.env.CORS_ALLOWED_ORIGINS); | ||||
| const corsOptions = { | ||||
| origin(origin, callback) { | ||||
| if (!origin || allowedOrigins.has(origin)) { | ||||
| callback(null, true); | ||||
| return; | ||||
| } | ||||
|
|
||||
| callback(new Error("Origin not allowed by CORS")); | ||||
| }, | ||||
| methods: ["GET", "POST", "OPTIONS"], | ||||
| allowedHeaders: ["Content-Type"], | ||||
| credentials: false, | ||||
| maxAge: 86400, | ||||
| }; | ||||
| const app = express(); | ||||
| let activeScraperRun = null; | ||||
|
|
||||
| app.use(cors()); | ||||
| app.disable("x-powered-by"); | ||||
| app.use(express.json({ limit: "16kb" })); | ||||
| app.use((req, res, next) => { | ||||
| res.setHeader("X-Content-Type-Options", "nosniff"); | ||||
| res.setHeader("X-Frame-Options", "DENY"); | ||||
| res.setHeader("Referrer-Policy", "strict-origin-when-cross-origin"); | ||||
| res.setHeader("Permissions-Policy", "camera=(), microphone=(), geolocation=()"); | ||||
|
|
||||
| if (req.secure || req.get("x-forwarded-proto") === "https") { | ||||
| res.setHeader("Strict-Transport-Security", "max-age=31536000; includeSubDomains"); | ||||
| } | ||||
|
|
||||
| next(); | ||||
| }); | ||||
| app.use(cors(corsOptions)); | ||||
| app.use(express.json()); | ||||
|
||||
| app.use(express.json()); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,28 @@ describe("jobs API", () => { | |
| const res = await request(app).get("/api/jobs/files").expect(200); | ||
| expect(res.body.files).toEqual([]); | ||
| }); | ||
|
|
||
| it("permite CORS para o frontend oficial", async () => { | ||
| tmpDir = mkdtempSync(join(tmpdir(), "jobs-api-")); | ||
| const app = createJobsApiApp({ outputDir: tmpDir }); | ||
| const res = await request(app) | ||
| .get("/api/health") | ||
| .set("Origin", "https://painel-vagas-lake.vercel.app") | ||
| .expect(200); | ||
|
Comment on lines
+44
to
+50
|
||
|
|
||
| expect(res.headers["access-control-allow-origin"]).toBe("https://painel-vagas-lake.vercel.app"); | ||
| }); | ||
|
|
||
| it("bloqueia origens nao autorizadas", async () => { | ||
| tmpDir = mkdtempSync(join(tmpdir(), "jobs-api-")); | ||
| const app = createJobsApiApp({ outputDir: tmpDir }); | ||
| const res = await request(app) | ||
| .get("/api/health") | ||
| .set("Origin", "https://malicioso.example") | ||
| .expect(403); | ||
|
|
||
| expect(res.body.message).toBe("Origem nao permitida."); | ||
| }); | ||
|
|
||
| it("GET /api/jobs retorna 404 quando nao ha planilhas", async () => { | ||
| tmpDir = mkdtempSync(join(tmpdir(), "jobs-api-")); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; | |||||||
| const mocks = vi.hoisted(() => ({ | ||||||||
| listen: vi.fn(), | ||||||||
| use: vi.fn(), | ||||||||
| set: vi.fn(), | ||||||||
| createJobsApiApp: vi.fn(), | ||||||||
| consoleLog: vi.fn(), | ||||||||
| existsSync: vi.fn(), | ||||||||
|
|
@@ -16,6 +17,7 @@ const mocks = vi.hoisted(() => ({ | |||||||
| mocks.createJobsApiApp.mockReturnValue({ | ||||||||
| listen: mocks.listen, | ||||||||
| use: mocks.use, | ||||||||
| set: mocks.set, | ||||||||
| }); | ||||||||
|
|
||||||||
| mocks.expressStatic.mockReturnValue(mocks.staticMiddleware); | ||||||||
|
|
@@ -68,8 +70,8 @@ describe("server entry", () => { | |||||||
| await importServerEntry(); | ||||||||
|
|
||||||||
| expect(mocks.createJobsApiApp).toHaveBeenCalledTimes(1); | ||||||||
| expect(mocks.listen).toHaveBeenCalled(); | ||||||||
| expect(mocks.listen.mock.calls[0][0]).toBe(3100); | ||||||||
| expect(mocks.set).toHaveBeenCalledWith("trust proxy", 1); | ||||||||
| expect(mocks.listen).toHaveBeenCalled(); expect(mocks.listen.mock.calls[0][0]).toBe(3100); | ||||||||
|
||||||||
| expect(mocks.listen).toHaveBeenCalled(); expect(mocks.listen.mock.calls[0][0]).toBe(3100); | |
| expect(mocks.listen).toHaveBeenCalled(); | |
| expect(mocks.listen.mock.calls[0][0]).toBe(3100); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Frontend -> backend API endpoint for production deployments | ||
| VITE_API_BASE_URL=https://jobsglobalscraper.ddns.net | ||
|
|
||
| # Optional override for local dev when using the Vite proxy | ||
| # VITE_API_PROXY_TARGET=http://localhost:3001 |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,28 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { JobFile, JobsResponse } from "@/types/jobs"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function normalizeBaseUrl(value: string | undefined): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return typeof value === "string" ? value.trim().replace(/\/+$/, "") : ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function getApiBaseUrl(): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const configuredBaseUrl = normalizeBaseUrl(import.meta.env.VITE_API_BASE_URL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (configuredBaseUrl) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return configuredBaseUrl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof window !== "undefined" && window.location.hostname === "painel-vagas-lake.vercel.app") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "https://jobsglobalscraper.ddns.net"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3
to
+19
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function normalizeBaseUrl(value: string | undefined): string { | |
| return typeof value === "string" ? value.trim().replace(/\/+$/, "") : ""; | |
| } | |
| function getApiBaseUrl(): string { | |
| const configuredBaseUrl = normalizeBaseUrl(import.meta.env.VITE_API_BASE_URL); | |
| if (configuredBaseUrl) { | |
| return configuredBaseUrl; | |
| } | |
| if (typeof window !== "undefined" && window.location.hostname === "painel-vagas-lake.vercel.app") { | |
| return "https://jobsglobalscraper.ddns.net"; | |
| } | |
| return ""; | |
| } | |
| const VERCEL_PRODUCTION_HOSTNAME = "painel-vagas-lake.vercel.app"; | |
| const VPS_API_BASE_URL = "https://jobsglobalscraper.ddns.net"; | |
| function normalizeBaseUrl(value: string | undefined): string { | |
| return typeof value === "string" ? value.trim().replace(/\/+$/, "") : ""; | |
| } | |
| export function resolveApiBaseUrl(configuredBaseUrl: string | undefined, hostname?: string): string { | |
| const normalizedConfiguredBaseUrl = normalizeBaseUrl(configuredBaseUrl); | |
| if (normalizedConfiguredBaseUrl) { | |
| return normalizedConfiguredBaseUrl; | |
| } | |
| if (hostname === VERCEL_PRODUCTION_HOSTNAME) { | |
| return VPS_API_BASE_URL; | |
| } | |
| return ""; | |
| } | |
| function getApiBaseUrl(): string { | |
| return resolveApiBaseUrl( | |
| import.meta.env.VITE_API_BASE_URL, | |
| typeof window !== "undefined" ? window.location.hostname : undefined, | |
| ); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
express.json()is being registered twice: once with a 16kb limit and again later without options. The second call overrides the intended body-size limit and adds redundant parsing work; keep a singleexpress.json()middleware (with the desired limit) and remove the duplicate registration.