Skip to content

Commit

Permalink
Update Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
BattlefieldDuck committed Mar 13, 2024
1 parent a8d27aa commit 172a918
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
FROM python:3.12

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /app

COPY requirements.txt ./
Expand Down
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,104 @@ This project is licensed under the MIT License.
## Stargazers over time

[![Stargazers over time](https://starchart.cc/opengsq/opengsq-master-server.svg?variant=adaptive)](https://starchart.cc/opengsq/opengsq-master-server)

---

# OpenGSQ Master Server

This project provides a self-hosted master server solution for your game servers.

## Development Setup

Follow these steps to set up your development environment:

1. Create a virtual environment:
```bash
python -m venv venv
```
2. Activate the virtual environment:
- On Windows, run: `venv\Scripts\activate`
- On Unix or MacOS, run: `source venv/bin/activate`
3. Install the required packages:
```bash
pip install -r requirements.txt
```

## Configuration

Copy the `.env.example` file to a new file named `.env` and update the variables as needed:

```bash
cp .env.example .env
```

Here's what each variable in the `.env` file represents:

- `DATABASE_URL`: The URL of your mongodb database.
- `PORT`: The port number on which the Flask application will run.
- `FACTORIO_USERNAME`: Your Factorio username.
- `FACTORIO_TOKEN`: Your Factorio token.
- `USERNAME`: The username for the Flask-MonitoringDashboard.
- `PASSWORD`: The password for the Flask-MonitoringDashboard.
- `SECURITY_TOKEN`: The security token for the Flask-MonitoringDashboard.

## Running the Application

You can start the scheduled task or run the Flask application in debug mode:

- Start the scheduled task:
```bash
python main.py
```
- Run Flask in debug mode:
```bash
python app.py
```
- Run Protocol:
```bash
python -m protocol.BeamMP
```

## Self-Hosting

You can use Docker Compose to self-host the application. Here's how:

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 -u main.py
container_name: opengsq-master-server-schedule
env_file:
- .env
restart: always
```

3. Create a `.env` file as stated in the Configuration section.

4. Run the following command to start the application:

```bash
docker-compose up -d
```
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:

schedule:
build: .
command: python -u main.py
command: python main.py
container_name: opengsq-master-server-schedule
env_file:
- .env
Expand Down
14 changes: 9 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time
import schedule

from protocols import MasterServer, BeamMP, Factorio, Palworld, Scum
from protocols import MasterServer

threads: dict[str, Thread] = {}

Expand All @@ -13,10 +13,14 @@ def run_threaded(master_server: MasterServer):
threads[master_server.key].start()


schedule.every(5).minutes.do(run_threaded, BeamMP()).run()
schedule.every(5).minutes.do(run_threaded, Factorio()).run()
schedule.every(5).minutes.do(run_threaded, Palworld()).run()
schedule.every(5).minutes.do(run_threaded, Scum()).run()
for Protocol in MasterServer.__subclasses__():
protocol = Protocol()

# Creates an index on protocol collection.
protocol.create_index()

# Create a schedule task
schedule.every(5).minutes.do(run_threaded, protocol).run()

for job in schedule.get_jobs():
print(f"Job: {job}, Next run: {job.next_run}, Period: {job.period}")
Expand Down
2 changes: 2 additions & 0 deletions protocols/BeamMP.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
class BeamMP(MasterServer):
def __init__(self) -> None:
super().__init__('BeamMP')

def create_index(self):
self.collection.create_index({'ip': 1, 'port': 1})

def job(self):
Expand Down
2 changes: 2 additions & 0 deletions protocols/Factorio.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
class Factorio(MasterServer):
def __init__(self) -> None:
super().__init__('Factorio')

def create_index(self):
self.collection.create_index('server_id')
self.collection.create_index('host_address')

Expand Down
9 changes: 7 additions & 2 deletions protocols/MasterServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

load_dotenv()

uri = os.getenv('DATABASE_URL')
client = MongoClient(uri)


class MasterServer(ABC):
def __init__(self, key: str) -> None:
Expand All @@ -20,15 +23,17 @@ def __init__(self, key: str) -> None:

@staticmethod
def get_db():
uri = os.getenv('DATABASE_URL')
client = MongoClient(uri)
db = client['MasterServer']
return db

@property
def key(self):
return self._key

@abstractmethod
def create_index(self):
pass

@abstractmethod
def job(self):
pass
Expand Down
2 changes: 2 additions & 0 deletions protocols/Palworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
class Palworld(MasterServer):
def __init__(self) -> None:
super().__init__('Palworld')

def create_index(self):
self.collection.create_index('server_id')
self.collection.create_index({'address': 1, 'port': 1})

Expand Down
2 changes: 2 additions & 0 deletions protocols/Scum.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Scum(MasterServer):

def __init__(self) -> None:
super().__init__('Scum')

def create_index(self):
self.collection.create_index({'ip': 1, 'port': 1})

def job(self):
Expand Down

0 comments on commit 172a918

Please sign in to comment.