Skip to content

Commit

Permalink
Merge pull request #42 from Strexas/MDE/PKFE-5
Browse files Browse the repository at this point in the history
MDE/PKFE-5
  • Loading branch information
mantvydasdeltuva authored Aug 22, 2024
2 parents a3ffd99 + 82098b3 commit 4632ac8
Show file tree
Hide file tree
Showing 35 changed files with 500,892 additions and 165 deletions.
3 changes: 2 additions & 1 deletion app/back-end/.env.development
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
FLASK_RUN_HOST=0.0.0.0
FLASK_RUN_PORT=8080
ORIGINS=http://localhost:5173,http://localhost:4173
ORIGINS=http://localhost:5173,http://localhost:4173
REDIS_URL=redis://localhost:6379/0
3 changes: 2 additions & 1 deletion app/back-end/.env.production
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
FLASK_RUN_HOST=1.1.1.1
FLASK_RUN_PORT=8081
ORIGINS=http://website.com
ORIGINS=http://website.com
REDIS_URL=redis://localhost:6379/0
34 changes: 31 additions & 3 deletions app/back-end/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Development Server Setup Guide

This guide provides instructions on setting up and running a Flask-based development server using Gunicorn, Gevent, Socket.IO, and CORS on a Windows operating system with WSL (Windows Subsystem for Linux).
This guide provides instructions on setting up and running a Flask-based development server using Gunicorn, Gevent, Socket.IO, CORS and Redis on a Windows operating system with WSL (Windows Subsystem for Linux).

## Prerequisites

Expand Down Expand Up @@ -45,14 +45,14 @@ This guide provides instructions on setting up and running a Flask-based develop

2. **Update Package List:**
```bash
sudo apt update
sudo apt-get update
```

Wait for the system to update.

3. **Install Required Packages:**
```bash
sudo apt install python3 python3-pip python3-venv
sudo apt-get install python3 python3-pip python3-venv redis
```

With any prompts type `y` and press `enter`.
Expand Down Expand Up @@ -127,6 +127,34 @@ This guide provides instructions on setting up and running a Flask-based develop
- Open `/mnt/c/Users/YourUsername/Path/To/Project/app/back-end/.venv/bin/python3.10`

## Step 6: Run the Development Server

1. **Test if Redis is running:**
```bash
redis-cli
```

Test the connect with the `ping` command.
```powershell
127.0.0.1:6379> ping
PONG
```

If you get this response `Could not connect to Redis at 127.0.0.1:6379: Connection refused`, exit out of the connection and start the Redis server.
```bash
sudo systemctl start redis
```

Now test it again.

#

```bash
sudo systemctl stop redis
```

This will stop the Redis server.

2. **Run the application**
```powershell
gunicorn -c gunicorn_config.py run:app
```
Expand Down
4 changes: 1 addition & 3 deletions app/back-end/gunicorn_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@

import multiprocessing

from src.setup.env import Env

# Bind to specified host and port
bind = Env.get_flask_run_host() + ":" + str(Env.get_flask_run_port())
bind = "0.0.0.0:8080"

# Use Gevent worker class for handling asynchronous requests with WebSocket support
worker_class = "geventwebsocket.gunicorn.workers.GeventWebSocketWorker"
Expand Down
4 changes: 3 additions & 1 deletion app/back-end/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
async-timeout==4.0.3
bidict==0.23.1
blinker==1.8.2
Brotli==1.1.0
Expand All @@ -21,9 +22,10 @@ pycparser==2.22
python-dotenv==1.0.1
python-engineio==4.9.1
python-socketio==5.11.3
redis==5.0.8
simple-websocket==1.0.0
Werkzeug==3.0.3
wsproto==1.2.0
zope.event==5.0
zope.interface==7.0.1
zstandard==0.23.0
zstandard==0.23.0
7 changes: 3 additions & 4 deletions app/back-end/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
- src.setup.env.Env: The class responsible for managing environment variables, including retrieving the host and port settings.
"""

from src import create_app, socketio
from src.setup.env import Env
from src import create_app, socketio, env

# Initialize the Flask app
app = create_app()

if __name__ == "__main__":
# Retrieve Flask server host and port from environment variables
host = Env.get_flask_run_host()
port = Env.get_flask_run_port()
host = env.get_flask_run_host()
port = env.get_flask_run_port()

# Run the app with Socket.IO support
socketio.run(app, debug=True, host=host, port=port)
65 changes: 33 additions & 32 deletions app/back-end/src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
"""
This module sets up and configures a Flask application with various extensions and routing.
The module handles:
- Applying gevent monkey patches to make the standard library cooperative.
- Loading and managing environment variables.
- Configuring application settings such as compression and CORS.
- Initializing Flask extensions, including compression, Socket.IO, CORS, and logging.
The module is responsible for:
- Applying gevent monkey patches to enable cooperative multitasking with the standard library.
- Loading environment variables to configure app settings.
- Configuring application settings such as compression and CORS policies.
- Initializing Flask extensions including compression, Socket.IO, and CORS.
- Registering application routes and event handlers.
Dependencies:
- Flask
- gevent.monkey
- src.setup.env.Env: Handles environment variable loading and retrieval.
- src.setup.extensions: Contains the initialized Flask extensions.
- src.setup.router: Manages application routing.
- src.setup.eventer: Registers application events.
- src.setup.constants: Contains application constants like BASE_ROUTE.
- Flask: The core web framework.
- gevent.monkey: Provides monkey patches to enable cooperative multitasking.
- src.setup.extensions: Contains initialization for Flask extensions like compression, Socket.IO, and CORS.
- src.setup.router: Defines and manages application routing through blueprints.
- src.setup.eventer: Sets up event handlers for application events.
- src.constants: Provides constants used in configuration, such as BASE_ROUTE.
Returns:
Flask: A fully configured Flask application instance.
Flask: A fully configured Flask application instance with all extensions and routes initialized.
"""

import gevent.monkey
from flask import Flask

from src.setup.env import Env
from src.setup.extensions import compress, socketio, cors
from src.setup.extensions import compress, socketio, cors, env
from src.setup.router import router
from src.setup.eventer import eventer
from src.setup.constants import BASE_ROUTE
from src.constants import BASE_ROUTE


def create_app():
"""
Create and configure the Flask application.
This function sets up the Flask app by applying necessary monkey patches for gevent,
loading environment variables, configuring app settings, and initializing various
Flask extensions such as compression, Socket.IO, CORS, and logging. It also registers
the application's main routes and event handlers.
This function initializes a Flask application with necessary configurations and extensions.
It applies gevent monkey patches for cooperative multitasking, sets up application settings
for compression and CORS, and integrates various Flask extensions such as compression,
Socket.IO, and CORS. Additionally, it registers the main application routes and sets up
event handlers.
Configuration Details:
- Compression: Enabled for `text/csv` MIME types using gzip with a compression level of 6.
- Socket.IO: Configured with gevent as the async mode, CORS allowed origins from environment,
and a Redis message queue.
- CORS: Applied with origins specified from the environment.
Returns:
Flask: A fully configured Flask application instance.
Flask: A fully configured Flask application instance with extensions initialized,
routes registered, and event handlers set up.
"""
# Apply monkey patches for gevent
gevent.monkey.patch_all()

# Load environment variables
Env.load_env()

# Set environment variables
origins = Env.get_origins()

# Create Flask app instance
app = Flask(__name__)

Expand All @@ -66,15 +66,16 @@ def create_app():
socketio.init_app(
app,
async_mode="gevent",
cors_allowed_origins=origins,
cors_allowed_origins=env.get_origins(),
message_queue=env.get_redis_url(),
max_http_buffer_size=50 * 1024 * 1024,
)
cors.init_app(app, resources={r"*": {"origins": origins}})

# Register main application routes
app.register_blueprint(router(BASE_ROUTE))
cors.init_app(app, resources={r"*": {"origins": env.get_origins()}})

# Set up event handlers
eventer()

# Register main application routes
app.register_blueprint(router(BASE_ROUTE))

return app
12 changes: 12 additions & 0 deletions app/back-end/src/setup/env.py → app/back-end/src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,15 @@ def get_origins(cls):
"""
origins = cls.get("ORIGINS", "*")
return origins.split(",")

@classmethod
def get_redis_url(cls):
"""
Get the Redis URL from environment variables.
This is used for connecting to the Redis server.
Returns:
str: The Redis URL, defaulting to "redis://localhost:6379/0".
"""
return cls.get("REDIS_URL", "redis://localhost:6379/0")
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
# Routes
BASE_ROUTE = "/api/v1"
WORKSPACE_ROUTE = "/workspace"

# Events
CONSOLE_FEEDBACK_EVENT = "console_feedback"
2 changes: 1 addition & 1 deletion app/back-end/src/events/workspace_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import os

from src.setup.extensions import socketio
from src.setup.constants import WORKSPACE_DIR
from src.constants import WORKSPACE_DIR


@socketio.on("workspace_file_update")
Expand Down
Loading

0 comments on commit 4632ac8

Please sign in to comment.