Skip to content

Commit

Permalink
Put api keys directly in js layer definitions
Browse files Browse the repository at this point in the history
Also don't generate definitions for layers that have require missing api keys.
  • Loading branch information
AntonKhorev committed Nov 24, 2024
1 parent 9b533b2 commit 37ce56f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
14 changes: 4 additions & 10 deletions app/assets/javascripts/leaflet.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,13 @@ L.OSM.Map = L.Map.extend({
this.baseLayers = [];

for (const layerDefinition of OSM.LAYER_DEFINITIONS) {
if (layerDefinition.apiKeyId && !OSM[layerDefinition.apiKeyId]) continue;

const layerOptions = {
const layer = new L.OSM[layerDefinition.leafletOsmId]({
attribution: makeAttribution(layerDefinition.credit),
code: layerDefinition.code,
keyid: layerDefinition.keyId,
name: I18n.t(`javascripts.map.base.${layerDefinition.nameId}`)
};
if (layerDefinition.apiKeyId) {
layerOptions.apikey = OSM[layerDefinition.apiKeyId];
}

const layer = new L.OSM[layerDefinition.leafletOsmId](layerOptions);
name: I18n.t(`javascripts.map.base.${layerDefinition.nameId}`),
apikey: layerDefinition.apiKey
});
this.baseLayers.push(layer);
}

Expand Down
10 changes: 1 addition & 9 deletions app/assets/javascripts/osm.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@ OSM = {
FOSSGIS_VALHALLA_URL: <%= Settings.fossgis_valhalla_url.to_json %>,
DEFAULT_LOCALE: <%= I18n.default_locale.to_json %>,

<% if Settings.key?(:thunderforest_key) %>
THUNDERFOREST_KEY: <%= Settings.thunderforest_key.to_json %>,
<% end %>

<% if Settings.key?(:tracestrack_key) %>
TRACESTRACK_KEY: <%= Settings.tracestrack_key.to_json %>,
<% end %>

LAYER_DEFINITIONS: <%= YAML.load_file(Rails.root.join("config/layers.yml")).to_json %>,
LAYER_DEFINITIONS: <%= MapLayers::definitions("config/layers.yml").to_json %>,
LAYERS_WITH_MAP_KEY: <%= YAML.load_file(Rails.root.join("config/key.yml")).keys.to_json %>,

MARKER_GREEN: <%= image_path("marker-green.png").to_json %>,
Expand Down
6 changes: 3 additions & 3 deletions config/layers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
code: "C"
keyId: "cyclemap"
nameId: "cycle_map"
apiKeyId: "THUNDERFOREST_KEY"
apiKeyId: "thunderforest_key"
credit:
id: "thunderforest_credit"
children:
Expand All @@ -37,7 +37,7 @@
code: "T"
keyId: "transportmap"
nameId: "transport_map"
apiKeyId: "THUNDERFOREST_KEY"
apiKeyId: "thunderforest_key"
credit:
id: "thunderforest_credit"
children:
Expand All @@ -49,7 +49,7 @@
code: "P"
keyId: "tracestracktopo"
nameId: "tracestracktop_topo"
apiKeyId: "TRACESTRACK_KEY"
apiKeyId: "tracestrack_key"
credit:
id: "tracestrack_credit"
children:
Expand Down
15 changes: 15 additions & 0 deletions lib/map_layers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module MapLayers
def self.definitions(layers_filename)
YAML.load_file(Rails.root.join(layers_filename)).filter_map do |layer|
if layer["apiKeyId"]
if Settings.key?(layer["apiKeyId"].to_sym)
layer["apiKey"] = Settings[layer["apiKeyId"]]
layer.delete "apiKeyId"
layer
end
else
layer
end
end
end
end

0 comments on commit 37ce56f

Please sign in to comment.