54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
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": `<a href="/spin" id="yaoiButton"><div>S</div><div>P</div><div>I</div><div>N</div></a>`,
|
|
"YAOI": () => {
|
|
var yaoi = getYaoi()
|
|
|
|
if (yaoi) {
|
|
return `<div class="post">${yaoi.html}</div>`
|
|
}
|
|
}
|
|
}
|
|
|
|
for (var key in replacers) {
|
|
html = html.replaceAll(`{${key}}`, replacers[key])
|
|
}
|
|
|
|
return html
|
|
}
|