app.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # This file is part of DarkFi (https://dark.fi)
  2. #
  3. # Copyright (C) 2020-2024 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. import asyncio
  18. from flask import Flask, request, render_template
  19. import rpc
  20. app = Flask(__name__)
  21. @app.route('/')
  22. async def index():
  23. blocks = await rpc.get_last_n_blocks("10")
  24. stats = await rpc.get_basic_statistics()
  25. return render_template('index.html', blocks=blocks, stats=stats)
  26. @app.route('/search', methods=['GET', 'POST'])
  27. async def search():
  28. search_hash = request.args.get('search_hash', '')
  29. try:
  30. block = await rpc.get_block(search_hash)
  31. transactions = await rpc.get_block_transactions(search_hash)
  32. return render_template('block.html', block=block, transactions=transactions)
  33. except Exception:
  34. transaction = await rpc.get_transaction(search_hash)
  35. return render_template('transaction.html', transaction=transaction)
  36. @app.route('/block/<header_hash>')
  37. async def block(header_hash):
  38. block = await rpc.get_block(header_hash)
  39. transactions = await rpc.get_block_transactions(header_hash)
  40. return render_template('block.html', block=block, transactions=transactions)
  41. @app.route('/transaction/<transaction_hash>')
  42. async def transaction(transaction_hash):
  43. transaction = await rpc.get_transaction(transaction_hash)
  44. return render_template('transaction.html', transaction=transaction)
  45. @app.errorhandler(404)
  46. def page_not_found(e):
  47. return render_template('404.html'), 404
  48. @app.errorhandler(500)
  49. def page_not_found(e):
  50. return render_template('500.html'), 500