app.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. "name": name,
  36. "availability": avail,
  37. } for name, avail in db.items()
  38. ],
  39. })
  40. body = request.get_json()
  41. print(body)
  42. last_update = datetime.datetime.now()
  43. # TODO actually implement updates
  44. return "", 204
  45. if __name__ == "__main__":
  46. app.run("0.0.0.0", 5000, debug=True)