Merge branch 'main' into rework/cards

This commit is contained in:
Neshura 2023-03-15 18:44:55 +01:00
commit 3f077029c6
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
13 changed files with 166 additions and 172 deletions

View file

@ -14,7 +14,8 @@ export default function About() {
About
</PageTitle>
<PageDescription>
This website is primarily for managing my game servers in one spot
I&apos;m currently expanding what I want to do with this site.
Currently a list of available services and servers is available via the respective navbar entry.
</PageDescription>
</>
)

View file

@ -1,13 +1,29 @@
import Docker from 'dockerode'
import ApiSecret from '../../private/portainer_api_secret.json'
import { DockerInfo } from '../../interfaces/DockerStatus';
import { ServiceLocation } from '../../interfaces/CardTypes';
export default async function ContainersAPI(req: any, res: any) {
const token = JSON.parse(JSON.stringify(ApiSecret.token));
try {
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 })
const res1 = await fetch('https://portainer.neshweb.net/api/endpoints/2/docker/containers/json', {
method: "GET",
headers: {"X-API-Key": token}
});
const unparsed = await res1.json();
let list: DockerInfo[] = [];
unparsed.forEach((entry: any) => {
let newEntry = {} as DockerInfo;
newEntry.name = entry.Names[0].substring(1);
newEntry.status = entry.State;
newEntry.id = entry.Id;
newEntry.location = ServiceLocation.tower_0;
list.push(newEntry);
});
res.status(200).json(list);
}

View file

@ -1,7 +1,7 @@
import Head from 'next/head'
import { Game } from '../interfaces/CardTypes';
import { PageContentBox, PageDescription, PageTitle, CardContentGame } from '../components/styles/content'
import GameList from '../public/pages.json';
import GameList from '../public/data/pages.json';
function Servers() {
// TODO: unuggly this shit
@ -9,7 +9,7 @@ function Servers() {
return (
<>
<Head>
<title>Neshweb - Games</title>
<title>Neshweb - Servers</title>
<meta charSet='utf-8' />
<link rel="icon" href="/favicon.ico" />
</Head>

View file

@ -3,10 +3,9 @@ import { Service, Status, ServiceType, ServiceLocation } from '../interfaces/Car
import Dockerode from 'dockerode';
import { ReactElement } from 'react'
import useSWR from 'swr';
import { CardContentService, PageContentBoxNew as PageContentBox, PageDescription, PageTitle } from '../components/styles/content';
import { ServiceCardMobile } from '../components/styles/cards/mobile';
import ServiceList from '../public/pages.json';
import useWindowSize from '../components/windowsize';
import ServiceList from '../public/data/pages.json';
import { DockerInfo } from '../interfaces/DockerStatus';
import { CardContentService, PageContentBox, PageDescription, PageTitle } from '../components/styles/content';
const fetcher = (url: string) => fetch(url).then((res) => res.json())
@ -79,82 +78,12 @@ function Services() {
)
}
async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) {
async function getStatus(entry: Service, containers: DockerInfo[]) {
// Currently the only location supporting different fetching depending on type is brr7-4800u
// Others to follow but low prio as this is currently the only location used
// Location BRR7-4800U
if (entry.location === ServiceLocation.brr7_4800u) {
// Type APP
if (entry.type === ServiceType.app) {
await fetch(entry.href)
.then((response) => {
if (response.ok) {
switch (response.status) {
case 200:
case 301:
case 302:
entry.status = Status.online;
break;
default:
entry.status = Status.offline;
}
}
else {
entry.status = Status.offline;
}
})
.catch((error) => {
console.error("Error pinging Website: ", error);
entry.status = Status.error;
})
}
// Type Docker
else if (entry.type === ServiceType.docker) {
if (entry.name !== null) {
let found = false;
for (let i = 0; i < containers.length; i++) {
const container = containers[i];
// Docker API returns container names with / prepended
if (containers[i].Names.includes("/" + entry.docker_container_name)) {
// so far only "running" is properly implemented, mroe cases to follow as needed
switch (container.State) {
case "running":
entry.status = Status.online;
break;
default:
console.log("Container Status " + container.State + " has no case implemented");
entry.status = Status.offline;
}
found = true;
// cancel the for
break;
}
// If container name is not missing the container is set to offline
else {
entry.status = Status.offline;
}
}
if (!found) {
console.warn("Container for " + entry.name + " could not be found");
}
}
// if name is null do not enter for loop
else {
console.error("Container Name not specified");
entry.status = Status.error;
}
}
// If no Type matches
else {
console.warn("Service Type for Service " + entry.name + " not specified or invalid");
entry.status = Status.error;
}
}
// Location Other
// TODO: implement docker type for other locations
else if (entry.location === ServiceLocation.other) {
// Currently uses the same handling as app type for the other location
// Type APP
if (entry.type === ServiceType.app) {
await fetch(entry.href)
.then((response) => {
if (response.ok) {
@ -177,15 +106,54 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[])
entry.status = Status.error;
})
}
// If no Location matches
// Type Docker
else if (entry.type === ServiceType.docker) {
if (entry.name !== null) {
let found = false;
for (let i = 0; i < containers.length; i++) {
const container = containers[i];
// Docker API returns container names with / prepended
if (container.name === entry.docker_container_name) {
if (container.location === entry.location) {
// so far only "running" is properly implemented, mroe cases to follow as needed
switch (container.status) {
case "running":
entry.status = Status.online;
break;
default:
console.log("Container Status " + container.status + " has no case implemented");
entry.status = Status.offline;
}
found = true;
// cancel the for
break;
}
}
// If container name is not missing the container is set to offline
else {
entry.status = Status.offline;
}
}
if (!found) {
console.warn("Container for " + entry.name + " could not be found");
}
}
// if name is null do not enter for loop
else {
console.error("Container Name not specified");
entry.status = Status.error;
}
}
// If no Type matches
else {
console.warn("Service Location for Service " + entry.name + " not specified");
console.warn("Service Type for Service " + entry.name + " not specified or invalid");
entry.status = Status.error;
}
return entry;
}
const fetchFullDataArray = (containerData: Dockerode.ContainerInfo[], dataSet: Service[]) => {
const fetchFullDataArray = (containerData: DockerInfo[], dataSet: Service[]) => {
const fetchStatus = (entry: Service) => getStatus(entry, containerData);
return Promise.all(dataSet.map(fetchStatus));
}
@ -207,4 +175,4 @@ function useServices() {
};
}
export default Services
export default Services