From cb3ae37c5c49d01f5b027397fd38d3dcb9d8e48d Mon Sep 17 00:00:00 2001 From: TatLead Date: Fri, 15 Mar 2024 23:11:40 +0000 Subject: [PATCH] Fixed flake8 tests and improved code quality --- .github/workflows/python-app.yml | 6 ++--- README.md | 40 +++++------------------------ config.py | 1 - docker-compose.prod.yml | 43 ++++++++++++++++++++++++++++++++ protocols/Factorio.py | 6 +++++ protocols/Front.py | 2 +- protocols/MasterServer.py | 1 - protocols/Scum.py | 2 +- protocols/__init__.py | 12 ++++----- version.py | 2 +- 10 files changed, 67 insertions(+), 48 deletions(-) create mode 100644 docker-compose.prod.yml diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 808c520..fd02d62 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -1,7 +1,7 @@ # This workflow will install Python dependencies, run tests and lint with a single version of Python # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python -name: Python application +name: Python Application on: push: @@ -18,9 +18,9 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Python 3.12 - uses: actions/setup-python@v3 + uses: actions/setup-python@v5 with: python-version: "3.12" - name: Install dependencies diff --git a/README.md b/README.md index 7e2b3b5..9b91a14 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # OpenGSQ Master Server Search API +[![Python application](https://github.com/opengsq/opengsq-master-server/actions/workflows/python-app.yml/badge.svg)](https://github.com/opengsq/opengsq-master-server/actions/workflows/python-app.yml) [![Docker Image CI](https://github.com/opengsq/opengsq-master-server/actions/workflows/docker-image.yml/badge.svg)](https://github.com/opengsq/opengsq-master-server/actions/workflows/docker-image.yml) +[![GitHub release](https://img.shields.io/github/release/opengsq/opengsq-master-server)](https://github.com/opengsq/opengsq-master-server/releases/) [![GitHub license](https://img.shields.io/github/license/opengsq/opengsq-master-server)](https://github.com/opengsq/opengsq-master-server/blob/main/LICENSE) This is an application that provides an API for searching game servers. The application supports the following games: BeamMP, Factorio, Palworld, and Scum. @@ -91,43 +93,13 @@ You can start the scheduled task or run the Flask application in debug mode: ## Self-Hosting -You can use Docker Compose to self-host the application. Here's how: +[![Docker Pulls](https://img.shields.io/docker/pulls/opengsq/opengsq-master-server.svg)](https://hub.docker.com/r/opengsq/opengsq-master-server) -1. Ensure that you have the following file structure: - - `docker-compose.yml` - - `.env` - -2. Create a `docker-compose.yml` file with the following content: - - ```yml - version: '3.8' - services: - flask: - image: opengsq/opengsq-master-server:latest - command: gunicorn -w 4 -b :8000 app:app - container_name: opengsq-master-server-flask - environment: - - FLASK_ENV=production - env_file: - - .env - ports: - - ${PORT}:8000 - restart: always - volumes: - - ./data:/app/data - - schedule: - image: opengsq/opengsq-master-server:latest - command: python main.py - container_name: opengsq-master-server-schedule - env_file: - - .env - restart: always - ``` +You can use Docker Compose to self-host the application. Here's how: -3. Create a `.env` file as stated in the [Configuration](#configuration) section. +1. Create a `docker-compose.yml` file [docker-compose.yml example](/docker-compose.prod.yml) -4. Run the following command to start the application: +2. Run the following command to start the application: ```bash docker-compose up -d diff --git a/config.py b/config.py index 0d9fe79..7593d2d 100644 --- a/config.py +++ b/config.py @@ -41,7 +41,6 @@ def overwrite(section: str, options: list[str]): config[section][option] = os.getenv( option, config.get(section, option)) - overwrite('authentication', ['USERNAME', 'PASSWORD', 'SECURITY_TOKEN']) # Update the app version diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..d75e4ad --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,43 @@ +version: '3.8' +services: + mongodb: + container_name: opengsq-master-server-mongodb + environment: + - MONGO_INITDB_ROOT_USERNAME=root + - MONGO_INITDB_ROOT_PASSWORD=example + expose: + - 27017 + image: mongo:latest + volumes: + - ./mongodb_data:/data/db + + flask: + command: gunicorn -w 4 -b :8000 app:app + container_name: opengsq-master-server-flask + depends_on: + - mongodb + environment: + - FLASK_ENV=production + - DATABASE_URL=mongodb://mongodb:27017/ + - PORT=8000 + - USERNAME=admin + - PASSWORD=admin + - SECURITY_TOKEN=cc83733cb0af8b884ff6577086b87909 + image: opengsq/opengsq-master-server:latest + ports: + - ${PORT}:8000 + restart: always + volumes: + - ./data:/app/data + + schedule: + command: python main.py + container_name: opengsq-master-server-schedule + depends_on: + - mongodb + environment: + - DATABASE_URL=mongodb://mongodb:27017/ + - FACTORIO_USERNAME= + - FACTORIO_TOKEN= + image: opengsq/opengsq-master-server:latest + restart: always diff --git a/protocols/Factorio.py b/protocols/Factorio.py index d2e85e7..5bf4026 100644 --- a/protocols/Factorio.py +++ b/protocols/Factorio.py @@ -39,6 +39,12 @@ def find(self, *, host: str, port: int): def _fetch(self) -> list: username = os.getenv("FACTORIO_USERNAME") token = os.getenv("FACTORIO_TOKEN") + + # Check if username or token is None or empty string + if not username or not token: + print("Warning: FACTORIO_USERNAME or FACTORIO_TOKEN is not set or empty.") + return [] + url = f"https://multiplayer.factorio.com/get-games?username={username}&token={token}" data = self._fetch_url(url) diff --git a/protocols/Front.py b/protocols/Front.py index b7e767c..271238d 100644 --- a/protocols/Front.py +++ b/protocols/Front.py @@ -35,7 +35,7 @@ def find(self, *, host: str, port: int): return result def _fetch(self) -> list: - url = f'https://privatelist.playthefront.com/private_list' + url = 'https://privatelist.playthefront.com/private_list' data = self._fetch_url(url) # Convert string to JSON diff --git a/protocols/MasterServer.py b/protocols/MasterServer.py index 2b05f13..db42eb1 100644 --- a/protocols/MasterServer.py +++ b/protocols/MasterServer.py @@ -1,7 +1,6 @@ from abc import ABC, abstractmethod from concurrent.futures import ThreadPoolExecutor from datetime import datetime, timedelta, timezone -import json import os import time from pymongo import MongoClient diff --git a/protocols/Scum.py b/protocols/Scum.py index 1497ccc..284715d 100644 --- a/protocols/Scum.py +++ b/protocols/Scum.py @@ -72,7 +72,7 @@ def _fetch(self): return servers - raise Exception(f"All master server addresses are unreachable") + raise Exception("All master server addresses are unreachable") def _parse_server(self, data: bytes): result = self.__SERVER_INFO_STRUCT.unpack(data) diff --git a/protocols/__init__.py b/protocols/__init__.py index 0f40334..ce205a1 100644 --- a/protocols/__init__.py +++ b/protocols/__init__.py @@ -1,6 +1,6 @@ -from .BeamMP import BeamMP -from .Factorio import Factorio -from .Front import Front -from .MasterServer import MasterServer -from .Palworld import Palworld -from .Scum import Scum +from .BeamMP import BeamMP # noqa: F401 +from .Factorio import Factorio # noqa: F401 +from .Front import Front # noqa: F401 +from .MasterServer import MasterServer # noqa: F401 +from .Palworld import Palworld # noqa: F401 +from .Scum import Scum # noqa: F401 diff --git a/version.py b/version.py index 6df8ed0..58d478a 100644 --- a/version.py +++ b/version.py @@ -1 +1 @@ -__version__ = '1.1.0' \ No newline at end of file +__version__ = '1.2.0'