Skip to content

Commit

Permalink
explorer/site: Add support for TOML-based configuration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kalm committed Feb 22, 2025
1 parent c98d728 commit 1a684c6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
49 changes: 49 additions & 0 deletions bin/explorer/site/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
error handlers, ensuring appropriate responses for these common HTTP errors.
"""

import os
import tomli

from flask import Flask, render_template

from blueprints.explore import explore_bp
Expand All @@ -41,6 +44,10 @@ def create_app():
"""
app = Flask(__name__)

# Load the app TOML configuration
env = os.getenv("FLASK_ENV", "localnet")
load_toml_config(app, env)

# Register Blueprints
app.register_blueprint(explore_bp)
app.register_blueprint(block_bp)
Expand Down Expand Up @@ -74,3 +81,45 @@ def internal_server_error(e):
return render_template('500.html'), 500

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")


3 changes: 2 additions & 1 deletion bin/explorer/site/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
flask[async]
Pygments
Pygments
tomli
8 changes: 2 additions & 6 deletions bin/explorer/site/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@

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 representing the channel with the JSON-RPC server."""
Expand Down Expand Up @@ -82,7 +78,7 @@ async def query(method, params):
returning the result of the query or raising an error if the request fails.
"""
# 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
request = {
Expand Down
36 changes: 36 additions & 0 deletions bin/explorer/site/site_config.toml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 1a684c6

Please sign in to comment.