feat: add basic frontend

This commit is contained in:
2024-12-24 15:25:16 +09:00
parent a06e72d24a
commit ab74b2fcfa
10 changed files with 540 additions and 57 deletions

View File

@ -1,17 +0,0 @@
import express, { Request, Response } from 'express'
export class APIServer {
private readonly router =
express.Router()
constructor () {
this.router.get('/bp_status', this.getBPStatus.bind(this))
}
private getBPStatus (req: Request, res: Response) {
res.send([])
}
public getRouter = () =>
this.router
}

68
src/BPManager.ts Normal file
View File

@ -0,0 +1,68 @@
import { BPSet, BPSetMetadata } from "./types";
import { readdir, readFile } from 'node:fs/promises'
import path from 'node:path'
export class BPManager {
private static _instance = new BPManager()
public static getInstance = () =>
this._instance
// ---
private readonly bpSets:
Record<string, BPSet | undefined> = {}
private readonly bpSetMetadatas:
Record<string, BPSetMetadata> = {}
private constructor() {
this.loadBPSets()
this.loadBPSetMetadatas()
}
private async loadBPSets() {
const bpSetFiles = await readdir('./dist/bpsets', {
recursive: true,
withFileTypes: true
})
for (const bpSetFile of bpSetFiles) {
if (bpSetFile.isDirectory())
continue
const bpSetPath = path.join(bpSetFile.parentPath, bpSetFile.name)
const bpSetClasses = await import('../' + bpSetPath) as Record<string, BPSet>
for (const bpSetClass of Object.keys(bpSetClasses))
this.bpSets[bpSetClass] = bpSetClasses[bpSetClass]
}
}
private async loadBPSetMetadatas() {
const bpSetMetadatasRaw = await readFile('./bpset_metadata.json')
const bpSetMetadatas = JSON.parse(bpSetMetadatasRaw.toString('utf-8')) as BPSetMetadata[]
for (const [idx, bpSetMetadata] of bpSetMetadatas.entries()) {
this.bpSetMetadatas[bpSetMetadata.name] = {
...bpSetMetadata,
nonCompliantResources: [],
compliantResources: [],
status:'LOADED',
idx
}
}
}
public readonly getBPSet = (name: string) =>
this.bpSets[name]
public readonly getBPSetMetadata = (name: string) =>
this.bpSetMetadatas[name]
public readonly getBPSets = () =>
Object.values(this.bpSets)
public readonly getBPSetMetadatas = () =>
Object.values(this.bpSetMetadatas)
}

View File

@ -1,21 +1,44 @@
import express, { Request, Response } from 'express'
import { APIServer } from './APIServer'
import { BPManager } from './BPManager'
import { BPSetMetadata } from './types'
export class WebServer {
private readonly app = express()
private readonly apiServer = new APIServer()
private readonly bpManager =
BPManager.getInstance()
constructor (
private readonly port = 2424
) {
this.initRoutes()
this.app.set('view engine', 'ejs')
this.app.set('views', './views');
this.app.use(express.static('./public'))
this.app.get('/', this.getMainPage.bind(this))
this.app.use(this.error404)
this.app.listen(this.port, this.showBanner.bind(this))
}
private initRoutes () {
this.app.use(express.static('./public'))
this.app.use('/api', this.apiServer.getRouter())
this.app.use(this.error404)
private getMainPage(_: Request, res: Response) {
const bpStatus: {
category: string,
metadatas: BPSetMetadata[]
}[] = []
const bpMetadatas = this.bpManager.getBPSetMetadatas()
const categories = new Set(bpMetadatas.map((v) => v?.awsService))
for (const category of categories)
bpStatus.push({
category,
metadatas: bpMetadatas.filter((v) => v.awsService === category)
})
res.render('index', {
bpStatus,
bpLength: bpMetadatas.length
})
}
private error404 (_: Request, res: Response) {

File diff suppressed because it is too large Load Diff

4
src/types.d.ts vendored
View File

@ -47,4 +47,8 @@ export interface BPSetMetadata {
reason: string
}[]
adviseBeforeFixFunction: string
nonCompliantResources: string[]
compliantResources: string[]
status: 'LOADED' | 'CHECKING' | 'ERROR' | 'FINISHED'
idx: number
}