Simple server list added

This commit is contained in:
{{QWERTYKBGUI}} 2023-07-06 12:40:33 +02:00
parent 833a480af3
commit 959a4b1ad6
6 changed files with 80 additions and 11 deletions

View File

@ -1,6 +1,4 @@
MIT License
Copyright (c) <year> <copyright holders>
Copyright (c) 2023 Cube Software(kacperks,Looki2000,mrkubax10)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View File

@ -1,3 +1,3 @@
# server-list
# Polygun online
Database of online servers for the PolyGun game
Database of online servers, users and mods for the PolyGun game

0
app.py Normal file
View File

View File

@ -1,6 +0,0 @@
Server:
IP
PORT ( if not 1337)
Name
Description
Icon(optional)

7
requirements.txt Normal file
View File

@ -0,0 +1,7 @@
uuid
configparser
pyjwt
flask
flask_marshmallow
flask_sqlalchemy
functools

70
server-list.py Normal file
View File

@ -0,0 +1,70 @@
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime, timedelta
import threading
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///servers.db'
db = SQLAlchemy(app)
class Server(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
description = db.Column(db.String(100))
ip = db.Column(db.String(15), nullable=False)
port = db.Column(db.Integer, nullable=False)
last_updated = db.Column(db.DateTime, nullable=False)
def __init__(self, name, description, ip, port):
self.name = name
self.description = description
self.ip = ip
self.port = port
self.last_updated = datetime.utcnow()
def update_last_updated(self):
self.last_updated = datetime.utcnow()
@app.route('/servers', methods=['POST'])
def add_server():
data = request.get_json()
server = Server(data['name'], data['description'], data['ip'], data['port'])
db.session.add(server)
db.session.commit()
return jsonify({'message': 'Server added successfully'}), 201
@app.route('/servers', methods=['GET'])
def get_servers():
servers = Server.query.all()
server_list = []
for server in servers:
server_data = {
'name': server.name,
'description': server.description,
'ip': server.ip,
'port': server.port
}
server_list.append(server_data)
return jsonify(server_list)
def remove_inactive_servers():
inactive_threshold = datetime.utcnow() - timedelta(minutes=5)
inactive_servers = Server.query.filter(Server.last_updated < inactive_threshold).all()
for server in inactive_servers:
db.session.delete(server)
db.session.commit()
def check_inactive_servers():
while True:
remove_inactive_servers()
# Sleep for 5 minutes before checking again
time.sleep(300)
if __name__ == '__main__':
db.create_all()
# Start a separate thread for checking inactive servers
checker_thread = threading.Thread(target=check_inactive_servers)
checker_thread.start()
app.run()