AI Implementation feature(820): Project Scaffold and Platform Foundations (#1)
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
})
|
||||
export class FrontendModule {}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Injectable, NestMiddleware } from '@nestjs/common';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
@Injectable()
|
||||
export class SpaFallbackMiddleware implements NestMiddleware {
|
||||
constructor(private readonly config: ConfigService) {}
|
||||
|
||||
use(req: Request, res: Response, next: NextFunction): void {
|
||||
const url = req.path || req.url;
|
||||
if (url.startsWith('/api') || url.startsWith('/uploads')) return next();
|
||||
if (path.extname(url)) return next();
|
||||
if (req.method !== 'GET') return next();
|
||||
|
||||
const distDir = path.resolve(this.config.get<string>('FRONTEND_DIST', '../frontend/dist'));
|
||||
const candidates = [distDir, path.join(distDir, 'browser')];
|
||||
for (const dir of candidates) {
|
||||
const p = path.join(dir, 'index.html');
|
||||
if (fs.existsSync(p)) {
|
||||
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
||||
fs.createReadStream(p).pipe(res);
|
||||
return;
|
||||
}
|
||||
}
|
||||
res.status(200).send('<!doctype html><html><body><h1>HIPCTF</h1><p>Frontend not built.</p></body></html>');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Injectable, NestMiddleware } from '@nestjs/common';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
@Injectable()
|
||||
export class UploadsStaticMiddleware implements NestMiddleware {
|
||||
private rootDir = '/tmp';
|
||||
|
||||
constructor(config: ConfigService) {
|
||||
const dir = path.resolve(config.get<string>('UPLOAD_DIR', '/data/hipctf/uploads'));
|
||||
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
||||
this.rootDir = dir;
|
||||
}
|
||||
|
||||
use(req: Request, res: Response, next: NextFunction): void {
|
||||
if (!req.path.startsWith('/uploads')) return next();
|
||||
const express = require('express');
|
||||
return express.static(this.rootDir)(req, res, next);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user