Compare commits
11 commits
Author | SHA1 | Date | |
---|---|---|---|
3cc7a378c0 | |||
e32d3c9233 | |||
6c0c6b071c | |||
d6b3c31348 | |||
b9d9ecaec2 | |||
ef4e816dac | |||
281945a672 | |||
94eac4f89b | |||
0997f578c5 | |||
6215557a7f | |||
6114ef68b0 |
14 changed files with 5543 additions and 1868 deletions
23
.gitea/workflows/basic.yaml
Normal file
23
.gitea/workflows/basic.yaml
Normal file
|
@ -0,0 +1,23 @@
|
|||
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 }}."
|
65
animals.json
Normal file
65
animals.json
Normal file
|
@ -0,0 +1,65 @@
|
|||
{
|
||||
"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"
|
||||
]
|
||||
}
|
|
@ -4,15 +4,18 @@ 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("db/results.db", (err) => {
|
||||
const db = new sqlite3.Database(config.db_path, (err) => {
|
||||
if (err) {
|
||||
console.error("Error opening SQLite database:", err.message);
|
||||
log.error("Error opening SQLite database:", err.message);
|
||||
} else {
|
||||
console.log("Connected to SQLite database");
|
||||
log.info("Connected to SQLite database", config.db_path);
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS button_clicks (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
@ -51,24 +54,24 @@ try {
|
|||
var jsondata = JSON.parse(data);
|
||||
animals = jsondata.animals;
|
||||
for (const animal of animals) {
|
||||
db.run(
|
||||
`
|
||||
INSERT INTO animals(name)
|
||||
SELECT '${animal}'
|
||||
WHERE NOT EXISTS(SELECT 1 FROM animals WHERE name = '${animal}');
|
||||
db.run(`
|
||||
INSERT INTO animals(name)
|
||||
SELECT ?
|
||||
WHERE NOT EXISTS(SELECT 1 FROM animals WHERE name = ?);
|
||||
`,
|
||||
[animal, animal],
|
||||
(err) => {
|
||||
if (err) {
|
||||
console.error(`Error inserting animal ${animal}: `, err.message);
|
||||
log.error(`Error inserting animal ${animal}: `, err.message);
|
||||
} else {
|
||||
console.log(`Success inserting animal ${animal}`);
|
||||
log.info(`Success inserting animal ${animal}`);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error loading animals:", error);
|
||||
log.error("Error loading animals:", error);
|
||||
animals = ["Dog", "Cat", "Elephant", "Lion", "Giraffe"];
|
||||
}
|
||||
|
||||
|
@ -88,7 +91,7 @@ app.get("/getNextAnimal", async (req, res) => {
|
|||
const randomAnimal = animals[randomIndex];
|
||||
res.json({ animalName: randomAnimal });
|
||||
} catch (error) {
|
||||
console.error("Error fetching random animal:", error);
|
||||
log.error("Error fetching random animal:", error);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
}
|
||||
});
|
||||
|
@ -98,7 +101,7 @@ app.post("/recordButtonClick", (req, res) => {
|
|||
try {
|
||||
//const { buttonName, sessionId } = req.body;
|
||||
const result = req.body;
|
||||
console.error(result);
|
||||
log.error(result);
|
||||
|
||||
db.run(
|
||||
"INSERT INTO button_clicks (session_id, animal_name, button_name, timestamp, time_difference) VALUES (?, ?, ?, ?, ?)",
|
||||
|
@ -111,7 +114,7 @@ app.post("/recordButtonClick", (req, res) => {
|
|||
],
|
||||
(err) => {
|
||||
if (err) {
|
||||
console.error("Error recording button click:", err.message);
|
||||
log.error("Error recording button click:", err.message);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
} else {
|
||||
res.sendStatus(200);
|
||||
|
@ -119,7 +122,7 @@ app.post("/recordButtonClick", (req, res) => {
|
|||
},
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error recording button click:", error);
|
||||
log.error("Error recording button click:", error);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
}
|
||||
});
|
||||
|
@ -200,11 +203,11 @@ app.get("/results", async (req, res) => {
|
|||
await Promise.all([getCount, getTotalAvgTime, getAvgTime]);
|
||||
res.json(results);
|
||||
} catch (error) {
|
||||
console.error("Error fetching results:", error);
|
||||
log.error("Error fetching results:", error);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server is running on port ${port}`);
|
||||
});
|
||||
module.exports = app;
|
||||
|
||||
// vim:set sts=2 sw=2 et:
|
29
app.test.js
Normal file
29
app.test.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
// 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:
|
17
config.js
Normal file
17
config.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
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 })
|
||||
}
|
|
@ -1,31 +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"
|
||||
]
|
||||
}
|
1814
express/package-lock.json
generated
1814
express/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -6,7 +6,7 @@
|
|||
</head>
|
||||
<body>
|
||||
<div id="isCritter">
|
||||
<h1>Is this a Critter?</h1>
|
||||
<h3>Is this a Critter?</h3>
|
||||
<p id="animal-name">what about?:</p>
|
||||
<button id="isCritterButton">Is Critter</button>
|
||||
<button id="isNotCritterButton">Is <b>not</b> Critter</button>
|
||||
|
@ -23,6 +23,9 @@
|
|||
</div>
|
||||
<script src="/asset/frontend.js"></script>
|
||||
<style type="text/css">
|
||||
#animal-name {
|
||||
font-size: 25px;
|
||||
}
|
||||
#isCritter {
|
||||
text-align: center;
|
||||
}
|
5359
package-lock.json
generated
Normal file
5359
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
|
@ -4,8 +4,9 @@
|
|||
"description": "",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node server.js"
|
||||
"test": "./node_modules/.bin/jest",
|
||||
"start": "node server.js",
|
||||
"act": "act -W ./.gitea/workflows/"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
@ -15,8 +16,13 @@
|
|||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.20.2",
|
||||
"bole": "^5.0.7",
|
||||
"express": "^4.18.2",
|
||||
"morgan": "^1.10.0",
|
||||
"sqlite3": "^5.1.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jest": "^29.7.0",
|
||||
"supertest": "^6.3.3"
|
||||
}
|
||||
}
|
15
server.js
Normal file
15
server.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
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:
|
Loading…
Reference in a new issue