generated from corysanin/nodejs-web-template
create admin form
This commit is contained in:
@@ -7,5 +7,10 @@ services:
|
|||||||
context: ./
|
context: ./
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
restart: "no"
|
restart: "no"
|
||||||
|
environment:
|
||||||
|
ADMINPARAM: password
|
||||||
|
ADMINPASS: letmein
|
||||||
|
volumes:
|
||||||
|
- ./config/:/usr/src/app/config/
|
||||||
ports:
|
ports:
|
||||||
- 8080:8080
|
- 8080:8080
|
||||||
|
|||||||
1
package-lock.json
generated
1
package-lock.json
generated
@@ -9,6 +9,7 @@
|
|||||||
"version": "0.0.5",
|
"version": "0.0.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"body-parser": "^2.2.0",
|
||||||
"ejs": "3.1.10",
|
"ejs": "3.1.10",
|
||||||
"express": "5.1.0",
|
"express": "5.1.0",
|
||||||
"ics": "^3.8.1",
|
"ics": "^3.8.1",
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
"views"
|
"views"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"body-parser": "^2.2.0",
|
||||||
"ejs": "3.1.10",
|
"ejs": "3.1.10",
|
||||||
"express": "5.1.0",
|
"express": "5.1.0",
|
||||||
"ics": "^3.8.1",
|
"ics": "^3.8.1",
|
||||||
|
|||||||
28
scripts/form.js
Normal file
28
scripts/form.js
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
|
const submit = document.getElementById('submitbtn');
|
||||||
|
submit.addEventListener('click', async () => {
|
||||||
|
submit.disabled = true;
|
||||||
|
const action = submit.parentElement.action;
|
||||||
|
try {
|
||||||
|
const resp = await fetch(action, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({
|
||||||
|
os: document.getElementById('os-select').value,
|
||||||
|
form: document.getElementById('form-select').value
|
||||||
|
}),
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!resp.ok) {
|
||||||
|
throw new Error(`Response status: ${resp.status}`);
|
||||||
|
}
|
||||||
|
location.reload();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error(error.message);
|
||||||
|
alert(error.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
32
src/Web.ts
32
src/Web.ts
@@ -41,10 +41,11 @@ class Web {
|
|||||||
initialize = async () => {
|
initialize = async () => {
|
||||||
// const options = this.options;
|
// const options = this.options;
|
||||||
const app: Express = this.app = express();
|
const app: Express = this.app = express();
|
||||||
|
const installPath = process.env['installstat'] || process.env['INSTALLSTAT'] || path.join(process.cwd(), 'config', 'installs.json');
|
||||||
try {
|
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);
|
console.error(ex);
|
||||||
this.installs = [];
|
this.installs = [];
|
||||||
}
|
}
|
||||||
@@ -55,7 +56,6 @@ class Web {
|
|||||||
app.set('view options', { outputFunctionName: 'echo' });
|
app.set('view options', { outputFunctionName: 'echo' });
|
||||||
app.use('/assets', express.static('assets', { maxAge: '30 days' }));
|
app.use('/assets', express.static('assets', { maxAge: '30 days' }));
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use(bodyParser.urlencoded({ extended: true }));
|
|
||||||
app.use((_req, res, next) => {
|
app.use((_req, res, next) => {
|
||||||
crypto.randomBytes(32, (err, randomBytes) => {
|
crypto.randomBytes(32, (err, randomBytes) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -72,11 +72,15 @@ class Web {
|
|||||||
res.send('Healthy');
|
res.send('Healthy');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const adminParam = process.env['ADMINPARAM'];
|
||||||
|
const adminPass = process.env['ADMINPASS'];
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
|
const adminMode = adminParam && adminPass && adminParam in req.query && req.query[adminParam] === adminPass;
|
||||||
if (req.query?.['utm_medium']) {
|
if (req.query?.['utm_medium']) {
|
||||||
console.log(`${req.query['utm_medium']} | ${req.headers?.['user-agent']}`);
|
console.log(`${req.query['utm_medium']} | ${req.headers?.['user-agent']}`);
|
||||||
}
|
}
|
||||||
else {
|
else if(!adminMode){
|
||||||
console.log(req.headers?.['user-agent']);
|
console.log(req.headers?.['user-agent']);
|
||||||
}
|
}
|
||||||
res.render('index', {
|
res.render('index', {
|
||||||
@@ -87,10 +91,28 @@ class Web {
|
|||||||
},
|
},
|
||||||
date: DATE,
|
date: DATE,
|
||||||
installs: this.installs,
|
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) => {
|
app.get('/os', (_, res) => {
|
||||||
res.render('os', {
|
res.render('os', {
|
||||||
page: {
|
page: {
|
||||||
|
|||||||
@@ -8,6 +8,9 @@
|
|||||||
<body class="preload">
|
<body class="preload">
|
||||||
<%- include("navigation", locals) %>
|
<%- include("navigation", locals) %>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
|
<% if (adminMode) { %>
|
||||||
|
<%- include("installform", locals) %>
|
||||||
|
<% } %>
|
||||||
<% if (installs && installs.length) { %>
|
<% if (installs && installs.length) { %>
|
||||||
<div id="installs">
|
<div id="installs">
|
||||||
<h1>We have saved <span><%= installs.length %></span> computer<% if (installs.length > 1) { %>s<% } %>!</h1>
|
<h1>We have saved <span><%= installs.length %></span> computer<% if (installs.length > 1) { %>s<% } %>!</h1>
|
||||||
|
|||||||
19
views/installform.ejs
Normal file
19
views/installform.ejs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<div>
|
||||||
|
<select name="OS" id="os-select">
|
||||||
|
<option value="mint">Mint</option>
|
||||||
|
<option value="manjaro">Manjaro</option>
|
||||||
|
<option value="chrome">ChromeOS</option>
|
||||||
|
<option value="other">Other</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<select name="form" id="form-select">
|
||||||
|
<option value="laptop">Laptop</option>
|
||||||
|
<option value="desktop">Desktop</option>
|
||||||
|
<option value="aio">AIO</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<form method="post">
|
||||||
|
<input type="button" value="Submit" id="submitbtn" />
|
||||||
|
</form>
|
||||||
|
<script src="/assets/js/form.js?v1" nonce="<%= cspNonce %>"></script>
|
||||||
Reference in New Issue
Block a user