Large Code Rewrite
Added 2 API Endpoints for use with useSWR, removed the service and status fetching from getStaticProps, rewrote status fetching to use async (WIP, needs testing)
This commit is contained in:
parent
89a8278dbe
commit
6af8cb3e1b
3 changed files with 157 additions and 99 deletions
pages/api
22
pages/api/containers.tsx
Normal file
22
pages/api/containers.tsx
Normal file
|
@ -0,0 +1,22 @@
|
|||
import fsPromises from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { Service, ServiceStatus } from '../../interfaces/LinkTypes';
|
||||
|
||||
export default async function ServicesAPI(req: any, res: any) {
|
||||
try {
|
||||
var Docker = require('dockerode');
|
||||
|
||||
const options = {
|
||||
socketPath: '/var/run/docker.sock',
|
||||
path: '/v1.41/containers/json'
|
||||
};
|
||||
var docker = new Docker({ socketPath: options.socketPath });
|
||||
const list = await docker.listContainers({ all: true })
|
||||
|
||||
res.status(200).json(list);
|
||||
}
|
||||
catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).json({ error: 'Error reading data' });
|
||||
}
|
||||
}
|
21
pages/api/services.tsx
Normal file
21
pages/api/services.tsx
Normal file
|
@ -0,0 +1,21 @@
|
|||
import fsPromises from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { Service, ServiceStatus } from '../../interfaces/LinkTypes';
|
||||
|
||||
export default async function ServicesAPI(req: any, res: any) {
|
||||
try {
|
||||
const filePath = path.join(process.cwd(), '/public/pages.json')
|
||||
const data = await fsPromises.readFile(filePath)
|
||||
.then((file) => JSON.parse(file.toString()));
|
||||
data.services.forEach((service: Service) => {
|
||||
service.status = ServiceStatus.loading;
|
||||
});
|
||||
|
||||
res.status(200).json(data.services);
|
||||
}
|
||||
catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).json({ error: 'Error reading data' });
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue