Skip to content

Commit

Permalink
Fixed flake8 tests and improved code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
BattlefieldDuck committed Mar 15, 2024
1 parent d66928b commit cb3ae37
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 48 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand Down
40 changes: 6 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions protocols/Factorio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion protocols/Front.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion protocols/MasterServer.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion protocols/Scum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions protocols/__init__.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.1.0'
__version__ = '1.2.0'

0 comments on commit cb3ae37

Please sign in to comment.