Compare commits

..

1 commit
main ... minify

Author SHA1 Message Date
fdeecd0e26
express: minify the javascript
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2023-09-15 13:51:06 -04:00
15 changed files with 2280 additions and 5544 deletions

View file

@ -1,23 +0,0 @@
name: Basic Checking
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
on: [push]
jobs:
Explore-Gitea-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!"
- run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
- name: Check out repository code
uses: actions/checkout@v3
- run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner."
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
- name: Install packages
run: npm install
- name: Run tests
run: npm test
- run: echo "🍏 This job's status is ${{ job.status }}."

View file

@ -1,65 +0,0 @@
{
"animals": [
"Squirrel",
"Armadillo",
"Beaver",
"Cricket",
"Eagle",
"Ferret",
"Fox",
"Frog",
"Guinea Pig",
"Hamster",
"Hummingbird",
"Iguana",
"Mouse",
"Muskrat",
"Newt",
"Otter",
"Parakeet",
"Penguin",
"Pigeon",
"Possum",
"Rabbit",
"Raccoon",
"Rat",
"Snake",
"Spider",
"Turtle",
"Wolf pup",
"Squirrel",
"Possum",
"Frog",
"Rat",
"Otter",
"Raccoon",
"Snake",
"Ferret",
"Mouse",
"Armadillo",
"Hamster",
"Fox",
"Iguana",
"Spider",
"Turtle",
"Beaver",
"Rabbit",
"Eagle",
"Pigeon",
"Parakeet",
"Hummingbird",
"Penguin",
"Cricket",
"Newt",
"Muskrat",
"Guinea Pig",
"Quokka",
"Fennec Fox",
"Flying Squirrel",
"Hedgehog",
"Badger",
"Koala Bear",
"Porcupine",
"Skunk"
]
}

View file

@ -1,29 +0,0 @@
// app.test.js
const request = require('supertest');
const app = require('./app');
describe('GET /', () => {
it('should respond with 200 status', async () => {
const response = await request(app).get('/');
expect(response.statusCode).toBe(200);
//expect(response.body.message).toBe('Hello, World!');
});
});
describe('GET /asset/frontend.js', () => {
it('should respond with 200 status', async () => {
const response = await request(app).get('/asset/frontend.js');
expect(response.statusCode).toBe(200);
//expect(response.body.message).toBe('Hello, World!');
});
});
describe('GET /getNextAnimal', () => {
it('should respond with 200 status', async () => {
const response = await request(app).get('/getNextAnimal');
expect(response.statusCode).toBe(200);
//expect(response.body.message).toBe('Hello, World!');
});
});
// vim:set sts=2 sw=2 et:

View file

@ -1,17 +0,0 @@
var config = module.exports;
var PRODUCTION = process.env.NODE_ENV === 'production';
const bole = require('bole');
config.express = {
port: process.env.EXPRESS_PORT || 3000,
ip: '127.0.0.1',
};
if (PRODUCTION) {
config.express.ip = '0.0.0.0';
config.db_path = "db/results.db";
bole.output({ level: 'info', stream: process.stdout })
} else {
config.db_path = ":memory:";
bole.output({ level: 'debug', stream: process.stdout })
}

31
express/animals.json Normal file
View file

@ -0,0 +1,31 @@
{
"animals": [
"Squirrel",
"Armadillo",
"Beaver",
"Cricket",
"Eagle",
"Ferret",
"Fox",
"Frog",
"Guinea Pig",
"Hamster",
"Hummingbird",
"Iguana",
"Mouse",
"Muskrat",
"Newt",
"Otter",
"Parakeet",
"Penguin",
"Pigeon",
"Possum",
"Rabbit",
"Raccoon",
"Rat",
"Snake",
"Spider",
"Turtle",
"Wolf pup"
]
}

1
express/asset/frontend.min.js vendored Normal file
View file

@ -0,0 +1 @@
let a;let b;function c(){return (Math.random().toString(36).substring(2,15)+Math.random().toString(36).substring(2,15))}async function d(){try {var A=await fetch('/getNextAnimal');var _=await A.json();document.getElementById('animal-name').textContent=_.animalName} catch (B) {console.error('Error fetching data:',B)}}function e(){var A=document.cookie.replace(/(?:(?:^|.*;\s*)sessionId\s*=\s*([^;]*).*$)|^.*$/,'$1');if(!A){var _=c();document.cookie=`sessionId=${_}`;return _}return A}function f(){var A=c();document.cookie=`sessionId=${A}`;g();e()}function g(){a=new Date()}function h(){if(a&&b){var A=b-a;console.log(`Time since session start: ${A} milliseconds`)}}document.getElementById('isCritterButton').addEventListener('click',()=>i('is critter',e()));document.getElementById('isNotCritterButton').addEventListener('click',()=>i('is not critter',e()));document.getElementById('startOverButton').addEventListener('click',()=>{f();d()});async function i(A,_){try {var B=new Date();if(b){var C=B-b,_b=document.getElementById('animal-name').textContent,_c=JSON.stringify({animal:_b,button:A,session:_,difference:C,time:a});await fetch('/recordButtonClick',{method:'POST',headers:{'Content-Type':'application/json'},body:_c})}b=B;h();d()} catch (_a) {console.error('Error recording button click:',_a)}}d();g();

View file

@ -6,7 +6,7 @@
</head>
<body>
<div id="isCritter">
<h3>Is this a Critter?</h3>
<h1>Is this a Critter?</h1>
<p id="animal-name">what about?:</p>
<button id="isCritterButton">Is Critter</button>
<button id="isNotCritterButton">Is <b>not</b> Critter</button>
@ -21,11 +21,8 @@
<div id="footer">
© 2023. All rights reserved. <a href="mailto:isacritter@hashbangbash.com">isacritter</a>; <a href="https://paypal.me/vbatts/1" target="_blank">keep isacritter alive</a>
</div>
<script src="/asset/frontend.js"></script>
<script src="/asset/frontend.min.js"></script>
<style type="text/css">
#animal-name {
font-size: 25px;
}
#isCritter {
text-align: center;
}

2222
express/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -4,9 +4,9 @@
"description": "",
"main": "server.js",
"scripts": {
"test": "./node_modules/.bin/jest",
"start": "node server.js",
"act": "act -W ./.gitea/workflows/"
"test": "echo \"Error: no test specified\" && exit 1",
"minify": "cat ./asset/frontend.js | ./node_modules/.bin/minify --js > asset/frontend.min.js",
"start": "node server.js"
},
"repository": {
"type": "git",
@ -16,13 +16,9 @@
"license": "MIT",
"dependencies": {
"body-parser": "^1.20.2",
"bole": "^5.0.7",
"express": "^4.18.2",
"minify": "^10.5.2",
"morgan": "^1.10.0",
"sqlite3": "^5.1.6"
},
"devDependencies": {
"jest": "^29.7.0",
"supertest": "^6.3.3"
}
}

View file

@ -4,18 +4,15 @@ const path = require("path");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const sqlite3 = require("sqlite3").verbose();
const bole = require('bole');
const log = bole('app');
const config = require('./config');
const app = express();
const port = 3000;
// Create an SQLite database and initialize tables
const db = new sqlite3.Database(config.db_path, (err) => {
const db = new sqlite3.Database("db/results.db", (err) => {
if (err) {
log.error("Error opening SQLite database:", err.message);
console.error("Error opening SQLite database:", err.message);
} else {
log.info("Connected to SQLite database", config.db_path);
console.log("Connected to SQLite database");
db.run(`
CREATE TABLE IF NOT EXISTS button_clicks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@ -54,24 +51,24 @@ try {
var jsondata = JSON.parse(data);
animals = jsondata.animals;
for (const animal of animals) {
db.run(`
INSERT INTO animals(name)
SELECT ?
WHERE NOT EXISTS(SELECT 1 FROM animals WHERE name = ?);
db.run(
`
INSERT INTO animals(name)
SELECT '${animal}'
WHERE NOT EXISTS(SELECT 1 FROM animals WHERE name = '${animal}');
`,
[animal, animal],
(err) => {
if (err) {
log.error(`Error inserting animal ${animal}: `, err.message);
console.error(`Error inserting animal ${animal}: `, err.message);
} else {
log.info(`Success inserting animal ${animal}`);
console.log(`Success inserting animal ${animal}`);
}
},
);
}
});
} catch (error) {
log.error("Error loading animals:", error);
console.error("Error loading animals:", error);
animals = ["Dog", "Cat", "Elephant", "Lion", "Giraffe"];
}
@ -91,7 +88,7 @@ app.get("/getNextAnimal", async (req, res) => {
const randomAnimal = animals[randomIndex];
res.json({ animalName: randomAnimal });
} catch (error) {
log.error("Error fetching random animal:", error);
console.error("Error fetching random animal:", error);
res.status(500).json({ error: "Internal server error" });
}
});
@ -101,7 +98,7 @@ app.post("/recordButtonClick", (req, res) => {
try {
//const { buttonName, sessionId } = req.body;
const result = req.body;
log.error(result);
console.error(result);
db.run(
"INSERT INTO button_clicks (session_id, animal_name, button_name, timestamp, time_difference) VALUES (?, ?, ?, ?, ?)",
@ -114,7 +111,7 @@ app.post("/recordButtonClick", (req, res) => {
],
(err) => {
if (err) {
log.error("Error recording button click:", err.message);
console.error("Error recording button click:", err.message);
res.status(500).json({ error: "Internal server error" });
} else {
res.sendStatus(200);
@ -122,7 +119,7 @@ app.post("/recordButtonClick", (req, res) => {
},
);
} catch (error) {
log.error("Error recording button click:", error);
console.error("Error recording button click:", error);
res.status(500).json({ error: "Internal server error" });
}
});
@ -203,11 +200,11 @@ app.get("/results", async (req, res) => {
await Promise.all([getCount, getTotalAvgTime, getAvgTime]);
res.json(results);
} catch (error) {
log.error("Error fetching results:", error);
console.error("Error fetching results:", error);
res.status(500).json({ error: "Internal server error" });
}
});
module.exports = app;
// vim:set sts=2 sw=2 et:
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});

5359
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,15 +0,0 @@
const config = require('./config');
const app = require('./app');
const bole = require('bole');
const log = bole('server');
app.listen(config.express.port, config.express.ip, function(error) {
if (error) {
log.error('Unable to listen for connections', error);
process.exit(10);
}
log.info('express is listening on http://' +
config.express.ip + ':' + config.express.port)
});
// vim:set sts=2 sw=2 et: