generated from corysanin/nodejs-web-template
create admin form
This commit is contained in:
34
src/Web.ts
34
src/Web.ts
@@ -41,21 +41,21 @@ class Web {
|
||||
initialize = async () => {
|
||||
// const options = this.options;
|
||||
const app: Express = this.app = express();
|
||||
const installPath = process.env['installstat'] || process.env['INSTALLSTAT'] || path.join(process.cwd(), 'config', 'installs.json');
|
||||
try {
|
||||
this.installs = JSON.parse(await fsp.readFile(process.env['config'] || process.env['CONFIG'] || path.join(process.cwd(), 'config', 'installs.json'), 'utf-8'));
|
||||
this.installs = JSON.parse(await fsp.readFile(installPath, 'utf-8'));
|
||||
}
|
||||
catch(ex) {
|
||||
catch (ex) {
|
||||
console.error(ex);
|
||||
this.installs = [];
|
||||
}
|
||||
|
||||
|
||||
|
||||
app.set('trust proxy', 1);
|
||||
app.set('view engine', 'ejs');
|
||||
app.set('view options', { outputFunctionName: 'echo' });
|
||||
app.use('/assets', express.static('assets', { maxAge: '30 days' }));
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use((_req, res, next) => {
|
||||
crypto.randomBytes(32, (err, randomBytes) => {
|
||||
if (err) {
|
||||
@@ -72,11 +72,15 @@ class Web {
|
||||
res.send('Healthy');
|
||||
});
|
||||
|
||||
const adminParam = process.env['ADMINPARAM'];
|
||||
const adminPass = process.env['ADMINPASS'];
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
const adminMode = adminParam && adminPass && adminParam in req.query && req.query[adminParam] === adminPass;
|
||||
if (req.query?.['utm_medium']) {
|
||||
console.log(`${req.query['utm_medium']} | ${req.headers?.['user-agent']}`);
|
||||
}
|
||||
else {
|
||||
else if(!adminMode){
|
||||
console.log(req.headers?.['user-agent']);
|
||||
}
|
||||
res.render('index', {
|
||||
@@ -87,10 +91,28 @@ class Web {
|
||||
},
|
||||
date: DATE,
|
||||
installs: this.installs,
|
||||
time: TIME
|
||||
time: TIME,
|
||||
adminMode
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/', async (req, res) => {
|
||||
const adminMode = adminParam && adminPass && adminParam in req.query && req.query[adminParam] === adminPass;
|
||||
if (!adminMode) {
|
||||
res.redirect('/');
|
||||
return;
|
||||
}
|
||||
const submit = req.body;
|
||||
if (!('os' in submit && 'form' in submit)) {
|
||||
res.status(400).send('bad request');
|
||||
return;
|
||||
}
|
||||
this.installs.push(submit);
|
||||
console.log(`updating ${installPath}`);
|
||||
await fsp.writeFile(installPath, JSON.stringify(this.installs, null, 2));
|
||||
res.send('ok');
|
||||
});
|
||||
|
||||
app.get('/os', (_, res) => {
|
||||
res.render('os', {
|
||||
page: {
|
||||
|
||||
Reference in New Issue
Block a user