app.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python3
  2. import json
  3. import atexit
  4. import datetime
  5. from flask import Flask, jsonify, request
  6. from flask_cors import CORS
  7. DB_FILE = "vacation.db"
  8. last_update = datetime.datetime.now()
  9. app = Flask(__name__)
  10. CORS(app)
  11. app.config["APPLICATION_ROOT"] = "/vacation/api"
  12. app.url_map.strict_slashes = False
  13. try:
  14. with open(DB_FILE) as infile:
  15. db = json.load(infile)
  16. except FileNotFoundError:
  17. db = {}
  18. @atexit.register
  19. def save_db(prefix=""):
  20. with open(prefix + DB_FILE, "w") as outfile:
  21. json.dump(db, outfile)
  22. def backup_db():
  23. save_db(f"backup-{datetime.datetime.now().isoformat()}-")
  24. @app.route("/")
  25. def health():
  26. return jsonify({"status": "healthy"})
  27. @app.route("/availability", methods=["GET", "POST"])
  28. def status():
  29. global last_update
  30. if request.method == "GET":
  31. return jsonify({
  32. "lastUpdated": last_update.isoformat(),
  33. "availability": [
  34. {
  35. "month": month,
  36. "day": day,
  37. "availability": avail,
  38. } for (month, day), avail in db.items()
  39. ],
  40. })
  41. body = request.get_json()
  42. print(body)
  43. last_update = datetime.datetime.now()
  44. # TODO actually implement updates
  45. return "", 204
  46. if __name__ == "__main__":
  47. app.run("0.0.0.0", 5000, debug=True)