Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.

Commit 2f21ccd

Browse files
committed
feat(config): Enhance config handling and add package.json retrieval
This commit introduces several enhancements to the configuration handling and adds functionality to retrieve the package.json data. The changes include: - Update config to use package.json - Enhanced config handling in `src/handlers/config.ts`: - Updated the getConfig, updateConfig, getPlugins, createbackup, listBackups, downloadbackup, restoreBackup, addHost, updateHost, and removeHost functions to improve error handling and logging. - Added a new getPackage function to retrieve package.json data. - Updated the index file to export new handlers
1 parent b7eb60a commit 2f21ccd

File tree

8 files changed

+323
-313
lines changed

8 files changed

+323
-313
lines changed

src/core/database/database.ts

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ const uid = userInfo().uid;
1313
export let db: Database;
1414

1515
try {
16-
const databasePath = path.join(dataFolder, "dockstatapi.db");
17-
console.log("Database path:", databasePath);
18-
console.log(`Running as: ${username} (${uid}:${gid})`);
16+
const databasePath = path.join(dataFolder, "dockstatapi.db");
17+
console.log("Database path:", databasePath);
18+
console.log(`Running as: ${username} (${uid}:${gid})`);
1919

20-
if (!existsSync(dataFolder)) {
21-
await mkdir(dataFolder, { recursive: true, mode: 0o777 });
22-
console.log("Created data directory:", dataFolder);
23-
}
20+
if (!existsSync(dataFolder)) {
21+
await mkdir(dataFolder, { recursive: true, mode: 0o777 });
22+
console.log("Created data directory:", dataFolder);
23+
}
2424

25-
db = new Database(databasePath, { create: true });
26-
console.log("Database opened successfully");
25+
db = new Database(databasePath, { create: true });
26+
console.log("Database opened successfully");
2727

28-
db.exec("PRAGMA journal_mode = WAL;");
28+
db.exec("PRAGMA journal_mode = WAL;");
2929
} catch (error) {
30-
console.error(`Cannot start DockStatAPI: ${error}`);
31-
process.exit(500);
30+
console.error(`Cannot start DockStatAPI: ${error}`);
31+
process.exit(500);
3232
}
3333

3434
export function init() {
35-
db.exec(`
35+
db.exec(`
3636
CREATE TABLE IF NOT EXISTS backend_log_entries (
3737
timestamp STRING NOT NULL,
3838
level TEXT NOT NULL,
@@ -105,11 +105,11 @@ export function init() {
105105
)
106106
`);
107107

108-
const themeRows = db
109-
.prepare("SELECT COUNT(*) AS count FROM themes")
110-
.get() as { count: number };
108+
const themeRows = db
109+
.prepare("SELECT COUNT(*) AS count FROM themes")
110+
.get() as { count: number };
111111

112-
const defaultCss = `
112+
const defaultCss = `
113113
.root,
114114
#root,
115115
#docs-root {
@@ -125,42 +125,42 @@ export function init() {
125125
}
126126
`;
127127

128-
if (themeRows.count === 0) {
129-
db.prepare(
130-
"INSERT INTO themes (name, creator, vars, tags) VALUES (?,?,?,?)",
131-
).run("default", "Its4Nik", defaultCss, "[default]");
132-
}
133-
134-
const configRow = db
135-
.prepare("SELECT COUNT(*) AS count FROM config")
136-
.get() as { count: number };
137-
138-
if (configRow.count === 0) {
139-
db.prepare(
140-
"INSERT INTO config (keep_data_for, fetching_interval) VALUES (7, 5)",
141-
).run();
142-
}
143-
144-
const hostRow = db
145-
.prepare("SELECT COUNT(*) AS count FROM docker_hosts")
146-
.get() as { count: number };
147-
148-
if (hostRow.count === 0) {
149-
db.prepare(
150-
"INSERT INTO docker_hosts (name, hostAddress, secure) VALUES (?, ?, ?)",
151-
).run("Localhost", "localhost:2375", false);
152-
}
153-
154-
const storeRow = db
155-
.prepare("SELECT COUNT(*) AS count FROM store_repos")
156-
.get() as { count: number };
157-
158-
if (storeRow.count === 0) {
159-
db.prepare("INSERT INTO store_repos (slug, base) VALUES (?, ?)").run(
160-
"DockStacks",
161-
"https://raw.githubusercontent.com/Its4Nik/DockStacks/refs/heads/main/Index.json",
162-
);
163-
}
128+
if (themeRows.count === 0) {
129+
db.prepare(
130+
"INSERT INTO themes (name, creator, vars, tags) VALUES (?,?,?,?)",
131+
).run("default", "Its4Nik", defaultCss, "[default]");
132+
}
133+
134+
const configRow = db
135+
.prepare("SELECT COUNT(*) AS count FROM config")
136+
.get() as { count: number };
137+
138+
if (configRow.count === 0) {
139+
db.prepare(
140+
"INSERT INTO config (keep_data_for, fetching_interval) VALUES (7, 5)",
141+
).run();
142+
}
143+
144+
const hostRow = db
145+
.prepare("SELECT COUNT(*) AS count FROM docker_hosts")
146+
.get() as { count: number };
147+
148+
if (hostRow.count === 0) {
149+
db.prepare(
150+
"INSERT INTO docker_hosts (name, hostAddress, secure) VALUES (?, ?, ?)",
151+
).run("Localhost", "localhost:2375", false);
152+
}
153+
154+
const storeRow = db
155+
.prepare("SELECT COUNT(*) AS count FROM store_repos")
156+
.get() as { count: number };
157+
158+
if (storeRow.count === 0) {
159+
db.prepare("INSERT INTO store_repos (slug, base) VALUES (?, ?)").run(
160+
"DockStacks",
161+
"https://raw.githubusercontent.com/Its4Nik/DockStacks/refs/heads/main/Index.json",
162+
);
163+
}
164164
}
165165

166166
init();

src/core/database/helper.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ import { logger } from "~/core/utils/logger";
22
import { backupInProgress } from "./_dbState";
33

44
export function executeDbOperation<T>(
5-
label: string,
6-
operation: () => T,
7-
validate?: () => void,
8-
dontLog?: boolean,
5+
label: string,
6+
operation: () => T,
7+
validate?: () => void,
8+
dontLog?: boolean,
99
): T {
10-
if (backupInProgress && label !== "backup" && label !== "restore") {
11-
throw new Error(
12-
`backup in progress Database operation not allowed: ${label}`,
13-
);
14-
}
15-
const startTime = Date.now();
16-
if (dontLog !== true) {
17-
logger.debug(`__task__ __db__ ${label} ⏳`);
18-
}
19-
if (validate) {
20-
validate();
21-
}
22-
const result = operation();
23-
const duration = Date.now() - startTime;
24-
if (dontLog !== true) {
25-
logger.debug(`__task__ __db__ ${label} ✔️ (${duration}ms)`);
26-
}
27-
return result;
10+
if (backupInProgress && label !== "backup" && label !== "restore") {
11+
throw new Error(
12+
`backup in progress Database operation not allowed: ${label}`,
13+
);
14+
}
15+
const startTime = Date.now();
16+
if (dontLog !== true) {
17+
logger.debug(`__task__ __db__ ${label} ⏳`);
18+
}
19+
if (validate) {
20+
validate();
21+
}
22+
const result = operation();
23+
const duration = Date.now() - startTime;
24+
if (dontLog !== true) {
25+
logger.debug(`__task__ __db__ ${label} ✔️ (${duration}ms)`);
26+
}
27+
return result;
2828
}

src/core/database/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import * as stores from "~/core/database/stores";
1313
import * as themes from "~/core/database/themes";
1414

1515
export const dbFunctions = {
16-
...dockerHosts,
17-
...logs,
18-
...config,
19-
...containerStats,
20-
...hostStats,
21-
...stacks,
22-
...backup,
23-
...stores,
24-
...themes,
16+
...dockerHosts,
17+
...logs,
18+
...config,
19+
...containerStats,
20+
...hostStats,
21+
...stacks,
22+
...backup,
23+
...stores,
24+
...themes,
2525
};
2626

2727
export type dbFunctions = typeof dbFunctions;

src/core/database/themes.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@ import { db } from "./database";
33
import { executeDbOperation } from "./helper";
44

55
const stmt = {
6-
insert: db.prepare(`
6+
insert: db.prepare(`
77
INSERT INTO themes (name, creator, vars, tags) VALUES (?, ?, ?, ?)
88
`),
9-
remove: db.prepare(`DELETE FROM themes WHERE name = ?`),
10-
read: db.prepare(`SELECT * FROM themes WHERE name = ?`),
11-
readAll: db.prepare(`SELECT * FROM themes`),
9+
remove: db.prepare("DELETE FROM themes WHERE name = ?"),
10+
read: db.prepare("SELECT * FROM themes WHERE name = ?"),
11+
readAll: db.prepare("SELECT * FROM themes"),
1212
};
1313

1414
export function getThemes() {
15-
return executeDbOperation("Get Themes", () => stmt.readAll.all()) as Theme[];
15+
return executeDbOperation("Get Themes", () => stmt.readAll.all()) as Theme[];
1616
}
1717

1818
export function addTheme({ name, creator, vars, tags }: Theme) {
19-
return executeDbOperation("Save Theme", () =>
20-
stmt.insert.run(name, creator, vars, tags.toString()),
21-
);
19+
return executeDbOperation("Save Theme", () =>
20+
stmt.insert.run(name, creator, vars, tags.toString()),
21+
);
2222
}
2323
export function getSpecificTheme(name: string): Theme {
24-
return executeDbOperation(
25-
"Getting specific Theme",
26-
() => stmt.read.get(name) as Theme,
27-
);
24+
return executeDbOperation(
25+
"Getting specific Theme",
26+
() => stmt.read.get(name) as Theme,
27+
);
2828
}
2929

3030
export function deleteTheme(name: string) {
31-
return executeDbOperation("Remove Theme", () => stmt.remove.run(name));
31+
return executeDbOperation("Remove Theme", () => stmt.remove.run(name));
3232
}

0 commit comments

Comments
 (0)