diff --git a/.app_version b/.app_version index 6aec9e54..e756a90e 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.21.4 +0.21.5 diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml index 5e2f006a..73b7cf9f 100644 --- a/.devcontainer/docker-compose.yml +++ b/.devcontainer/docker-compose.yml @@ -31,8 +31,6 @@ services: TIME_ZONE: Europe/London APPLICATION_PROTOCOL: http DISTANCE_UNIT: km - PHOTON_API_HOST: photon.komoot.io - PHOTON_API_USE_HTTPS: true PROMETHEUS_EXPORTER_ENABLED: false PROMETHEUS_EXPORTER_HOST: 0.0.0.0 PROMETHEUS_EXPORTER_PORT: 9394 diff --git a/.env.development b/.env.development index 24313ecb..e083342f 100644 --- a/.env.development +++ b/.env.development @@ -4,5 +4,4 @@ DATABASE_PASSWORD=password DATABASE_NAME=dawarich_development DATABASE_PORT=5432 REDIS_URL=redis://localhost:6379/1 -PHOTON_API_HOST='photon.komoot.io' DISTANCE_UNIT='km' diff --git a/CHANGELOG.md b/CHANGELOG.md index 8962c791..737cf0c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# 0.21.5 - 2025-01-07 + +You may now use Geoapify API for reverse geocoding. To obrain an API key, sign up at https://myprojects.geoapify.com/ and create a new project. Make sure you have read and understood the [pricing policy](https://www.geoapify.com/pricing) and [Terms and Conditions](https://www.geoapify.com/terms-and-conditions/). + +### Added + +- Geoapify API support for reverse geocoding. Provide `GEOAPIFY_API_KEY` env var to use it. + +### Removed + +- Photon ENV vars from the `.env.development` and docker-compose.yml files. + # 0.21.4 - 2025-01-05 ### Fixed diff --git a/app/jobs/reverse_geocoding_job.rb b/app/jobs/reverse_geocoding_job.rb index dc49d2a2..d368720f 100644 --- a/app/jobs/reverse_geocoding_job.rb +++ b/app/jobs/reverse_geocoding_job.rb @@ -18,8 +18,8 @@ class ReverseGeocodingJob < ApplicationJob end def rate_limit_for_photon_api - return unless PHOTON_API_HOST == 'photon.komoot.io' + return unless DawarichSettings.photon_enabled? - sleep 1 if PHOTON_API_HOST == 'photon.komoot.io' + sleep 1 if DawarichSettings.photon_uses_komoot_io? end end diff --git a/app/services/reverse_geocoding/places/fetch_data.rb b/app/services/reverse_geocoding/places/fetch_data.rb index 0ed4e236..9eec9de4 100644 --- a/app/services/reverse_geocoding/places/fetch_data.rb +++ b/app/services/reverse_geocoding/places/fetch_data.rb @@ -12,8 +12,8 @@ class ReverseGeocoding::Places::FetchData end def call - if ::PHOTON_API_HOST.blank? - Rails.logger.warn('PHOTON_API_HOST is not set') + unless DawarichSettings.reverse_geocoding_enabled? + Rails.logger.warn('Reverse geocoding is not enabled') return end diff --git a/config/initializers/01_constants.rb b/config/initializers/01_constants.rb index bfa380b6..eef38298 100644 --- a/config/initializers/01_constants.rb +++ b/config/initializers/01_constants.rb @@ -1,11 +1,19 @@ # frozen_string_literal: true MIN_MINUTES_SPENT_IN_CITY = ENV.fetch('MIN_MINUTES_SPENT_IN_CITY', 60).to_i +DISTANCE_UNIT = ENV.fetch('DISTANCE_UNIT', 'km').to_sym + +APP_VERSION = File.read('.app_version').strip + +TELEMETRY_STRING = Base64.encode64('IjVFvb8j3P9-ArqhSGav9j8YcJaQiuNIzkfOPKQDk2lvKXqb8t1NSRv50oBkaKtlrB_ZRzO9NdurpMtncV_HYQ==') +TELEMETRY_URL = 'https://influxdb2.frey.today/api/v2/write' + +# Reverse geocoding settings REVERSE_GEOCODING_ENABLED = ENV.fetch('REVERSE_GEOCODING_ENABLED', 'true') == 'true' + PHOTON_API_HOST = ENV.fetch('PHOTON_API_HOST', nil) PHOTON_API_KEY = ENV.fetch('PHOTON_API_KEY', nil) PHOTON_API_USE_HTTPS = ENV.fetch('PHOTON_API_USE_HTTPS', 'true') == 'true' -DISTANCE_UNIT = ENV.fetch('DISTANCE_UNIT', 'km').to_sym -APP_VERSION = File.read('.app_version').strip -TELEMETRY_STRING = Base64.encode64('IjVFvb8j3P9-ArqhSGav9j8YcJaQiuNIzkfOPKQDk2lvKXqb8t1NSRv50oBkaKtlrB_ZRzO9NdurpMtncV_HYQ==') -TELEMETRY_URL = 'https://influxdb2.frey.today/api/v2/write' + +GEOAPIFY_API_KEY = ENV.fetch('GEOAPIFY_API_KEY', nil) +# /Reverse geocoding settings diff --git a/config/initializers/03_dawarich_settings.rb b/config/initializers/03_dawarich_settings.rb new file mode 100644 index 00000000..9d632067 --- /dev/null +++ b/config/initializers/03_dawarich_settings.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class DawarichSettings + class << self + def reverse_geocoding_enabled? + photon_enabled? || geoapify_enabled? + end + + def photon_enabled? + PHOTON_API_HOST.present? + end + + def photon_uses_komoot_io? + PHOTON_API_HOST == 'photon.komoot.io' + end + + def geoapify_enabled? + GEOAPIFY_API_KEY.present? + end + end +end diff --git a/config/initializers/geocoder.rb b/config/initializers/geocoder.rb index 837fb394..eb1a7fc4 100644 --- a/config/initializers/geocoder.rb +++ b/config/initializers/geocoder.rb @@ -12,11 +12,13 @@ settings = { } } -if defined?(PHOTON_API_HOST) +if PHOTON_API_HOST.present? settings[:lookup] = :photon settings[:photon] = { use_https: PHOTON_API_USE_HTTPS, host: PHOTON_API_HOST } + settings[:http_headers] = { 'X-Api-Key' => PHOTON_API_KEY } if defined?(PHOTON_API_KEY) +elsif GEOAPIFY_API_KEY.present? + settings[:lookup] = :geoapify + settings[:api_key] = GEOAPIFY_API_KEY end -settings[:http_headers] = { 'X-Api-Key' => PHOTON_API_KEY } if defined?(PHOTON_API_KEY) - Geocoder.configure(settings) diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index 9e54f2ab..d9dec786 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -27,4 +27,4 @@ Sidekiq.configure_client do |config| config.redis = { url: ENV['REDIS_URL'] } end -Sidekiq::Queue['reverse_geocoding'].limit = 1 if Sidekiq.server? && PHOTON_API_HOST == 'photon.komoot.io' +Sidekiq::Queue['reverse_geocoding'].limit = 1 if Sidekiq.server? && DawarichSettings.photon_uses_komoot_io? diff --git a/docker-compose.yml b/docker-compose.yml index fc46ae30..688f0ff2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -67,8 +67,6 @@ services: TIME_ZONE: Europe/London APPLICATION_PROTOCOL: http DISTANCE_UNIT: km - PHOTON_API_HOST: photon.komoot.io - PHOTON_API_USE_HTTPS: true PROMETHEUS_EXPORTER_ENABLED: false PROMETHEUS_EXPORTER_HOST: 0.0.0.0 PROMETHEUS_EXPORTER_PORT: 9394 @@ -122,8 +120,6 @@ services: BACKGROUND_PROCESSING_CONCURRENCY: 10 APPLICATION_PROTOCOL: http DISTANCE_UNIT: km - PHOTON_API_HOST: photon.komoot.io - PHOTON_API_USE_HTTPS: true PROMETHEUS_EXPORTER_ENABLED: false PROMETHEUS_EXPORTER_HOST: dawarich_app PROMETHEUS_EXPORTER_PORT: 9394 diff --git a/docker-compose_mounted_volumes.yml b/docker-compose_mounted_volumes.yml index e724aa88..e355cf59 100644 --- a/docker-compose_mounted_volumes.yml +++ b/docker-compose_mounted_volumes.yml @@ -45,8 +45,6 @@ services: APPLICATION_HOSTS: localhost APPLICATION_PROTOCOL: http DISTANCE_UNIT: km - PHOTON_API_HOST: photon.komoot.io - PHOTON_API_USE_HTTPS: true stdin_open: true tty: true entrypoint: dev-entrypoint.sh @@ -101,8 +99,6 @@ services: BACKGROUND_PROCESSING_CONCURRENCY: 10 APPLICATION_PROTOCOL: http DISTANCE_UNIT: km - PHOTON_API_HOST: photon.komoot.io - PHOTON_API_USE_HTTPS: true stdin_open: true tty: true entrypoint: dev-entrypoint.sh diff --git a/spec/lib/dawarich_settings_spec.rb b/spec/lib/dawarich_settings_spec.rb new file mode 100644 index 00000000..304ac2de --- /dev/null +++ b/spec/lib/dawarich_settings_spec.rb @@ -0,0 +1,94 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe DawarichSettings do + describe '.reverse_geocoding_enabled?' do + context 'when photon is enabled' do + before do + allow(described_class).to receive(:photon_enabled?).and_return(true) + allow(described_class).to receive(:geoapify_enabled?).and_return(false) + end + + it 'returns true' do + expect(described_class.reverse_geocoding_enabled?).to be true + end + end + + context 'when geoapify is enabled' do + before do + allow(described_class).to receive(:photon_enabled?).and_return(false) + allow(described_class).to receive(:geoapify_enabled?).and_return(true) + end + + it 'returns true' do + expect(described_class.reverse_geocoding_enabled?).to be true + end + end + + context 'when neither service is enabled' do + before do + allow(described_class).to receive(:photon_enabled?).and_return(false) + allow(described_class).to receive(:geoapify_enabled?).and_return(false) + end + + it 'returns false' do + expect(described_class.reverse_geocoding_enabled?).to be false + end + end + end + + describe '.photon_enabled?' do + context 'when PHOTON_API_HOST is present' do + before { stub_const('PHOTON_API_HOST', 'photon.example.com') } + + it 'returns true' do + expect(described_class.photon_enabled?).to be true + end + end + + context 'when PHOTON_API_HOST is blank' do + before { stub_const('PHOTON_API_HOST', '') } + + it 'returns false' do + expect(described_class.photon_enabled?).to be false + end + end + end + + describe '.photon_uses_komoot_io?' do + context 'when PHOTON_API_HOST is komoot.io' do + before { stub_const('PHOTON_API_HOST', 'photon.komoot.io') } + + it 'returns true' do + expect(described_class.photon_uses_komoot_io?).to be true + end + end + + context 'when PHOTON_API_HOST is different' do + before { stub_const('PHOTON_API_HOST', 'photon.example.com') } + + it 'returns false' do + expect(described_class.photon_uses_komoot_io?).to be false + end + end + end + + describe '.geoapify_enabled?' do + context 'when GEOAPIFY_API_KEY is present' do + before { stub_const('GEOAPIFY_API_KEY', 'some-api-key') } + + it 'returns true' do + expect(described_class.geoapify_enabled?).to be true + end + end + + context 'when GEOAPIFY_API_KEY is blank' do + before { stub_const('GEOAPIFY_API_KEY', '') } + + it 'returns false' do + expect(described_class.geoapify_enabled?).to be false + end + end + end +end