Skip to content

Commit bc5bc22

Browse files
committed
Appi composeen, luodaan taulut jo startupissa niin statuksen kysyminen ei romuta koko appia
1 parent dc83116 commit bc5bc22

File tree

6 files changed

+60
-16
lines changed

6 files changed

+60
-16
lines changed

backend/src/app.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ app.get('/api/ping', async (req, res) => {
3232
res.send({ data: 'pong' })
3333
})
3434

35-
// Expose a small config endpoint so frontend can show current START_YEAR
3635
app.get('/api/config', (req, res) => {
3736
try {
3837
res.status(200).json({ startYear: yearFrom(), endYear: yearTo() });

backend/src/db/db.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ async function fillDb(statutes: StatuteKey[], judgments: JudgmentKey[]): Promise
5050

5151
async function dbIsReady(): Promise<boolean> {
5252
try {
53+
console.log('tsekataan db')
5354
const client = await pool.connect();
55+
console.log('yli poolin')
5456
let result = await client.query("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'images');")
5557
const imagesExists = result.rows[0].exists;
5658

@@ -244,9 +246,7 @@ async function dbIsUpToDate(startYear?: number): Promise<{upToDate: boolean, sta
244246

245247
async function createTables(): Promise<void> {
246248
try {
247-
console.log('ennen poolia')
248249
const client = await pool.connect();
249-
console.log("taulut");
250250
await client.query(`
251251
CREATE TABLE IF NOT EXISTS images (
252252
uuid UUID PRIMARY KEY,

backend/src/index.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
import app from './app.js'
2-
import { setPool } from './db/db.js'
2+
import { setPool, createTables, addStatusRow } from './db/db.js'
33
import './util/config.js'
44

55
setPool(process.env.PG_URI ?? '')
66

77
const PORT = 3001
8-
app.listen(PORT, () => {
9-
console.log(`Server running on port ${PORT}`)
10-
})
8+
9+
async function startServer() {
10+
try {
11+
await createTables()
12+
console.log('[STARTUP] Tables created/verified')
13+
14+
await addStatusRow(
15+
{
16+
message: 'server_started',
17+
timestamp: new Date().toISOString()
18+
},
19+
false
20+
)
21+
console.log('[STARTUP] Initial status row inserted')
22+
} catch (error) {
23+
console.error('[STARTUP] Error initializing database:', error)
24+
}
25+
26+
app.listen(PORT, () => {
27+
console.log(`Server running on port ${PORT}`)
28+
})
29+
}
30+
31+
startServer()

backend/src/search.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ export async function deleteCollection(name: string, lang: string) {
328328

329329

330330
export async function searchStatutes(lang: string, queryStr: string): Promise<StatuteSearchResult[]> {
331-
const searchParameters: SearchParams = {
331+
const searchParameters: SearchParams<Record<string, any>> = {
332332
q: queryStr,
333333
query_by: "title,common_names,keywords,headings,year,number,paragraphs",
334334
query_by_weights: "50,49,48,20,15,10,1",
@@ -350,7 +350,7 @@ export async function searchStatutes(lang: string, queryStr: string): Promise<St
350350

351351

352352
export async function searchJudgments(lang: string, queryStr: string, level: string): Promise<JudgmentSearchResult[]> {
353-
const searchParameters: SearchParams = {
353+
const searchParameters: SearchParams<Record<string, any>> = {
354354
q: queryStr,
355355
query_by: "keywords,level,year,number,headings,paragraphs",
356356
query_by_weights: "60,50,49,48,10,1",

docker-compose.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ services:
1111
- "5432:5432"
1212
volumes:
1313
- finlex-db-data:/data
14+
healthcheck:
15+
test: ["CMD-SHELL", "pg_isready -U postgres -d testdb"]
16+
interval: 1s
17+
timeout: 5s
18+
retries: 20
19+
start_period: 10s
1420
typesense:
1521
build:
1622
context: .
@@ -20,5 +26,23 @@ services:
2026
TYPESENSE_DATA_DIR: /tmp
2127
ports:
2228
- "8108:8108"
29+
app:
30+
build:
31+
context: .
32+
dockerfile: e2e/Dockerfile.app
33+
environment:
34+
PG_URI: postgresql://postgres:postgres@postgres:5432/testdb
35+
TYPESENSE_HOST: typesense
36+
TYPESENSE_PORT: 8108
37+
TYPESENSE_API_KEY: xyz
38+
START_YEAR: 2024
39+
NODE_ENV: production
40+
ports:
41+
- "3001:3001"
42+
depends_on:
43+
postgres:
44+
condition: service_healthy
45+
typesense:
46+
condition: service_started
2347
volumes:
2448
finlex-db-data:

e2e/Dockerfile.app

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
FROM node:24
1+
FROM node:24.0.2
22
RUN apt update && apt install -y curl
3-
USER node
43

54
WORKDIR /home/node/app/frontend
6-
COPY frontend/package-lock.json .
75
COPY frontend/package.json .
8-
RUN npm ci
6+
RUN npm install --prefer-offline --no-audit
97

108
COPY frontend/tsconfig.json .
119
COPY frontend/tsconfig.node.json .
@@ -18,13 +16,15 @@ RUN mkdir -p ../backend/src/frontend
1816
RUN npm run build
1917

2018
WORKDIR /home/node/app/backend
21-
COPY backend/package-lock.json .
2219
COPY backend/package.json .
23-
RUN npm ci
20+
RUN npm install --prefer-offline --no-audit
2421

2522
COPY backend/tsconfig.json .
2623
COPY backend/src ./src
2724
RUN npm run build
25+
RUN chown -R node:node /home/node/app || true
26+
RUN chmod -R a+rX /home/node/app/backend/dist || true
2827

28+
USER node
2929
WORKDIR /home/node/app/backend/dist
30-
CMD node start.js
30+
CMD node index.js

0 commit comments

Comments
 (0)