mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-11 07:55:52 +01:00
♻️ Change Docker instance to singleton
This commit is contained in:
19
src/pages/api/docker/DockerSingleton.tsx
Normal file
19
src/pages/api/docker/DockerSingleton.tsx
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import Docker from 'dockerode';
|
||||||
|
|
||||||
|
export default class DockerSingleton extends Docker {
|
||||||
|
private static dockerInstance: DockerSingleton;
|
||||||
|
|
||||||
|
private constructor() {
|
||||||
|
super({
|
||||||
|
host: '192.168.1.56',
|
||||||
|
port: 2376,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static getInstance(): DockerSingleton {
|
||||||
|
if (!this.dockerInstance) {
|
||||||
|
this.dockerInstance = new this();
|
||||||
|
}
|
||||||
|
return this.dockerInstance;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import Docker from 'dockerode';
|
import DockerSingleton from '../DockerSingleton';
|
||||||
|
|
||||||
const docker = new Docker();
|
const docker = DockerSingleton.getInstance();
|
||||||
|
|
||||||
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||||
// Get the slug of the request
|
// Get the slug of the request
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import DockerSingleton from './DockerSingleton';
|
||||||
import Docker from 'dockerode';
|
|
||||||
|
|
||||||
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||||
try {
|
try {
|
||||||
const docker = new Docker();
|
const docker = DockerSingleton.getInstance();
|
||||||
const containers = await docker.listContainers({ all: true });
|
const containers = await docker.listContainers({ all: true });
|
||||||
res.status(200).json(containers);
|
res.status(200).json(containers);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Reference in New Issue
Block a user