From ee3c495bc45fa0bcbd490fb5fc72ce519b4b9ece Mon Sep 17 00:00:00 2001 From: levg34 Date: Fri, 3 Jun 2022 22:37:37 +0200 Subject: [PATCH] get config as JSON from any config file --- pages/api/config/[file].ts | 32 ++++++++++++++++++++++++++++++++ pages/resources.tsx | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 pages/api/config/[file].ts diff --git a/pages/api/config/[file].ts b/pages/api/config/[file].ts new file mode 100644 index 0000000..ea3c43f --- /dev/null +++ b/pages/api/config/[file].ts @@ -0,0 +1,32 @@ +import type { NextApiRequest, NextApiResponse } from 'next' +import { getSession } from 'next-auth/react' +import { getLogger } from '@utils/loggerUtils' +import { getSSHClient } from '@utils/sshUtils' +import ServerConfig, { ServerConfigType } from '@classes/ServerConfig' + +import usersConfig from '@config/usersConfig.json' + +const logger = getLogger('config.ts') + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + try { + const session = await getSession({ req }) + if (!session) return res.status(401).json({error: 'Unauthorized'}) + if (!session.user?.email || !usersConfig.admins.includes(session.user?.email)) return res.status(403).json({error: 'Forbidden'}) + + const sshClient = await getSSHClient() + + const { file } = req.query + const response = await sshClient.execCommand(`cat beammp-server/${file}`) + logger.info({response, user: session.user.email}, 'get config') + const configString = response.stdout + const config = new ServerConfig(configString) + res.status(200).json(config.config) + } catch (error) { + logger.error(error) + res.status(500).json({error}) + } +} diff --git a/pages/resources.tsx b/pages/resources.tsx index 513e958..160aa3e 100644 --- a/pages/resources.tsx +++ b/pages/resources.tsx @@ -2,7 +2,7 @@ import { useEffect, useState } from 'react' import { Container, FormControl, InputGroup, Button, Form, ListGroup, Alert, Spinner, Stack, DropdownButton, Dropdown } from 'react-bootstrap' import useSWR from 'swr' import { fetcher } from '@utils/swrUtils' -import { ResourceItem } from './api/resources/list/[folder]' +import { ResourceItem } from '@api/resources/list/[folder]' const ResourcesPage = () => { const {data: resourcesFolders} = useSWR('/api/resources-folders',fetcher)