Skip to content

Commit

Permalink
chore: Update .gitignore and backend.js, and delete style.css
Browse files Browse the repository at this point in the history
  • Loading branch information
maxiwee69 committed May 24, 2024
1 parent 4537bf9 commit 750c38f
Show file tree
Hide file tree
Showing 18 changed files with 4,870 additions and 134 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
comment.py
comment.py
database.db
52 changes: 26 additions & 26 deletions backend.js
Original file line number Diff line number Diff line change
@@ -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}`);
});
63 changes: 63 additions & 0 deletions backend.py
Original file line number Diff line number Diff line change
@@ -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)
Loading

0 comments on commit 750c38f

Please sign in to comment.