app.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # This file is part of DarkFi (https://dark.fi)
  2. #
  3. # Copyright (C) 2020-2025 Dyne.org foundation
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. """
  18. Module: app.py
  19. This module initializes the DarkFi explorer Flask application by registering various blueprints for handling routes
  20. related to blocks, contracts, transactions, search, and the explore section, including the home page. It also defines
  21. error handlers, ensuring appropriate responses for these common HTTP errors.
  22. """
  23. from flask import Flask, render_template
  24. from blueprints.explore import explore_bp
  25. from blueprints.block import block_bp
  26. from blueprints.contract import contract_bp
  27. from blueprints.transaction import transaction_bp
  28. def create_app():
  29. """
  30. Creates and configures the DarkFi explorer Flask application.
  31. This function creates and initializes the explorer the Flask app,
  32. registering applicable blueprints for handling explorer-related routes,
  33. and defining error handling for common HTTP errors. It returns a fully
  34. configured Flask application instance.
  35. """
  36. app = Flask(__name__)
  37. # Register Blueprints
  38. app.register_blueprint(explore_bp)
  39. app.register_blueprint(block_bp)
  40. app.register_blueprint(contract_bp)
  41. app.register_blueprint(transaction_bp)
  42. # Define page not found error handler
  43. @app.errorhandler(404)
  44. def page_not_found(e):
  45. """
  46. Handles 404 errors by rendering a custom 404 error page when a requested page is not found,
  47. returning a rendered template along with a 404 status code.
  48. Args:
  49. e: The error object associated with the 404 error.
  50. """
  51. # Render the custom 404 error page
  52. return render_template('404.html'), 404
  53. # Define internal server error handler
  54. @app.errorhandler(500)
  55. def internal_server_error(e):
  56. """
  57. Handles 500 errors by rendering a custom 500 error page when an internal server error occurs,
  58. returning a rendered template along with a 500 status code.
  59. Args:
  60. e: The error object associated with the 500 error.
  61. """
  62. # Render the custom 500 error page
  63. return render_template('500.html'), 500
  64. return app