diff --git a/.gitignore b/.gitignore index 13896bb..191d35a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -comment.py +comment.py +database.db diff --git a/backend.js b/backend.js index 4a1b56b..dcd3a15 100644 --- a/backend.js +++ b/backend.js @@ -1,27 +1,27 @@ -const express = require('express'); -const cors = require('cors'); -const writeFileAtomic = require('write-file-atomic'); -const app = express(); -const port = 3001; - -app.use(cors()); -app.use(express.json()); - -app.post('/counter', (req, res) => { - const answer = req.body.answer; - console.log(`Received answer: ${answer}`); // Print the received answer - - writeFileAtomic('counter.txt', `${answer}\n`, { encoding: 'utf8', mode: 0o600 }, function (err) { - if (err) { - console.error(`Error writing to file: ${err}`); // Print the error - res.status(500).json({ message: 'Error writing to file' }); - } else { - console.log('Saved!'); - res.status(200).json({ message: 'counter upped' }); - } - }); -}); - -app.listen(port, () => { - console.log(`Server listening at http://localhost:${port}`); +const express = require('express'); +const cors = require('cors'); +const writeFileAtomic = require('write-file-atomic'); +const app = express(); +const port = 3001; + +app.use(cors()); +app.use(express.json()); + +app.post('/counter', (req, res) => { + const answer = req.body.answer; + console.log(`Received answer: ${answer}`); // Print the received answer + + writeFileAtomic('counter.txt', `${answer}\n`, { encoding: 'utf8', mode: 0o600 }, function (err) { + if (err) { + console.error(`Error writing to file: ${err}`); // Print the error + res.status(500).json({ message: 'Error writing to file' }); + } else { + console.log('Saved!'); + res.status(200).json({ message: 'counter upped' }); + } + }); +}); + +app.listen(port, () => { + console.log(`Server listening at http://localhost:${port}`); }); \ No newline at end of file diff --git a/backend.py b/backend.py new file mode 100644 index 0000000..15872d4 --- /dev/null +++ b/backend.py @@ -0,0 +1,63 @@ +from flask import Flask, request, Response, jsonify +from flask_cors import CORS +import sqlite3 +import datetime +import socket + +app = Flask(__name__) +CORS(app) # This will enable CORS for all routes + +@app.route('/submit', methods=['POST']) +def submit_text(): + data = request.get_json() + print(f"Received data: {data}") # This line prints the received data + text = data['text'] + ip_address = request.remote_addr + + # Get the current timestamp + timestamp = datetime.datetime.now() + + conn = sqlite3.connect('database.db') + c = conn.cursor() + # Include the timestamp when inserting data into the database + c.execute("INSERT INTO submissions (ip_address, text, timestamp) VALUES (?, ?, ?)", (ip_address, text, timestamp)) + conn.commit() + conn.close() + + return 'Success', 200 + +@app.route('/getData', methods=['GET']) +def get_data(): + # Set the server's IP address + server_ip = '192.168.178.44' + # Set the allowed client's IP addresses + allowed_client_ips = ['192.168.178.77', '127.0.0.1'] + # Get the client's IP address + client_ip = request.remote_addr + + # If the client's IP address is not in the list of allowed client's IP addresses, return an error + if client_ip not in allowed_client_ips and client_ip != server_ip: + print(f"Access denied for IP address: {client_ip}") # Print a message to the console + return 'Access denied', 403 + + conn = sqlite3.connect('database.db') + c = conn.cursor() + # Fetch the timestamp from the database + c.execute("SELECT ip_address, text, timestamp FROM submissions") + data = c.fetchall() + conn.close() + + return jsonify(data), 200 + +@app.route('/clearDb', methods=['POST']) +def clear_db(): + conn = sqlite3.connect('database.db') + c = conn.cursor() + c.execute("DELETE FROM submissions") + conn.commit() + conn.close() + + return 'Success', 200 + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/index.html b/index.html index 68ff72c..9706c23 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,6 @@
- @@ -9,27 +8,20 @@