diff --git a/main.py b/main.py new file mode 100644 index 0000000..4247482 --- /dev/null +++ b/main.py @@ -0,0 +1,48 @@ +"""ParentCalendar. + +Usage: + python main.py start the server (settings: see .env.example) + python main.py backup 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__)