update
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
services:
|
services:
|
||||||
element-manager:
|
element-manager:
|
||||||
build: .
|
build: .
|
||||||
|
ports:
|
||||||
|
- "8000:8000"
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
@@ -14,12 +16,12 @@ services:
|
|||||||
- traefik.http.middlewares.manager-redirect.redirectscheme.permanent=true
|
- traefik.http.middlewares.manager-redirect.redirectscheme.permanent=true
|
||||||
- traefik.http.middlewares.manager-redirect.redirectscheme.scheme=https
|
- traefik.http.middlewares.manager-redirect.redirectscheme.scheme=https
|
||||||
- traefik.http.routers.manager.middlewares=manager-redirect
|
- traefik.http.routers.manager.middlewares=manager-redirect
|
||||||
- traefik.http.middlewares.manager-auth.basicauth.users=${TRAEFIK_AUTH}
|
# - traefik.http.middlewares.manager-auth.basicauth.users=${TRAEFIK_AUTH}
|
||||||
- traefik.http.routers.manager-secure.entrypoints=https
|
- traefik.http.routers.manager-secure.entrypoints=https
|
||||||
- traefik.http.routers.manager-secure.rule=Host(`manager.dongho.kim`)
|
- traefik.http.routers.manager-secure.rule=Host(`manager.dongho.kim`)
|
||||||
- traefik.http.routers.manager-secure.tls=true
|
- traefik.http.routers.manager-secure.tls=true
|
||||||
- traefik.http.routers.manager-secure.tls.certresolver=cloudflare
|
- traefik.http.routers.manager-secure.tls.certresolver=cloudflare
|
||||||
- traefik.http.routers.manager-secure.middlewares=manager-auth
|
# - traefik.http.routers.manager-secure.middlewares=manager-auth
|
||||||
- traefik.http.services.manager-secure-service.loadbalancer.server.port=8000
|
- traefik.http.services.manager-secure-service.loadbalancer.server.port=8000
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
|
|||||||
28
main.py
28
main.py
@@ -1,17 +1,33 @@
|
|||||||
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
from fastapi import FastAPI, HTTPException, BackgroundTasks, Request
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from bot import MatrixBot
|
from bot import MatrixBot
|
||||||
import asyncio
|
import asyncio
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# Configure logging to output to stdout
|
||||||
|
logging.basicConfig(
|
||||||
|
stream=sys.stdout,
|
||||||
|
level=logging.INFO,
|
||||||
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||||
|
)
|
||||||
|
logger = logging.getLogger("matrix_manager")
|
||||||
|
|
||||||
bot = MatrixBot()
|
bot = MatrixBot()
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
# Startup
|
# Startup
|
||||||
await bot.login()
|
logger.info("Starting up Matrix Manager Service...")
|
||||||
|
try:
|
||||||
|
await bot.login()
|
||||||
|
logger.info("Bot logged in successfully.")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to login bot: {e}")
|
||||||
yield
|
yield
|
||||||
# Shutdown
|
# Shutdown
|
||||||
|
logger.info("Shutting down Matrix Manager Service...")
|
||||||
await bot.close()
|
await bot.close()
|
||||||
|
|
||||||
app = FastAPI(lifespan=lifespan)
|
app = FastAPI(lifespan=lifespan)
|
||||||
@@ -38,6 +54,7 @@ async def send_notification(notification: Notification, background_tasks: Backgr
|
|||||||
"""
|
"""
|
||||||
Send a notification to a Matrix room.
|
Send a notification to a Matrix room.
|
||||||
"""
|
"""
|
||||||
|
logger.info(f"Received notification request from service: {notification.service_name}")
|
||||||
try:
|
try:
|
||||||
# Format the message to include the service name
|
# Format the message to include the service name
|
||||||
# Plain text fallback
|
# Plain text fallback
|
||||||
@@ -46,9 +63,11 @@ async def send_notification(notification: Notification, background_tasks: Backgr
|
|||||||
html_message = f"<b>[{notification.service_name}]</b><br>{notification.content}"
|
html_message = f"<b>[{notification.service_name}]</b><br>{notification.content}"
|
||||||
|
|
||||||
# We can send it in background to not block the API response
|
# We can send it in background to not block the API response
|
||||||
|
logger.info(f"Queueing message for room_id: {notification.room_id or 'Default'}")
|
||||||
background_tasks.add_task(bot.send_message, plain_message, html_message, notification.room_id)
|
background_tasks.add_task(bot.send_message, plain_message, html_message, notification.room_id)
|
||||||
return {"status": "queued"}
|
return {"status": "queued"}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logger.error(f"Error in send_notification: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
@app.post("/jellyfin")
|
@app.post("/jellyfin")
|
||||||
@@ -56,8 +75,10 @@ async def receive_jellyfin_webhook(payload: JellyfinPayload, background_tasks: B
|
|||||||
"""
|
"""
|
||||||
Receive webhook from Jellyfin and forward to Matrix.
|
Receive webhook from Jellyfin and forward to Matrix.
|
||||||
"""
|
"""
|
||||||
|
logger.info(f"Received Jellyfin webhook. Type: {payload.notification_type}, Item: {payload.item_type}")
|
||||||
try:
|
try:
|
||||||
if payload.notification_type != "ItemAdded":
|
if payload.notification_type != "ItemAdded":
|
||||||
|
logger.info("Ignored Jellyfin event (not ItemAdded)")
|
||||||
return {"status": "ignored", "reason": "Not an ItemAdded event"}
|
return {"status": "ignored", "reason": "Not an ItemAdded event"}
|
||||||
|
|
||||||
# content construction
|
# content construction
|
||||||
@@ -80,13 +101,16 @@ async def receive_jellyfin_webhook(payload: JellyfinPayload, background_tasks: B
|
|||||||
plain_message = f"[Jellyfin]\n{content}"
|
plain_message = f"[Jellyfin]\n{content}"
|
||||||
html_message = f"<b>[Jellyfin]</b><br>{content.replace(chr(10), '<br>')}"
|
html_message = f"<b>[Jellyfin]</b><br>{content.replace(chr(10), '<br>')}"
|
||||||
|
|
||||||
|
logger.info(f"Queueing Jellyfin message for room_id: {payload.room_id or 'Default'}")
|
||||||
background_tasks.add_task(bot.send_message, plain_message, html_message, payload.room_id)
|
background_tasks.add_task(bot.send_message, plain_message, html_message, payload.room_id)
|
||||||
return {"status": "queued"}
|
return {"status": "queued"}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logger.error(f"Error in receive_jellyfin_webhook: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
|
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
async def health_check():
|
async def health_check():
|
||||||
|
logger.info("Health check requested")
|
||||||
return {"status": "ok"}
|
return {"status": "ok"}
|
||||||
|
|||||||
Reference in New Issue
Block a user