Add main.py to serve and back up

This commit is contained in:
iamdoubz committed 2026-11-05 16:08:32 -06:00
1 parent 79cd4fc602
commit 455e9cf7f8
1 file changed
+48
+48
View File
@@ -0,0 +1,48 @@
"""ParentCalendar.
Usage:
python main.py start the server (settings: see .env.example)
python main.py backup <destination> copy the database (SQLite online backup) and the photos
"""
import shutil
import sqlite3
import sys
from datetime import datetime
from pathlib import Path
from parentcalendar import config, db
def backup(destination):
dest = Path(destination)
dest.mkdir(parents=True, exist_ok=True)
target = dest / f"parentcalendar-{datetime.now():%Y%m%d-%H%M%S}.db"
src, out = db.connect(), sqlite3.connect(target)
try:
src.backup(out) # consistent copy even while the server is writing
finally:
out.close()
src.close()
if config.IMAGES_DIR.exists():
shutil.copytree(config.IMAGES_DIR, dest / "images", dirs_exist_ok=True)
print(f"Backed up to {target}")
def serve():
if not config.PUBLIC_URL:
sys.exit("Set PUBLIC_URL (for example https://calendar.example.com) in the environment or in .env.")
if not (config.STATIC_DIR / "index.html").exists():
sys.exit("The front end isn't built yet. Run: cd frontend && npm ci && npm run build")
import uvicorn
uvicorn.run("parentcalendar.app:app", host=config.HOST, port=config.PORT, proxy_headers=True)
if __name__ == "__main__":
args = sys.argv[1:]
if args[:1] == ["backup"] and len(args) == 2:
backup(args[1])
elif not args:
serve()
else:
sys.exit(__doc__)