Skip to content

Commit

Permalink
get config as JSON from any config file
Browse files Browse the repository at this point in the history
  • Loading branch information
levg34 committed Jun 3, 2022
1 parent e722d27 commit ee3c495
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions pages/api/config/[file].ts
Original file line number Diff line number Diff line change
@@ -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<ServerConfigType | {error: any}>
) {
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})
}
}
2 changes: 1 addition & 1 deletion pages/resources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>('/api/resources-folders',fetcher)
Expand Down

0 comments on commit ee3c495

Please sign in to comment.