import * as path from "path"
import * as fs from "fs"
import { staticPath } from "./constants.js"
import { flavors, variety } from "./constants.js"
import { getYaoi } from "./scraper.js"
export async function middleware(req, res, next) {
var filePath = (req.baseUrl + req.path).trim()
if (!filePath.includes(".")) {
if (filePath.charAt(filePath.length - 1) != '/') {
res.redirect(filePath + '/')
return
}
filePath = path.join(filePath, '/index.html')
}
filePath = path.join(staticPath, filePath || 'index.html')
if (fs.existsSync(filePath) && filePath.includes(".html")) {
var data = fs.readFileSync(filePath).toString()
res.contentType(path.basename(filePath))
data = await converter(data)
res.send(data)
}
else {
next()
}
}
export async function converter(html) {
var replacers = {
"FLAVOR_COUNT": flavors.length,
"VARIETY": variety,
"FLAVORS": flavors.join(", ").replace(/,(?=[^,]+$)/, ' &'),
"SPIN_BUTTON": `