Add support for provider selection (#346)

This commit is contained in:
soky srm
2025-11-17 18:13:20 +01:00
committed by GitHub
parent 0290ed636f
commit 0d798c522a
8 changed files with 109 additions and 15 deletions

View File

@@ -10,10 +10,26 @@ class Settings::HostingsController < ApplicationController
[ "Home", root_path ],
[ "Self-Hosting", nil ]
]
twelve_data_provider = Provider::Registry.get_provider(:twelve_data)
@twelve_data_usage = twelve_data_provider&.usage
@yahoo_finance_provider = Provider::Registry.get_provider(:yahoo_finance)
# Determine which providers are currently selected
exchange_rate_provider = ENV["EXCHANGE_RATE_PROVIDER"].presence || Setting.exchange_rate_provider
securities_provider = ENV["SECURITIES_PROVIDER"].presence || Setting.securities_provider
# Show Twelve Data settings if either provider is set to twelve_data
@show_twelve_data_settings = exchange_rate_provider == "twelve_data" || securities_provider == "twelve_data"
# Show Yahoo Finance settings if either provider is set to yahoo_finance
@show_yahoo_finance_settings = exchange_rate_provider == "yahoo_finance" || securities_provider == "yahoo_finance"
# Only fetch provider data if we're showing the section
if @show_twelve_data_settings
twelve_data_provider = Provider::Registry.get_provider(:twelve_data)
@twelve_data_usage = twelve_data_provider&.usage
end
if @show_yahoo_finance_settings
@yahoo_finance_provider = Provider::Registry.get_provider(:yahoo_finance)
end
end
def update
@@ -34,6 +50,14 @@ class Settings::HostingsController < ApplicationController
Setting.twelve_data_api_key = hosting_params[:twelve_data_api_key]
end
if hosting_params.key?(:exchange_rate_provider)
Setting.exchange_rate_provider = hosting_params[:exchange_rate_provider]
end
if hosting_params.key?(:securities_provider)
Setting.securities_provider = hosting_params[:securities_provider]
end
if hosting_params.key?(:openai_access_token)
token_param = hosting_params[:openai_access_token].to_s.strip
# Ignore blanks and redaction placeholders to prevent accidental overwrite
@@ -71,7 +95,7 @@ class Settings::HostingsController < ApplicationController
private
def hosting_params
params.require(:setting).permit(:onboarding_state, :require_email_confirmation, :brand_fetch_client_id, :twelve_data_api_key, :openai_access_token, :openai_uri_base, :openai_model)
params.require(:setting).permit(:onboarding_state, :require_email_confirmation, :brand_fetch_client_id, :twelve_data_api_key, :openai_access_token, :openai_uri_base, :openai_model, :exchange_rate_provider, :securities_provider)
end
def ensure_admin

View File

@@ -3,7 +3,7 @@ module ExchangeRate::Provided
class_methods do
def provider
provider = ENV["EXCHANGE_RATE_PROVIDER"] || "twelve_data"
provider = ENV["EXCHANGE_RATE_PROVIDER"].presence || Setting.exchange_rate_provider
registry = Provider::Registry.for_concept(:exchange_rates)
registry.get_provider(provider.to_sym)
end

View File

@@ -5,7 +5,7 @@ module Security::Provided
class_methods do
def provider
provider = ENV["SECURITIES_PROVIDER"] || "twelve_data"
provider = ENV["SECURITIES_PROVIDER"].presence || Setting.securities_provider
registry = Provider::Registry.for_concept(:securities)
registry.get_provider(provider.to_sym)
end

View File

@@ -11,6 +11,10 @@ class Setting < RailsSettings::Base
field :openai_model, type: :string, default: ENV["OPENAI_MODEL"]
field :brand_fetch_client_id, type: :string, default: ENV["BRAND_FETCH_CLIENT_ID"]
# Provider selection
field :exchange_rate_provider, type: :string, default: ENV.fetch("EXCHANGE_RATE_PROVIDER", "twelve_data")
field :securities_provider, type: :string, default: ENV.fetch("SECURITIES_PROVIDER", "twelve_data")
# Dynamic fields are now stored as individual entries with "dynamic:" prefix
# This prevents race conditions and ensures each field is independently managed

View File

@@ -0,0 +1,51 @@
<div class="space-y-4">
<div>
<h2 class="font-medium mb-1"><%= t(".title") %></h2>
<p class="text-secondary text-sm mb-4"><%= t(".description") %></p>
</div>
<%= styled_form_with model: Setting.new,
url: settings_hosting_path,
method: :patch,
data: {
controller: "auto-submit-form",
"auto-submit-form-trigger-event-value": "change"
} do |form| %>
<div class="space-y-4">
<%= form.select :exchange_rate_provider,
[
[t(".providers.twelve_data"), "twelve_data"],
[t(".providers.yahoo_finance"), "yahoo_finance"]
],
{ label: t(".exchange_rate_provider_label") },
{
value: ENV.fetch("EXCHANGE_RATE_PROVIDER", Setting.exchange_rate_provider),
disabled: ENV["EXCHANGE_RATE_PROVIDER"].present?,
data: { "auto-submit-form-target": "auto" }
} %>
<%= form.select :securities_provider,
[
[t(".providers.twelve_data"), "twelve_data"],
[t(".providers.yahoo_finance"), "yahoo_finance"]
],
{ label: t(".securities_provider_label") },
{
value: ENV.fetch("SECURITIES_PROVIDER", Setting.securities_provider),
disabled: ENV["SECURITIES_PROVIDER"].present?,
data: { "auto-submit-form-target": "auto" }
} %>
</div>
<% if ENV["EXCHANGE_RATE_PROVIDER"].present? || ENV["SECURITIES_PROVIDER"].present? %>
<div class="mt-4 bg-warning-50 border border-warning-200 rounded-lg p-3">
<div class="flex items-start gap-2">
<%= icon("alert-circle", class: "w-5 h-5 text-warning-600 mt-0.5 shrink-0") %>
<p class="text-sm text-warning-800">
<%= t(".env_configured_message") %>
</p>
</div>
</div>
<% end %>
<% end %>
</div>

View File

@@ -6,7 +6,7 @@
<% if @yahoo_finance_provider&.healthy? %>
<div class="space-y-4">
<div class="flex items-center space-x-3">
<div class="w-3 h-3 bg-green-500 rounded-full"></div>
<div class="w-3 h-3 bg-success rounded-full"></div>
<p class="text-sm text-secondary">
<%= t(".status_active") %>
</p>
@@ -15,18 +15,19 @@
<% else %>
<div class="space-y-4">
<div class="flex items-center space-x-3">
<div class="w-3 h-3 bg-red-500 rounded-full"></div>
<div class="w-3 h-3 bg-destructive rounded-full"></div>
<p class="text-sm text-secondary">
<%= t(".status_inactive") %>
</p>
</div>
<div class="bg-red-50 border border-red-200 rounded-lg p-3">
<div class="flex">
<div class="ml-3">
<h3 class="text-sm font-medium text-red-800">
<div class="bg-destructive-50 border border-destructive-200 rounded-lg p-3">
<div class="flex items-start gap-2">
<%= icon("alert-circle", class: "w-5 h-5 text-destructive-600 mt-0.5 shrink-0") %>
<div>
<h3 class="text-sm font-medium text-destructive-800">
<%= t(".connection_failed") %>
</h3>
<div class="mt-2 text-sm text-red-700">
<div class="mt-2 text-sm text-destructive-700">
<p><%= t(".troubleshooting") %></p>
</div>
</div>

View File

@@ -7,8 +7,13 @@
<% end %>
<%= settings_section title: t(".financial_data_providers") do %>
<div class="space-y-6">
<%= render "settings/hostings/yahoo_finance_settings" %>
<%= render "settings/hostings/twelve_data_settings" %>
<%= render "settings/hostings/provider_selection" %>
<% if @show_yahoo_finance_settings %>
<%= render "settings/hostings/yahoo_finance_settings" %>
<% end %>
<% if @show_twelve_data_settings %>
<%= render "settings/hostings/twelve_data_settings" %>
<% end %>
</div>
<% end %>
<%= settings_section title: t(".invites") do %>

View File

@@ -25,6 +25,15 @@ en:
confirm_clear_cache:
title: Clear data cache?
body: Are you sure you want to clear the data cache? This will remove all exchange rates, security prices, account balances, and other data. This action cannot be undone.
provider_selection:
title: Provider Selection
description: Choose which service to use for fetching exchange rates and security prices. Yahoo Finance is free and doesn't require an API key. Twelve Data requires a free API key but may offer more data coverage.
exchange_rate_provider_label: Exchange Rate Provider
securities_provider_label: Securities (Stock Prices) Provider
env_configured_message: Provider selection is disabled because environment variables (EXCHANGE_RATE_PROVIDER or SECURITIES_PROVIDER) are set. To enable selection here, remove these environment variables from your configuration.
providers:
twelve_data: Twelve Data
yahoo_finance: Yahoo Finance
brand_fetch_settings:
description: Enter the Client ID provided by Brand Fetch
label: Client ID