Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from flask import Flask, render_template, request, jsonify
import os
import sqlite3

app = Flask(__name__)
app.config['DATABASE_PATH'] = 'database.db'

@app.route('/')
def index():
return render_template('index.html')

@app.route('/api/messages', methods=['GET'])
def get_messages():
try:
conn = sqlite3.connect(app.config['DATABASE_PATH'])
cursor = conn.cursor()
cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 100")
messages = cursor.fetchall()
conn.close()
return jsonify(messages)
except Exception as e:
return jsonify({'error': str(e)})

@app.route('/api/send', methods=['POST'])
def send_message():
try:
data = request.get_json()
conn = sqlite3.connect(app.config['DATABASE_PATH'])
cursor = conn.cursor()
cursor.execute("INSERT INTO messages (user, message) VALUES (?, ?)",
(data['user'], data['message']))
conn.commit()
conn.close()
return jsonify({'status': 'success'})
except Exception as e:
return jsonify({'error': str(e)})

def init_database():
"""Initialize the database with required tables"""
conn = sqlite3.connect(app.config['DATABASE_PATH'])
cursor = conn.cursor()

# Create messages table if it doesn't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user TEXT NOT NULL,
message TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')

# Create users table if it doesn't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
uid INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
cookie TEXT,
label TEXT
)
''')

conn.commit()
conn.close()
print("Database initialized successfully")

if __name__ == '__main__':
init_database()
app.run(debug=True, host='0.0.0.0', port=5000)
Binary file added database.db
Binary file not shown.
32 changes: 32 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3
"""
Main entry point for the NeoChatroom Flask application.
This script initializes the database and starts the Flask server.
"""

import os
import sys
from app import app, init_database

def main():
"""Main function to run the application"""
print("Starting NeoChatroom application...")

# Initialize database
try:
init_database()
print("Database initialization completed")
except Exception as e:
print(f"Error initializing database: {e}")
sys.exit(1)

# Start the Flask application
try:
print("Starting Flask server on http://0.0.0.0:5000")
app.run(debug=True, host='0.0.0.0', port=5000)
except Exception as e:
print(f"Error starting Flask server: {e}")
sys.exit(1)

if __name__ == '__main__':
main()