Jaguar_Project / __init__.py
xenecode's picture
Translate frontend and backend text to English
6c68d01
Raw History Blame Contribute Delete
3.25 kB
# __init__.py
from flask import Flask
from flask import send_from_directory
import os
from flask_cors import CORS
from flask_socketio import SocketIO
from config import Config
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
# Blueprint'leri import et
from routes.scan_routes import scan_bp
from routes.health_routes import health_bp
from routes.robot_routes import robot_bp
# Robot servisini import et
from services.robot_service import start_robot_service, set_status_callback
import atexit
from apscheduler.schedulers.background import BackgroundScheduler
import eventlet
eventlet.monkey_patch()
MAX_LOG_LINES = 1000 # Maximum number of lines to keep
def trim_log_file():
try:
with open(Config.LOG_FILE, "r") as f:
lines = f.readlines()
if len(lines) > MAX_LOG_LINES:
with open(Config.LOG_FILE, "w") as f:
f.writelines(lines[-MAX_LOG_LINES:])
print(f"[LOG] Log file trimmed ({len(lines)} lines → {MAX_LOG_LINES})")
except Exception as e:
print(f"[ERROR] Log trimming error: {e}")
# Start scheduler
scheduler = BackgroundScheduler()
scheduler.add_job(func=trim_log_file, trigger="interval", minutes=5)
scheduler.start()
# Stop the scheduler when the app shuts down
atexit.register(lambda: scheduler.shutdown())
# SocketIO objesi global
socketio = SocketIO(cors_allowed_origins="*", async_mode="eventlet")
def create_app():
# Initialize Sentry SDK before creating Flask app
# sentry_sdk.init(
# dsn=os.getenv("SENTRY_DSN"),
# # Add data like request headers and IP for users
# send_default_pii=True,
# # Enable Flask integration
# integrations=[FlaskIntegration()],
# # Set traces_sample_rate to 1.0 to capture 100% of transactions for performance monitoring
# # We recommend adjusting this value in production
# traces_sample_rate=2.0,
# )
app = Flask(
__name__, static_folder="static/react/static", template_folder="static/react"
)
CORS(app)
# Register blueprints
app.register_blueprint(scan_bp)
app.register_blueprint(robot_bp)
app.register_blueprint(health_bp)
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def serve(path):
build_root = os.path.join(app.root_path, app.template_folder)
if path != "" and os.path.exists(os.path.join(app.static_folder, path)):
return send_from_directory(app.static_folder, path)
elif path != "" and os.path.isfile(os.path.join(build_root, path)):
# Files from the public/ folder (e.g. the robots/fr5 3D model)
return send_from_directory(build_root, path)
else:
return send_from_directory(app.template_folder, "index.html")
# SocketIO'yu init et
socketio.init_app(app)
# Set the callback function for the robot service
def emit_robot_data(event_name, data):
"""Broadcast robot data via SocketIO"""
socketio.emit(event_name, data)
# Register the callback with the robot service
set_status_callback(emit_robot_data)
# Start the robot service (in separate threads)
start_robot_service()
return app, socketio