generated from Dicken/dickendock
Erstes lauffähiges Grundgerüst: Monorepo, Fastify-API, React-Startseite, Docker
This commit is contained in:
70
apps/backend/src/db/client.ts
Normal file
70
apps/backend/src/db/client.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { existsSync, mkdirSync } from "node:fs";
|
||||
import { dirname } from "node:path";
|
||||
import Database from "better-sqlite3";
|
||||
import { drizzle } from "drizzle-orm/better-sqlite3";
|
||||
import * as schema from "./schema.js";
|
||||
|
||||
const databasePath = process.env.DATABASE_PATH ?? "./data/launchpad.db";
|
||||
|
||||
// Stellt sicher, dass das Verzeichnis für die SQLite-Datei existiert
|
||||
// (relevant im Docker-Volume /data).
|
||||
const dir = dirname(databasePath);
|
||||
if (dir !== "." && !existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
const sqlite = new Database(databasePath);
|
||||
sqlite.pragma("journal_mode = WAL");
|
||||
sqlite.pragma("foreign_keys = ON");
|
||||
|
||||
export const db = drizzle(sqlite, { schema });
|
||||
|
||||
/**
|
||||
* Legt die Tabellen an, falls sie noch nicht existieren.
|
||||
* Für den ersten Commit reicht ein einfaches "CREATE TABLE IF NOT EXISTS",
|
||||
* spätere Commits ersetzen dies durch echte Drizzle-Migrationen (drizzle-kit).
|
||||
*/
|
||||
export function ensureSchema(): void {
|
||||
sqlite.exec(`
|
||||
CREATE TABLE IF NOT EXISTS devices (
|
||||
id TEXT PRIMARY KEY,
|
||||
hostname TEXT NOT NULL,
|
||||
ip TEXT NOT NULL,
|
||||
mac TEXT,
|
||||
manufacturer TEXT,
|
||||
model TEXT,
|
||||
online INTEGER NOT NULL DEFAULT 0,
|
||||
source TEXT NOT NULL,
|
||||
last_scan TEXT,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS services (
|
||||
id TEXT PRIMARY KEY,
|
||||
device_id TEXT NOT NULL REFERENCES devices(id) ON DELETE CASCADE,
|
||||
display_name TEXT NOT NULL,
|
||||
category TEXT,
|
||||
favorite INTEGER NOT NULL DEFAULT 0,
|
||||
"order" REAL NOT NULL DEFAULT 0,
|
||||
alias TEXT NOT NULL DEFAULT '[]',
|
||||
icon TEXT,
|
||||
hostname TEXT NOT NULL,
|
||||
url TEXT NOT NULL,
|
||||
https INTEGER NOT NULL DEFAULT 0,
|
||||
port INTEGER NOT NULL,
|
||||
favicon TEXT,
|
||||
description TEXT,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS categories (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
"order" REAL NOT NULL DEFAULT 0,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
`);
|
||||
}
|
||||
60
apps/backend/src/db/schema.ts
Normal file
60
apps/backend/src/db/schema.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { sqliteTable, text, integer, real } from "drizzle-orm/sqlite-core";
|
||||
|
||||
/**
|
||||
* Ein physisches/logisches Gerät im Netzwerk (z. B. per FritzBox-Scan gefunden).
|
||||
*/
|
||||
export const devices = sqliteTable("devices", {
|
||||
id: text("id").primaryKey(),
|
||||
hostname: text("hostname").notNull(),
|
||||
ip: text("ip").notNull(),
|
||||
mac: text("mac"),
|
||||
manufacturer: text("manufacturer"),
|
||||
model: text("model"),
|
||||
online: integer("online", { mode: "boolean" }).notNull().default(false),
|
||||
source: text("source").notNull(), // fritzbox | dns | http | https | portscan | manual
|
||||
lastScan: text("last_scan"), // ISO-8601
|
||||
createdAt: text("created_at").notNull(),
|
||||
updatedAt: text("updated_at").notNull(),
|
||||
});
|
||||
|
||||
/**
|
||||
* Ein Dienst, der auf einem Gerät läuft (z. B. Frigate, Portainer, Gitea ...).
|
||||
* Benutzeränderungen (displayName, category, favorite, order, alias, icon)
|
||||
* dürfen von Scans NIEMALS überschrieben werden – siehe README/Spezifikation.
|
||||
*/
|
||||
export const services = sqliteTable("services", {
|
||||
id: text("id").primaryKey(),
|
||||
deviceId: text("device_id")
|
||||
.notNull()
|
||||
.references(() => devices.id, { onDelete: "cascade" }),
|
||||
|
||||
// vom Benutzer gepflegt, wird von Scans nicht überschrieben
|
||||
displayName: text("display_name").notNull(),
|
||||
category: text("category"),
|
||||
favorite: integer("favorite", { mode: "boolean" }).notNull().default(false),
|
||||
order: real("order").notNull().default(0),
|
||||
alias: text("alias").notNull().default("[]"), // JSON-Array als String
|
||||
icon: text("icon"),
|
||||
|
||||
// von Scans aktualisierbar
|
||||
hostname: text("hostname").notNull(),
|
||||
url: text("url").notNull(),
|
||||
https: integer("https", { mode: "boolean" }).notNull().default(false),
|
||||
port: integer("port").notNull(),
|
||||
favicon: text("favicon"),
|
||||
description: text("description"),
|
||||
|
||||
createdAt: text("created_at").notNull(),
|
||||
updatedAt: text("updated_at").notNull(),
|
||||
});
|
||||
|
||||
/**
|
||||
* Frei definierbare Kategorien (Erstellen/Löschen/Umbenennen/Sortieren per Drag & Drop).
|
||||
*/
|
||||
export const categories = sqliteTable("categories", {
|
||||
id: text("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
order: real("order").notNull().default(0),
|
||||
createdAt: text("created_at").notNull(),
|
||||
updatedAt: text("updated_at").notNull(),
|
||||
});
|
||||
41
apps/backend/src/index.ts
Normal file
41
apps/backend/src/index.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import Fastify from "fastify";
|
||||
import cors from "@fastify/cors";
|
||||
import { ensureSchema } from "./db/client.js";
|
||||
import { healthRoutes } from "./routes/health.js";
|
||||
|
||||
const PORT = Number(process.env.PORT ?? 3001);
|
||||
const HOST = process.env.HOST ?? "0.0.0.0";
|
||||
|
||||
async function main() {
|
||||
const app = Fastify({
|
||||
logger: {
|
||||
level: process.env.LOG_LEVEL ?? "info",
|
||||
transport:
|
||||
process.env.NODE_ENV !== "production"
|
||||
? { target: "pino-pretty", options: { colorize: true } }
|
||||
: undefined,
|
||||
},
|
||||
});
|
||||
|
||||
await app.register(cors, {
|
||||
origin: process.env.CORS_ORIGIN ?? true,
|
||||
});
|
||||
|
||||
ensureSchema();
|
||||
|
||||
await app.register(healthRoutes);
|
||||
|
||||
app.get("/", async () => {
|
||||
return { name: "LaunchPad API", status: "running" };
|
||||
});
|
||||
|
||||
try {
|
||||
await app.listen({ port: PORT, host: HOST });
|
||||
app.log.info(`LaunchPad backend läuft auf http://${HOST}:${PORT}`);
|
||||
} catch (err) {
|
||||
app.log.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
15
apps/backend/src/routes/health.ts
Normal file
15
apps/backend/src/routes/health.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import type { HealthStatus } from "@launchpad/shared";
|
||||
|
||||
const startedAt = Date.now();
|
||||
|
||||
export async function healthRoutes(app: FastifyInstance): Promise<void> {
|
||||
app.get("/api/health", async (): Promise<HealthStatus> => {
|
||||
return {
|
||||
status: "ok",
|
||||
timestamp: new Date().toISOString(),
|
||||
uptimeSeconds: Math.round((Date.now() - startedAt) / 1000),
|
||||
version: "0.1.0",
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user