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

Commit 21e3a80

Browse files
committed
Refactor(handlers): Standardize formatting and imports
This commit improves code readability and consistency across handler files by: - Standardizing import statements to be grouped and ordered. - Ensuring consistent spacing and formatting within the code.
1 parent 956f662 commit 21e3a80

6 files changed

Lines changed: 502 additions & 502 deletions

File tree

src/handlers/config.ts

Lines changed: 184 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,193 @@
1+
import { existsSync, readdirSync, unlinkSync } from "node:fs";
12
import { dbFunctions } from "~/core/database";
2-
import type { config } from "~/typings/database";
3-
import { logger } from "~/core/utils/logger";
4-
import { pluginManager } from "~/core/plugins/plugin-manager";
5-
import { hashApiKey } from "~/middleware/auth";
63
import { backupDir } from "~/core/database/backup";
4+
import { pluginManager } from "~/core/plugins/plugin-manager";
5+
import { logger } from "~/core/utils/logger";
76
import {
8-
authorEmail,
9-
authorName,
10-
authorWebsite,
11-
contributors,
12-
dependencies,
13-
description,
14-
devDependencies,
15-
license,
16-
version,
7+
authorEmail,
8+
authorName,
9+
authorWebsite,
10+
contributors,
11+
dependencies,
12+
description,
13+
devDependencies,
14+
license,
15+
version,
1716
} from "~/core/utils/package-json";
18-
import { existsSync, readdirSync, unlinkSync } from "node:fs";
19-
import { DockerHost } from "~/typings/docker";
17+
import { hashApiKey } from "~/middleware/auth";
18+
import type { config } from "~/typings/database";
19+
import type { DockerHost } from "~/typings/docker";
2020

2121
class apiHandler {
22-
async getConfig() {
23-
try {
24-
const data = dbFunctions.getConfig() as config[];
25-
const distinct = data[0];
26-
27-
logger.debug("Fetched backend config");
28-
return distinct;
29-
} catch (error) {
30-
const errMsg = error instanceof Error ? error.message : String(error);
31-
throw new Error(errMsg);
32-
}
33-
}
34-
35-
async updateConfig(
36-
fetching_interval: number,
37-
keep_data_for: number,
38-
api_key: string
39-
) {
40-
try {
41-
dbFunctions.updateConfig(
42-
fetching_interval,
43-
keep_data_for,
44-
await hashApiKey(api_key)
45-
);
46-
return "Updated DockStatAPI config";
47-
} catch (error) {
48-
const errMsg = error instanceof Error ? error.message : String(error);
49-
throw new Error(errMsg);
50-
}
51-
}
52-
53-
async getPlugins() {
54-
try {
55-
return pluginManager.getPlugins();
56-
} catch (error) {
57-
const errMsg = error instanceof Error ? error.message : String(error);
58-
throw new Error(errMsg);
59-
}
60-
}
61-
62-
async getPackage() {
63-
try {
64-
logger.debug("Fetching package.json");
65-
const data = {
66-
version: version,
67-
description: description,
68-
license: license,
69-
authorName: authorName,
70-
authorEmail: authorEmail,
71-
authorWebsite: authorWebsite,
72-
contributors: contributors,
73-
dependencies: dependencies,
74-
devDependencies: devDependencies,
75-
};
76-
77-
logger.debug(
78-
`Received: ${JSON.stringify(data).length} chars in package.json`
79-
);
80-
81-
if (JSON.stringify(data).length <= 10) {
82-
throw new Error("Failed to read package.json");
83-
}
84-
85-
return data;
86-
} catch (error) {
87-
const errMsg = error instanceof Error ? error.message : String(error);
88-
throw new Error(errMsg);
89-
}
90-
}
91-
92-
async createbackup() {
93-
try {
94-
const backupFilename = await dbFunctions.backupDatabase();
95-
return backupFilename;
96-
} catch (error) {
97-
const errMsg = error instanceof Error ? error.message : String(error);
98-
throw new Error(errMsg);
99-
}
100-
}
101-
102-
async listBackups() {
103-
try {
104-
const backupFiles = readdirSync(backupDir);
105-
106-
const filteredFiles = backupFiles.filter((file: string) => {
107-
return !(
108-
file.startsWith(".") ||
109-
file.endsWith(".db") ||
110-
file.endsWith(".db-shm") ||
111-
file.endsWith(".db-wal")
112-
);
113-
});
114-
115-
return filteredFiles;
116-
} catch (error) {
117-
const errMsg = error instanceof Error ? error.message : String(error);
118-
throw new Error(errMsg);
119-
}
120-
}
121-
122-
async downloadbackup(downloadFile?: string) {
123-
try {
124-
const filename: string = downloadFile || dbFunctions.findLatestBackup();
125-
const filePath = `${backupDir}/${filename}`;
126-
127-
if (!existsSync(filePath)) {
128-
throw new Error("Backup file not found");
129-
}
130-
131-
return Bun.file(filePath);
132-
} catch (error) {
133-
const errMsg = error instanceof Error ? error.message : String(error);
134-
throw new Error(errMsg);
135-
}
136-
}
137-
138-
async restoreBackup(file: Bun.FileBlob) {
139-
try {
140-
if (!file) {
141-
throw new Error("No file uploaded");
142-
}
143-
144-
if (!(file.name || "").endsWith(".db.bak")) {
145-
throw new Error("Invalid file type. Expected .db.bak");
146-
}
147-
148-
const tempPath = `${backupDir}/upload_${Date.now()}.db.bak`;
149-
const fileBuffer = await file.arrayBuffer();
150-
151-
await Bun.write(tempPath, fileBuffer);
152-
dbFunctions.restoreDatabase(tempPath);
153-
unlinkSync(tempPath);
154-
155-
return "Database restored successfully";
156-
} catch (error) {
157-
const errMsg = error instanceof Error ? error.message : String(error);
158-
throw new Error(errMsg);
159-
}
160-
}
161-
162-
async addHost(host: DockerHost) {
163-
try {
164-
dbFunctions.addDockerHost(host);
165-
return `Added docker host (${host.name} - ${host.hostAddress})`;
166-
} catch (error: unknown) {
167-
const errMsg = error instanceof Error ? error.message : String(error);
168-
throw new Error(errMsg);
169-
}
170-
}
171-
172-
async updateHost(host: DockerHost) {
173-
try {
174-
dbFunctions.updateDockerHost(host);
175-
return `Updated docker host (${host.id})`;
176-
} catch (error) {
177-
const errMsg = error instanceof Error ? error.message : String(error);
178-
throw new Error(errMsg);
179-
}
180-
}
181-
182-
async removeHost(id: number) {
183-
try {
184-
dbFunctions.deleteDockerHost(id);
185-
return `Deleted docker host (${id})`;
186-
} catch (error) {
187-
const errMsg = error instanceof Error ? error.message : String(error);
188-
throw new Error(errMsg);
189-
}
190-
}
22+
async getConfig() {
23+
try {
24+
const data = dbFunctions.getConfig() as config[];
25+
const distinct = data[0];
26+
27+
logger.debug("Fetched backend config");
28+
return distinct;
29+
} catch (error) {
30+
const errMsg = error instanceof Error ? error.message : String(error);
31+
throw new Error(errMsg);
32+
}
33+
}
34+
35+
async updateConfig(
36+
fetching_interval: number,
37+
keep_data_for: number,
38+
api_key: string,
39+
) {
40+
try {
41+
dbFunctions.updateConfig(
42+
fetching_interval,
43+
keep_data_for,
44+
await hashApiKey(api_key),
45+
);
46+
return "Updated DockStatAPI config";
47+
} catch (error) {
48+
const errMsg = error instanceof Error ? error.message : String(error);
49+
throw new Error(errMsg);
50+
}
51+
}
52+
53+
async getPlugins() {
54+
try {
55+
return pluginManager.getPlugins();
56+
} catch (error) {
57+
const errMsg = error instanceof Error ? error.message : String(error);
58+
throw new Error(errMsg);
59+
}
60+
}
61+
62+
async getPackage() {
63+
try {
64+
logger.debug("Fetching package.json");
65+
const data = {
66+
version: version,
67+
description: description,
68+
license: license,
69+
authorName: authorName,
70+
authorEmail: authorEmail,
71+
authorWebsite: authorWebsite,
72+
contributors: contributors,
73+
dependencies: dependencies,
74+
devDependencies: devDependencies,
75+
};
76+
77+
logger.debug(
78+
`Received: ${JSON.stringify(data).length} chars in package.json`,
79+
);
80+
81+
if (JSON.stringify(data).length <= 10) {
82+
throw new Error("Failed to read package.json");
83+
}
84+
85+
return data;
86+
} catch (error) {
87+
const errMsg = error instanceof Error ? error.message : String(error);
88+
throw new Error(errMsg);
89+
}
90+
}
91+
92+
async createbackup() {
93+
try {
94+
const backupFilename = await dbFunctions.backupDatabase();
95+
return backupFilename;
96+
} catch (error) {
97+
const errMsg = error instanceof Error ? error.message : String(error);
98+
throw new Error(errMsg);
99+
}
100+
}
101+
102+
async listBackups() {
103+
try {
104+
const backupFiles = readdirSync(backupDir);
105+
106+
const filteredFiles = backupFiles.filter((file: string) => {
107+
return !(
108+
file.startsWith(".") ||
109+
file.endsWith(".db") ||
110+
file.endsWith(".db-shm") ||
111+
file.endsWith(".db-wal")
112+
);
113+
});
114+
115+
return filteredFiles;
116+
} catch (error) {
117+
const errMsg = error instanceof Error ? error.message : String(error);
118+
throw new Error(errMsg);
119+
}
120+
}
121+
122+
async downloadbackup(downloadFile?: string) {
123+
try {
124+
const filename: string = downloadFile || dbFunctions.findLatestBackup();
125+
const filePath = `${backupDir}/${filename}`;
126+
127+
if (!existsSync(filePath)) {
128+
throw new Error("Backup file not found");
129+
}
130+
131+
return Bun.file(filePath);
132+
} catch (error) {
133+
const errMsg = error instanceof Error ? error.message : String(error);
134+
throw new Error(errMsg);
135+
}
136+
}
137+
138+
async restoreBackup(file: Bun.FileBlob) {
139+
try {
140+
if (!file) {
141+
throw new Error("No file uploaded");
142+
}
143+
144+
if (!(file.name || "").endsWith(".db.bak")) {
145+
throw new Error("Invalid file type. Expected .db.bak");
146+
}
147+
148+
const tempPath = `${backupDir}/upload_${Date.now()}.db.bak`;
149+
const fileBuffer = await file.arrayBuffer();
150+
151+
await Bun.write(tempPath, fileBuffer);
152+
dbFunctions.restoreDatabase(tempPath);
153+
unlinkSync(tempPath);
154+
155+
return "Database restored successfully";
156+
} catch (error) {
157+
const errMsg = error instanceof Error ? error.message : String(error);
158+
throw new Error(errMsg);
159+
}
160+
}
161+
162+
async addHost(host: DockerHost) {
163+
try {
164+
dbFunctions.addDockerHost(host);
165+
return `Added docker host (${host.name} - ${host.hostAddress})`;
166+
} catch (error: unknown) {
167+
const errMsg = error instanceof Error ? error.message : String(error);
168+
throw new Error(errMsg);
169+
}
170+
}
171+
172+
async updateHost(host: DockerHost) {
173+
try {
174+
dbFunctions.updateDockerHost(host);
175+
return `Updated docker host (${host.id})`;
176+
} catch (error) {
177+
const errMsg = error instanceof Error ? error.message : String(error);
178+
throw new Error(errMsg);
179+
}
180+
}
181+
182+
async removeHost(id: number) {
183+
try {
184+
dbFunctions.deleteDockerHost(id);
185+
return `Deleted docker host (${id})`;
186+
} catch (error) {
187+
const errMsg = error instanceof Error ? error.message : String(error);
188+
throw new Error(errMsg);
189+
}
190+
}
191191
}
192192

193193
export const ApiHandler = new apiHandler();

src/handlers/database.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { dbFunctions } from "~/core/database";
22

33
class databaseHandler {
4-
async getContainers() {
5-
return dbFunctions.getContainerStats();
6-
}
4+
async getContainers() {
5+
return dbFunctions.getContainerStats();
6+
}
77

8-
async getHosts() {
9-
return dbFunctions.getHostStats();
10-
}
8+
async getHosts() {
9+
return dbFunctions.getHostStats();
10+
}
1111
}
1212

1313
export const DatabaseHandler = new databaseHandler();

0 commit comments

Comments
 (0)