Poprawki w api i docker do wpółpracy sqlite
This commit is contained in:
@@ -18,16 +18,22 @@ FROM node:20-alpine
|
|||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN mkdir -p /app/data
|
||||||
|
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
COPY --from=builder /app/node_modules ./node_modules
|
COPY --from=builder /app/node_modules ./node_modules
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
|
|
||||||
|
COPY ./src/data/ServicesRange.db ./data/ServicesRange.db
|
||||||
|
|
||||||
# YAML + public (runtime needed!)
|
# YAML + public (runtime needed!)
|
||||||
COPY --from=builder /app/src/content ./src/content
|
COPY --from=builder /app/src/content ./src/content
|
||||||
COPY --from=builder /app/public ./public
|
COPY --from=builder /app/public ./public
|
||||||
|
|
||||||
|
|
||||||
ENV HOST=0.0.0.0
|
ENV HOST=0.0.0.0
|
||||||
ENV PORT=4321
|
ENV PORT=4321
|
||||||
|
ENV FUZ_DB_PATH=/app/data/ServicesRange.db
|
||||||
|
|
||||||
EXPOSE 4321
|
EXPOSE 4321
|
||||||
|
|
||||||
|
|||||||
@@ -9,10 +9,14 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
|
environment:
|
||||||
|
- FUZ_DB_PATH=/app/data/ServicesRange.db
|
||||||
ports:
|
ports:
|
||||||
- "4000:4321"
|
- "4000:4321"
|
||||||
networks:
|
networks:
|
||||||
- n8n_default
|
- n8n_default
|
||||||
|
volumes:
|
||||||
|
- ./data:/app/data
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
n8n_default:
|
n8n_default:
|
||||||
|
|||||||
@@ -1,18 +1,13 @@
|
|||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
import Database from "better-sqlite3";
|
import { getDb } from "./db";
|
||||||
|
|
||||||
type CityRow = { city: string };
|
type CityRow = { city: string };
|
||||||
|
|
||||||
export const GET: APIRoute = async () => {
|
export const GET: APIRoute = async () => {
|
||||||
const db = new Database("./src/data/ServicesRange.db", { readonly: true });
|
const db = getDb();
|
||||||
|
const rows = db.prepare("SELECT DISTINCT city FROM ranges ORDER BY city").all() as CityRow[];
|
||||||
|
|
||||||
const rows = db
|
return new Response(JSON.stringify(rows.map((x) => x.city)), {
|
||||||
.prepare(`SELECT DISTINCT city FROM ranges ORDER BY city`)
|
|
||||||
.all() as CityRow[];
|
|
||||||
|
|
||||||
const cities = rows.map(r => r.city);
|
|
||||||
|
|
||||||
return new Response(JSON.stringify(cities), {
|
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,47 +1,30 @@
|
|||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
import Database from "better-sqlite3";
|
import { getDb } from "./db";
|
||||||
|
|
||||||
// 🔥 Funkcja normalizująca — identyczna jak w C#
|
function normalize(s: string) {
|
||||||
function normalize(input: string): string {
|
return s.normalize("NFD").replace(/\p{Diacritic}/gu, "").toLowerCase();
|
||||||
return input
|
|
||||||
.normalize("NFD")
|
|
||||||
.replace(/\p{Diacritic}/gu, "") // usuwa ogonki
|
|
||||||
.toLowerCase();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CityRow = {
|
type CityRow = { city: string };
|
||||||
city: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const GET: APIRoute = async ({ request }) => {
|
export const GET: APIRoute = async ({ request }) => {
|
||||||
const url = new URL(request.url);
|
const q = new URL(request.url).searchParams.get("q")?.trim() || "";
|
||||||
const q = url.searchParams.get("q")?.trim() || "";
|
|
||||||
|
|
||||||
if (q.length < 2) {
|
if (q.length < 2)
|
||||||
return new Response(JSON.stringify([]), {
|
return new Response("[]", { headers: { "Content-Type": "application/json" } });
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const db = new Database("./src/data/ServicesRange.db", { readonly: true });
|
const db = getDb();
|
||||||
|
|
||||||
// 🔥 Pobieramy wszystkie miasta (jest ich mało – to działa błyskawicznie)
|
const rows = db.prepare(`SELECT DISTINCT city FROM ranges`).all() as CityRow[];
|
||||||
const stmt = db.prepare(`
|
|
||||||
SELECT DISTINCT city
|
|
||||||
FROM ranges
|
|
||||||
`);
|
|
||||||
|
|
||||||
const rows = stmt.all() as CityRow[];
|
const nq = normalize(q);
|
||||||
|
|
||||||
// 🔥 Normalizujemy po stronie Node — ZERO problemów z kolacją SQLite
|
const result = rows
|
||||||
const pattern = normalize(q);
|
.filter((r) => normalize(r.city).includes(nq))
|
||||||
|
.slice(0, 20)
|
||||||
const filtered = rows
|
|
||||||
.filter((row) => normalize(row.city).includes(pattern))
|
|
||||||
.slice(0, 20) // LIMIT 20
|
|
||||||
.map((r) => r.city);
|
.map((r) => r.city);
|
||||||
|
|
||||||
return new Response(JSON.stringify(filtered), {
|
return new Response(JSON.stringify(result), {
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
13
src/pages/api/db.ts
Normal file
13
src/pages/api/db.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import Database from "better-sqlite3";
|
||||||
|
|
||||||
|
const DB_PATH =
|
||||||
|
process.env.FUZ_DB_PATH || "./src/data/ServicesRange.db";
|
||||||
|
|
||||||
|
let db: Database.Database;
|
||||||
|
|
||||||
|
export function getDb() {
|
||||||
|
if (!db) {
|
||||||
|
db = new Database(DB_PATH, { readonly: true });
|
||||||
|
}
|
||||||
|
return db;
|
||||||
|
}
|
||||||
@@ -1,35 +1,27 @@
|
|||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
import Database from "better-sqlite3";
|
import { getDb } from "./db";
|
||||||
|
|
||||||
interface HasStreetsRow {
|
|
||||||
cnt: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const GET: APIRoute = async ({ request }) => {
|
export const GET: APIRoute = async ({ request }) => {
|
||||||
const url = new URL(request.url);
|
const city = new URL(request.url).searchParams.get("city")?.trim() || "";
|
||||||
const city = url.searchParams.get("city")?.trim() || "";
|
|
||||||
|
|
||||||
if (!city) {
|
if (!city)
|
||||||
return new Response(JSON.stringify({ hasStreets: false }), {
|
return new Response(JSON.stringify({ hasStreets: false }), {
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
const db = new Database("./src/data/ServicesRange.db", { readonly: true });
|
const db = getDb();
|
||||||
|
|
||||||
const stmt = db.prepare(`
|
const row = db
|
||||||
SELECT COUNT(*) AS cnt
|
.prepare(
|
||||||
FROM ranges
|
`SELECT COUNT(*) AS cnt
|
||||||
WHERE LOWER(city) = LOWER(?)
|
FROM ranges
|
||||||
AND TRIM(street) <> ''
|
WHERE LOWER(city) = LOWER(?)
|
||||||
`);
|
AND TRIM(street) <> ''`
|
||||||
|
)
|
||||||
const row = stmt.get(city) as HasStreetsRow;
|
.get(city) as { cnt: number };
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({ hasStreets: row.cnt > 0 }),
|
||||||
hasStreets: row.cnt > 0,
|
|
||||||
}),
|
|
||||||
{ headers: { "Content-Type": "application/json" } }
|
{ headers: { "Content-Type": "application/json" } }
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,85 +1,28 @@
|
|||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
import Database from "better-sqlite3";
|
import { getDb } from "./db";
|
||||||
|
|
||||||
interface RangeRow {
|
|
||||||
city: string;
|
|
||||||
street: string;
|
|
||||||
number: string;
|
|
||||||
availableFiber: number;
|
|
||||||
availableRadio: number;
|
|
||||||
lat: number;
|
|
||||||
lon: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const POST: APIRoute = async ({ request }) => {
|
export const POST: APIRoute = async ({ request }) => {
|
||||||
try {
|
const { city, street, number } = await request.json();
|
||||||
const body = await request.json();
|
|
||||||
const city = body.city?.trim() || "";
|
|
||||||
const street = body.street?.trim() || "";
|
|
||||||
const number = body.number?.trim() || "";
|
|
||||||
|
|
||||||
if (!city || !number) {
|
const db = getDb();
|
||||||
return new Response(JSON.stringify({ ok: false }), {
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const db = new Database("./src/data/ServicesRange.db", { readonly: true });
|
const row = db
|
||||||
|
.prepare(
|
||||||
const stmtHas = db.prepare(`
|
`SELECT *
|
||||||
SELECT COUNT(*) AS cnt
|
FROM ranges
|
||||||
FROM ranges
|
WHERE LOWER(city) = LOWER(?)
|
||||||
WHERE LOWER(city) = LOWER(?) AND TRIM(street) <> ''
|
|
||||||
`);
|
|
||||||
|
|
||||||
const has = (stmtHas.get(city) as { cnt: number }).cnt > 0;
|
|
||||||
let row: RangeRow | undefined;
|
|
||||||
|
|
||||||
if (!has) {
|
|
||||||
const stmt = db.prepare(`
|
|
||||||
SELECT *
|
|
||||||
FROM ranges
|
|
||||||
WHERE LOWER(city) = LOWER(?)
|
|
||||||
AND TRIM(number) = ?
|
|
||||||
LIMIT 1
|
|
||||||
`);
|
|
||||||
row = stmt.get(city, number) as RangeRow | undefined;
|
|
||||||
|
|
||||||
} else if (street === "") {
|
|
||||||
const stmt = db.prepare(`
|
|
||||||
SELECT *
|
|
||||||
FROM ranges
|
|
||||||
WHERE LOWER(city) = LOWER(?)
|
|
||||||
AND TRIM(number) = ?
|
|
||||||
LIMIT 1
|
|
||||||
`);
|
|
||||||
row = stmt.get(city, number) as RangeRow | undefined;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
const stmt = db.prepare(`
|
|
||||||
SELECT *
|
|
||||||
FROM ranges
|
|
||||||
WHERE LOWER(city) = LOWER(?)
|
|
||||||
AND LOWER(street) = LOWER(?)
|
AND LOWER(street) = LOWER(?)
|
||||||
AND TRIM(number) = ?
|
AND LOWER(number) = LOWER(?)
|
||||||
LIMIT 1
|
LIMIT 1`
|
||||||
`);
|
)
|
||||||
row = stmt.get(city, street, number) as RangeRow | undefined;
|
.get(city, street || "", number || "");
|
||||||
}
|
|
||||||
|
|
||||||
if (!row) {
|
if (!row)
|
||||||
return new Response(JSON.stringify({ ok: false }), {
|
return new Response(JSON.stringify({ ok: false }), {
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
return new Response(
|
return new Response(JSON.stringify({ ok: true, result: row }), {
|
||||||
JSON.stringify({ ok: true, result: row }),
|
headers: { "Content-Type": "application/json" },
|
||||||
{ headers: { "Content-Type": "application/json" } }
|
});
|
||||||
);
|
};
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
console.error("SEARCH API ERROR:", err);
|
|
||||||
return new Response(JSON.stringify({ ok: false }), { status: 500 });
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,49 +1,36 @@
|
|||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
import Database from "better-sqlite3";
|
import { getDb } from "./db";
|
||||||
|
|
||||||
type StreetRow = {
|
function normalize(s: string) {
|
||||||
street: string;
|
return s.normalize("NFD").replace(/\p{Diacritic}/gu, "").toLowerCase();
|
||||||
};
|
|
||||||
|
|
||||||
// 🔥 Funkcja normalizująca — taka sama jak dla miast
|
|
||||||
function normalize(input: string): string {
|
|
||||||
return input
|
|
||||||
.normalize("NFD")
|
|
||||||
.replace(/\p{Diacritic}/gu, "") // usuwa ogonki
|
|
||||||
.toLowerCase();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type StreetRow = { street: string };
|
||||||
|
|
||||||
export const GET: APIRoute = async ({ request }) => {
|
export const GET: APIRoute = async ({ request }) => {
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
|
|
||||||
const city = url.searchParams.get("city")?.trim() || "";
|
const city = url.searchParams.get("city")?.trim() || "";
|
||||||
const q = url.searchParams.get("q")?.trim() || "";
|
const q = url.searchParams.get("q")?.trim() || "";
|
||||||
|
|
||||||
if (!city || q.length < 1) {
|
if (!city || q.length < 1)
|
||||||
return new Response(JSON.stringify([]), {
|
return new Response("[]", { headers: { "Content-Type": "application/json" } });
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const db = new Database("./src/data/ServicesRange.db", { readonly: true });
|
const db = getDb();
|
||||||
|
|
||||||
// 🔥 Pobieramy WSZYSTKIE ulice tego miasta (jest ich mało)
|
const rows = db.prepare(
|
||||||
const stmt = db.prepare(`
|
`SELECT DISTINCT street
|
||||||
SELECT DISTINCT street
|
FROM ranges
|
||||||
FROM ranges
|
WHERE LOWER(city) = LOWER(?)
|
||||||
WHERE city = ?
|
AND TRIM(street) <> ''`
|
||||||
ORDER BY street
|
).all(city) as StreetRow[];
|
||||||
`);
|
|
||||||
|
|
||||||
const rows = stmt.all(city) as StreetRow[];
|
|
||||||
|
|
||||||
// 🔥 Normalizujemy porównanie — SQLite LIKE NIE JEST UŻYWANE
|
|
||||||
const pattern = normalize(q);
|
const pattern = normalize(q);
|
||||||
|
|
||||||
const filtered = rows
|
const filtered = rows
|
||||||
.filter((row) => normalize(row.street).includes(pattern))
|
.map((s) => s.street)
|
||||||
.slice(0, 20)
|
.filter((s) => normalize(s).includes(pattern))
|
||||||
.map((r) => r.street);
|
.slice(0, 20);
|
||||||
|
|
||||||
return new Response(JSON.stringify(filtered), {
|
return new Response(JSON.stringify(filtered), {
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
|
|||||||
Reference in New Issue
Block a user