Nessuna descrizione

files_api.py 924B

1234567891011121314151617181920212223242526272829303132
  1. import os
  2. from flask import Flask, send_from_directory, jsonify
  3. from flask_cors import CORS, cross_origin
  4. app = Flask(__name__, static_url_path='')
  5. app.config["SECRET_KEY"] = "f82abccd93a28b6cdda4525c9afa7a30"
  6. app.config["CORS_HEADERS"] = "Content-Type"
  7. cors = CORS(app, resources={r"/articles": {"origins": "http://localhost:5500"}})
  8. @app.route("/articles/<path:article>")
  9. @cross_origin(origin="localhost", headers=["Content-Type", "Authorization"])
  10. def send_text_file(article):
  11. return send_from_directory(directory="articles", path=article)
  12. @app.route("/articles")
  13. @cross_origin(origin="localhost", headers=["Content-Type", "Authorization"])
  14. def list_files():
  15. files = list()
  16. for filename in os.listdir("articles"):
  17. path = os.path.join("articles", filename)
  18. if os.path.isfile(path):
  19. files.append(filename)
  20. return jsonify(files)
  21. if __name__ == "__main__":
  22. app.run()