Explorar el Código

explorer/site: Add support for TOML-based configuration

This update improves the configuration setup for the explorer site by replacing hardcoded values with a TOML-based configuration file, enabling easier configuration and deployment across different networks, such as devnet, testnet, and mainnet.

- Introduced a `load_toml_config` function to load environment-specific key-value pairs from a TOML configuration file into the Flask app's `app.config`
- Created an initial `site_config.toml` file to define default configurations for various environments (e.g., localnet, devnet, testnet, and mainnet). The configurations for devnet, testnet, and mainnet are still a work in progress and marked as TODOs.
- Updated the RPC module to connect to the explorerd using the values specified in the configuration file
- Added tomli to requirements.txt
kalm hace 1 año
padre
commit
1a684c6702

+ 49 - 0
bin/explorer/site/app.py

@@ -23,6 +23,9 @@ related to blocks, contracts, transactions, search, and the explore section, inc
 error handlers, ensuring appropriate responses for these common HTTP errors.
 error handlers, ensuring appropriate responses for these common HTTP errors.
 """
 """
 
 
+import os
+import tomli
+
 from flask import Flask, render_template
 from flask import Flask, render_template
 
 
 from blueprints.explore import explore_bp
 from blueprints.explore import explore_bp
@@ -41,6 +44,10 @@ def create_app():
     """
     """
     app = Flask(__name__)
     app = Flask(__name__)
 
 
+    # Load the app TOML configuration
+    env = os.getenv("FLASK_ENV", "localnet")
+    load_toml_config(app, env)
+
     # Register Blueprints
     # Register Blueprints
     app.register_blueprint(explore_bp)
     app.register_blueprint(explore_bp)
     app.register_blueprint(block_bp)
     app.register_blueprint(block_bp)
@@ -74,3 +81,45 @@ def create_app():
         return render_template('500.html'), 500
         return render_template('500.html'), 500
 
 
     return app
     return app
+
+def load_toml_config(app, env="localnet", config_path="site_config.toml"):
+    """
+    Loads environment-specific key-value pairs from a TOML configuration file into `app.config`.
+
+    Args:
+        app (Flask): The Flask application.
+        env (str): The name of the environment section to load (default is "localnet").
+        config_path (str): The path to the TOML configuration file.
+
+    Raises:
+        FileNotFoundError: If the configuration file cannot be found.
+        KeyError: If the specified environment section does not exist.
+    """
+
+    # Verify that the configuration file exists
+    if not os.path.isfile(config_path):
+        raise FileNotFoundError(f"Configuration file '{config_path}' not found.")
+
+    # Open and parse the configuration file (TOML)
+    with open(config_path, "rb") as f:
+        config = tomli.load(f)
+
+    # Ensure the specified environment section exists in the configuration
+    if env not in config:
+        raise KeyError(f"Environment '{env}' not found in {config_path}")
+
+    # Load the environment specific configurations into the Flask app's config object
+    for key, value in config[env].items():
+        app.config[key.upper()] = value
+
+    # Print the loaded configuration for debugging or confirmation purposes
+    print("\n" + "=" * 40)
+    print("Loaded Explorer Site Configuration")
+    print("=" * 40)
+
+    for key in config[env]:
+        print(f"{key.upper()} = {app.config[key.upper()]}")
+
+    print("=" * 40 + "\n")
+
+

+ 2 - 1
bin/explorer/site/requirements.txt

@@ -1,2 +1,3 @@
 flask[async]
 flask[async]
-Pygments
+Pygments
+tomli

+ 2 - 6
bin/explorer/site/rpc.py

@@ -25,11 +25,7 @@ and handle responses from the server.
 
 
 import asyncio, json, random
 import asyncio, json, random
 
 
-from flask import abort
-
-# DarkFi explorer daemon JSON-RPC configuration
-URL = "127.0.0.1"
-PORT = 14567
+from flask import abort, current_app
 
 
 class Channel:
 class Channel:
     """Class representing the channel with the JSON-RPC server."""
     """Class representing the channel with the JSON-RPC server."""
@@ -82,7 +78,7 @@ async def query(method, params):
      returning the result of the query or raising an error if the request fails.
      returning the result of the query or raising an error if the request fails.
     """
     """
     # Create the channel to send RPC request
     # Create the channel to send RPC request
-    channel = await create_channel(URL, PORT)
+    channel = await create_channel(current_app.config['EXPLORER_RPC_URL'], current_app.config['EXPLORER_RPC_PORT'])
 
 
     # Prepare request
     # Prepare request
     request = {
     request = {

+ 36 - 0
bin/explorer/site/site_config.toml

@@ -0,0 +1,36 @@
+# This file is part of DarkFi (https://dark.fi)
+#
+# Copyright (C) 2020-2025 Dyne.org foundation
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+# Localnet Configuration
+[localnet]
+EXPLORER_RPC_URL = "127.0.0.1"
+EXPLORER_RPC_PORT = 14567
+
+# Devnet Configuration
+[devnet]
+EXPLORER_RPC_URL = "TODO"
+EXPLORER_RPC_PORT = "TODO"
+
+# Testnet Configuration
+[testnet]
+EXPLORER_RPC_URL = "TODO"
+EXPLORER_RPC_PORT = "TODO"
+
+# Mainnet Configuration
+[mainnet]
+EXPLORER_RPC_URL = "TODO"
+EXPLORER_RPC_PORT = "TODO"