- api/: FastAPI app with MySQL (accounts, forwarding_rules, messages)
- GET/POST/PUT/DELETE /accounts + /accounts/{id}/test
- GET /messages + /messages/{id}/retry
- GET /stats
- POST /connection/test
- Auto-runs migrations/init.sql on startup
- worker/: Python IMAP poller + BizDocs upload (schedule every 5 min)
- web/: multi-stage Dockerfile — builds admin-ui via Node, serves via nginx
- nginx proxies /api/ → api:8000, SPA fallback for React Router
- docker-compose.yml: updated web build context + mysql init.sql mount
Co-Authored-By: Paperclip <[email protected]>
97 lines
3.4 KiB
Python
97 lines
3.4 KiB
Python
import os
|
|
from sqlalchemy import create_engine, text
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
_url = (
|
|
"mysql+pymysql://{user}:{pw}@{host}:{port}/{db}?charset=utf8mb4".format(
|
|
user=os.environ["MYSQL_USER"],
|
|
pw=os.environ["MYSQL_PASSWORD"],
|
|
host=os.environ.get("MYSQL_HOST", "mysql"),
|
|
port=os.environ.get("MYSQL_PORT", "3306"),
|
|
db=os.environ.get("MYSQL_DATABASE", "forwarder"),
|
|
)
|
|
)
|
|
engine = create_engine(_url, pool_pre_ping=True, pool_recycle=3600)
|
|
Session = sessionmaker(bind=engine)
|
|
|
|
|
|
def get_enabled_accounts():
|
|
with Session() as s:
|
|
rows = s.execute(
|
|
text(
|
|
"SELECT id, imap_host, imap_port, imap_ssl, email, imap_user, imap_password, "
|
|
"folder, bizdocs_api_key, bizdocs_entity_id, last_uid "
|
|
"FROM accounts WHERE active = 1"
|
|
)
|
|
).mappings().all()
|
|
return [dict(r) for r in rows]
|
|
|
|
|
|
def get_rules_for_account(account_id: int):
|
|
with Session() as s:
|
|
rows = s.execute(
|
|
text(
|
|
"SELECT id, filter_from, filter_subject, filter_has_attachment, allowed_extensions "
|
|
"FROM forwarding_rules WHERE account_id = :aid AND enabled = 1"
|
|
),
|
|
{"aid": account_id},
|
|
).mappings().all()
|
|
return [dict(r) for r in rows]
|
|
|
|
|
|
def create_message(account_id, rule_id, message_uid, subject, sender, received_at, filename, file_size):
|
|
with Session() as s:
|
|
r = s.execute(
|
|
text(
|
|
"INSERT INTO messages "
|
|
"(account_id, rule_id, message_uid, subject, sender, received_at, "
|
|
"filename, file_size_bytes, status, attempted_at) "
|
|
"VALUES (:aid, :rid, :uid, :subj, :sndr, :dt, :fn, :sz, 'pending', NOW())"
|
|
),
|
|
dict(
|
|
aid=account_id, rid=rule_id, uid=str(message_uid),
|
|
subj=subject, sndr=sender, dt=received_at, fn=filename, sz=file_size,
|
|
),
|
|
)
|
|
s.commit()
|
|
return r.lastrowid
|
|
|
|
|
|
def update_message(msg_id: int, status: str, bizdocs_doc_id=None, error=None):
|
|
with Session() as s:
|
|
forwarded_at = "NOW()" if status == "forwarded" else "NULL"
|
|
s.execute(
|
|
text(
|
|
f"UPDATE messages SET status = :st, bizdocs_doc_id = :doc, "
|
|
f"error = :err, forwarded_at = {forwarded_at} WHERE id = :mid"
|
|
),
|
|
dict(st=status, doc=bizdocs_doc_id, err=error, mid=msg_id),
|
|
)
|
|
s.commit()
|
|
|
|
|
|
def update_account_uid(account_id: int, last_uid: int):
|
|
with Session() as s:
|
|
s.execute(
|
|
text(
|
|
"UPDATE accounts SET last_uid = :uid, last_checked_at = NOW() WHERE id = :aid"
|
|
),
|
|
{"uid": last_uid, "aid": account_id},
|
|
)
|
|
s.commit()
|
|
|
|
|
|
def get_pending_retries():
|
|
"""Return pending messages that were previously failed (retry flow)."""
|
|
with Session() as s:
|
|
rows = s.execute(
|
|
text(
|
|
"SELECT m.id, m.account_id, m.message_uid, m.filename, "
|
|
"a.imap_host, a.imap_port, a.imap_ssl, a.imap_user, a.imap_password, "
|
|
"a.folder, a.bizdocs_api_key, a.bizdocs_entity_id "
|
|
"FROM messages m JOIN accounts a ON a.id = m.account_id "
|
|
"WHERE m.status = 'pending' AND a.active = 1"
|
|
)
|
|
).mappings().all()
|
|
return [dict(r) for r in rows]
|