|
@ -1,6 +1,6 @@ |
|
|
const express = require('express'); |
|
|
const express = require('express'); |
|
|
const path = require('path'); |
|
|
const path = require('path'); |
|
|
const fs = require('fs') |
|
|
const fs = require('fs'); |
|
|
const bodyParser = require('body-parser'); |
|
|
const bodyParser = require('body-parser'); |
|
|
const crypto = require('crypto'); |
|
|
const crypto = require('crypto'); |
|
|
const { exec } = require('child_process'); |
|
|
const { exec } = require('child_process'); |
|
@ -8,21 +8,25 @@ require('dotenv').config(); |
|
|
|
|
|
|
|
|
const app = express(); |
|
|
const app = express(); |
|
|
const PORT = process.env.PORT || 3000; |
|
|
const PORT = process.env.PORT || 3000; |
|
|
const GITEA_SECRET = process.env.GITEA_SECRET |
|
|
const GITEA_SECRET = process.env.GITEA_SECRET; |
|
|
const counterFilePath = path.join(__dirname, 'public/json/counter.json'); |
|
|
const counterFilePath = path.join(__dirname, 'public/json/counter.json'); |
|
|
|
|
|
|
|
|
function readCounter() { |
|
|
function readCounter() { |
|
|
const data = fs.readFileSync(counterFilePath) |
|
|
const data = fs.readFileSync(counterFilePath); |
|
|
return JSON.parse(data).count; |
|
|
return JSON.parse(data).count; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function writeCounter(count) { |
|
|
function writeCounter(count) { |
|
|
const data = JSON.stringify({ count: count}, null, 2) |
|
|
const data = JSON.stringify({ count: count }, null, 2); |
|
|
fs.writeFileSync(counterFilePath, data) |
|
|
fs.writeFileSync(counterFilePath, data); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Serve static files from the 'public' directory
|
|
|
function IncrementCounter() { |
|
|
app.use(express.static(path.join(__dirname, 'public'))); |
|
|
let count = readCounter(); |
|
|
|
|
|
count++; |
|
|
|
|
|
writeCounter(count); |
|
|
|
|
|
console.log(count); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// Middleware to capture raw body
|
|
|
// Middleware to capture raw body
|
|
|
app.use(bodyParser.json({ |
|
|
app.use(bodyParser.json({ |
|
@ -31,14 +35,11 @@ app.use(bodyParser.json({ |
|
|
} |
|
|
} |
|
|
})); |
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Custom middleware to handle URLs without .html for specific routes
|
|
|
// Custom middleware to handle URLs without .html for specific routes
|
|
|
app.use((req, res, next) => { |
|
|
app.use((req, res, next) => { |
|
|
const urlPath = req.path.split('?')[0]; |
|
|
const urlPath = req.path.split('?')[0]; |
|
|
const htmlRoutes = ['/about', '/list', '/gallery']; |
|
|
const htmlRoutes = ['/about', '/list', '/gallery']; |
|
|
let count = readCounter() |
|
|
|
|
|
count++; |
|
|
|
|
|
writeCounter(count) |
|
|
|
|
|
console.log(count) |
|
|
|
|
|
if (htmlRoutes.includes(urlPath)) { |
|
|
if (htmlRoutes.includes(urlPath)) { |
|
|
req.url += '.html'; |
|
|
req.url += '.html'; |
|
|
} |
|
|
} |
|
@ -48,25 +49,30 @@ app.use((req, res, next) => { |
|
|
|
|
|
|
|
|
// Route to serve the index.html file
|
|
|
// Route to serve the index.html file
|
|
|
app.get('/', (req, res) => { |
|
|
app.get('/', (req, res) => { |
|
|
|
|
|
IncrementCounter() |
|
|
res.sendFile(path.join(__dirname, 'public', 'index.html')); |
|
|
res.sendFile(path.join(__dirname, 'public', 'index.html')); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// Routes to serve the HTML files without .html extension
|
|
|
// Routes to serve the HTML files without .html extension
|
|
|
app.get('/about.html', (req, res) => { |
|
|
app.get('/about.html', (req, res) => { |
|
|
|
|
|
IncrementCounter(); |
|
|
res.sendFile(path.join(__dirname, 'public', 'about.html')); |
|
|
res.sendFile(path.join(__dirname, 'public', 'about.html')); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
app.get('/list.html', (req, res) => { |
|
|
app.get('/list.html', (req, res) => { |
|
|
|
|
|
IncrementCounter(); |
|
|
res.sendFile(path.join(__dirname, 'public', 'list.html')); |
|
|
res.sendFile(path.join(__dirname, 'public', 'list.html')); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
app.get('/gallery.html', (req, res) => { |
|
|
app.get('/gallery.html', (req, res) => { |
|
|
|
|
|
IncrementCounter(); |
|
|
res.sendFile(path.join(__dirname, 'public', 'gallery.html')); |
|
|
res.sendFile(path.join(__dirname, 'public', 'gallery.html')); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// Serve articles without .html extension
|
|
|
// Serve articles without .html extension
|
|
|
app.get('/articles/:articleName', (req, res) => { |
|
|
app.get('/articles/:articleName', (req, res) => { |
|
|
const articleName = req.params.articleName; |
|
|
const articleName = req.params.articleName; |
|
|
|
|
|
IncrementCounter(); |
|
|
res.sendFile(path.join(__dirname, 'public/articles', `${articleName}.html`)); |
|
|
res.sendFile(path.join(__dirname, 'public/articles', `${articleName}.html`)); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
@ -85,7 +91,7 @@ app.post('/api', (req, res) => { |
|
|
const bufferSignature = Buffer.from(signature, 'hex'); |
|
|
const bufferSignature = Buffer.from(signature, 'hex'); |
|
|
const bufferDigest = Buffer.from(digest, 'hex'); |
|
|
const bufferDigest = Buffer.from(digest, 'hex'); |
|
|
|
|
|
|
|
|
console.log(bufferSignature.length, bufferDigest.length) |
|
|
console.log(bufferSignature.length, bufferDigest.length); |
|
|
|
|
|
|
|
|
if (bufferSignature.length === bufferDigest.length && crypto.timingSafeEqual(bufferSignature, bufferDigest)) { |
|
|
if (bufferSignature.length === bufferDigest.length && crypto.timingSafeEqual(bufferSignature, bufferDigest)) { |
|
|
// Secret is valid, update the repository
|
|
|
// Secret is valid, update the repository
|
|
@ -110,6 +116,9 @@ app.use((err, req, res, next) => { |
|
|
res.status(500).send('Something broke!'); |
|
|
res.status(500).send('Something broke!'); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Serve static files from the 'public' directory
|
|
|
|
|
|
app.use(express.static(path.join(__dirname, 'public'))); |
|
|
|
|
|
|
|
|
// Start the server
|
|
|
// Start the server
|
|
|
app.listen(PORT, (err) => { |
|
|
app.listen(PORT, (err) => { |
|
|
if (err) { |
|
|
if (err) { |
|
|