Skip to content

Commit 0728a63

Browse files
committed
feat: enhance MindsDB integration and refactor Docker setup for improved application startup
1 parent 795f418 commit 0728a63

File tree

14 files changed

+114
-70
lines changed

14 files changed

+114
-70
lines changed

apps/server/Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ RUN adduser --system --uid 1001 bunuser
2828
USER bunuser
2929

3030
COPY --from=builder --chown=bunuser:kbnet /app/apps/server/dist ./
31+
COPY --from=builder --chown=bunuser:kbnet /app/packages/database /app/packages/database
3132
COPY --from=builder --chown=bunuser:kbnet /app/packages/database/generated /app/generated
3233
COPY --from=installer --chown=bunuser:kbnet /app/node_modules ./node_modules
3334
COPY --from=builder --chown=bunuser:kbnet /app/package.json ./package.json
3435
COPY --from=builder --chown=bunuser:kbnet /app/turbo.json ./turbo.json
3536
COPY --from=builder --chown=bunuser:kbnet /app/package.json ./package.json
37+
COPY --from=builder --chown=bunuser:kbnet /app/apps/server/entrypoint.sh ./entrypoint.sh
3638

3739

3840
EXPOSE 8000
3941
RUN chmod 700 ./bin
42+
RUN chmod +x ./entrypoint.sh
4043

41-
CMD ["./bin"]
44+
45+
CMD ["./entrypoint.sh"]

apps/server/entrypoint.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
3+
# Change to the database package directory
4+
cd /app/packages/database || exit 1
5+
6+
# Run Prisma database migration
7+
echo "Running Prisma database migration..."
8+
npx prisma db push --skip-generate
9+
10+
# Return to the app root directory
11+
cd /app || exit 1
12+
13+
# Start the application
14+
echo "Starting application..."
15+
exec ./bin

apps/server/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { authClient, tokenToSession } from "./lib/auth-client";
77
import { createServer } from "http";
88
import { MessageType, pack } from "@kbnet/shared";
99
import { serve } from "@hono/node-server";
10-
import { runMindsDBQuery } from "@kbnet/shared/mindsdb";
10+
import { connectMindsDB, runMindsDBQuery } from "@kbnet/shared/mindsdb";
1111
import { prisma } from "@kbnet/db";
1212

1313
config();

docker-compose.yml

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
services:
22
db:
3+
container_name: kb_db
34
image: postgres:15-alpine
45
environment:
56
POSTGRES_USER: mindsdb
@@ -12,29 +13,47 @@ services:
1213
networks:
1314
- kbnet
1415
mindsdb:
16+
container_name: kb_mindsdb
1517
image: mindsdb/mindsdb:latest
1618
ports:
1719
- "47334:47334"
1820
- "47335:47335"
19-
- "47337:47337"
2021
volumes:
2122
- mindsdb_data:/root/mdb_storage
2223
depends_on:
2324
- db
2425
networks:
2526
- kbnet
2627

27-
# server:
28-
# image: kbnet-server:latest
29-
# depends_on:
30-
# - mindsdb
31-
# - db
32-
# ports:
33-
# - "8000:8000"
34-
# env_file:
35-
# - .env
36-
# networks:
37-
# - kbnet
28+
platform:
29+
container_name: kb_platform
30+
build:
31+
context: .
32+
dockerfile: ./apps/platform/Dockerfile
33+
depends_on:
34+
- server
35+
- db
36+
ports:
37+
- "3000:3000"
38+
env_file:
39+
- .env
40+
networks:
41+
- kbnet
42+
43+
server:
44+
container_name: kb_server
45+
build:
46+
context: .
47+
dockerfile: ./apps/server/Dockerfile
48+
depends_on:
49+
- mindsdb
50+
- db
51+
ports:
52+
- "8000:8000"
53+
env_file:
54+
- .env
55+
networks:
56+
- kbnet
3857

3958
volumes:
4059
mindsdb_data:

packages/shared/src/lib/constants/configs.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const PROJECT_NAME = "kbnet";
2-
const APPDB_DS = "appdb_ds";
32

43
export const MindsDBConfig = {
54
PROJECT_NAME: PROJECT_NAME,
@@ -9,12 +8,12 @@ export const MindsDBConfig = {
98
SUMMARY_JOB_NAME: `${PROJECT_NAME}.summary_job`,
109
PENDING_SUMMARY_VIEW_NAME: `${PROJECT_NAME}.pending_summary_view`,
1110

12-
APPDB_DS: APPDB_DS,
13-
MAPS: `${APPDB_DS}.public.maps`,
14-
NODES: `${APPDB_DS}.public.nodes`,
15-
NAVIGATION_STEPS: `${APPDB_DS}.public.navigation_steps`,
16-
NODE_RELATIONSHIPS: `${APPDB_DS}.public.node_relationships`,
17-
MAPS_SUMMARIES: `${APPDB_DS}.public.maps_summaries`,
11+
APPDB_DS: "appdb_ds",
12+
MAPS: `appdb_ds.public.maps`,
13+
NODES: `appdb_ds.public.nodes`,
14+
NAVIGATION_STEPS: `appdb_ds.public.navigation_steps`,
15+
NODE_RELATIONSHIPS: `appdb_ds.public.node_relationships`,
16+
MAPS_SUMMARIES: `appdb_ds.public.maps_summaries`,
1817

1918
HACKERNEWS_DS: "hackernews_ds",
2019
MEDIAWIKI_DS: "mediawiki_ds",
Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
1-
export { connectMindsDB, runMindsDBQuery } from "./mindsdb";
1+
import mindsdb, { type SqlQueryResult } from "mindsdb-js-sdk";
2+
import dotenv from "dotenv";
3+
4+
dotenv.config();
5+
6+
const config = {
7+
host: process.env.MINDSDB_HOST || "http://localhost:47334",
8+
user: process.env.MINDSDB_USER || "mindsdb",
9+
password: process.env.MINDSDB_PASSWORD || "mindsdb",
10+
};
11+
12+
export async function connectMindsDB() {
13+
try {
14+
// @ts-ignore
15+
await mindsdb.default.connect(config);
16+
} catch (error) {
17+
console.error("Failed to connect to MindsDB:", error);
18+
throw error;
19+
}
20+
}
21+
22+
export async function runMindsDBQuery(query: string): Promise<SqlQueryResult> {
23+
try {
24+
await connectMindsDB();
25+
// @ts-ignore
26+
const result = await mindsdb.default.SQL.runQuery(query);
27+
return result;
28+
} catch (error) {
29+
console.error("Error executing MindsDB query:", error);
30+
throw error;
31+
}
32+
}

packages/shared/src/lib/mindsdb/mindsdb.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

self-host/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# Required environment variables for the KBNet self-hosted instance
33
DATABASE_URL=postgresql://mindsdb:mindsdb@kbnet_database:5432/mindsdb
44

5-
NEXT_PUBLIC_SERVER_URL=http://kbnet_server:8000
6-
NEXT_PUBLIC_WS_SERVER_URL=ws://kbnet_server:8000
5+
NEXT_PUBLIC_SERVER_URL=http://localhost:8000
6+
NEXT_PUBLIC_WS_SERVER_URL=ws://localhost:8000
77

88
BETTER_AUTH_SECRET=auth_secret_token
99
BETTER_AUTH_URL=http://localhost:3000

self-host/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ check-db:
5454

5555
prisma-push:
5656
@echo "🔄 Running Prisma DB push..."
57+
export DATABASE_URL=$(grep DATABASE_URL .env | cut -d '=' -f2-)
58+
@if [ -z "$$DATABASE_URL" ]; then echo "❌ DATABASE_URL is not set in .env file"; exit 1; fi
59+
@echo "Using DATABASE_URL: $$DATABASE_URL"
60+
@echo "Running Prisma DB push..."
5761
npx prisma db push --skip-generate
5862

5963
check-mindsdb:

self-host/seed/src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import { connectMindsDB } from "./lib/mindsdb.js";
88
export async function seed() {
99
try {
1010
await connectMindsDB();
11+
await kb.createKB();
12+
await datasources.createDatasource();
1113
await jobs.createPendingSummaryView();
14+
await ml.setup();
1215
await jobs.createSummaryGenerationJob();
13-
await datasources.createDatasource();
14-
await kb.createKB();
1516
await agents.createAgent();
16-
await ml.setup();
1717
console.log("MindsDB setup completed successfully.");
1818
} catch (error) {
1919
console.log(error);

0 commit comments

Comments
 (0)