Skip to content

Commit ce87c39

Browse files
committed
Move config to config.json file
This commit allows the user to have greater control of the services, layers and overlays in the frontend. Instead of only being able to send one backend variable, which would then be replaced into a javscript file read as a text file by hardcoded values, this file can be passed to the docker container with the -v command. The README file has been updated to describe the changes.
1 parent 540a4ef commit ce87c39

File tree

6 files changed

+92
-125
lines changed

6 files changed

+92
-125
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ Serves the frontend at `http://localhost:9966` running queries against the routi
1616
docker run -p 9966:9966 ghcr.io/project-osrm/osrm-frontend:latest
1717
```
1818

19-
Per default routing requests are made against the backend at `http://localhost:5000`.
20-
You can change the backend by using `-e OSRM_BACKEND='http://localhost:5001'` in the `docker run` command.
19+
By default, the docker container uses the settings in `config/config.json` file. If you'd like to provide a custom config file (with your own backend server, layers and overlays), you can run the container with the following command:
20+
21+
```
22+
docker run -p 9966:9966 -v ./YOUR_CONFIG.json:/src/config/config.json osrm-frontend
23+
```
2124

2225
In case Docker complains about not being able to connect to the Docker daemon make sure you are in the `docker` group.
2326

@@ -56,15 +59,20 @@ npm run start-index
5659

5760
## Changing Backends
5861

59-
In `src/leaflet_options.js` adjust:
62+
In `config/config.json` add or adjust your own services:
6063

6164
```
6265
services: [{
63-
label: 'Car (fastest)',
66+
label: 'Car',
6467
path: 'http://localhost:5000/route/v1'
6568
}],
6669
```
6770

71+
You can also adjust your own layers and overlays.
72+
73+
## Debug
74+
> Note: "debug" pages currently do not work "out of the box" and require manual configuration
75+
6876
For debug tiles showing speeds and small components available at `/debug` adjust in `debug/index.html`
6977

7078
```

config/config.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"zoom": 13,
3+
"center": [
4+
50.8503,
5+
4.3517
6+
],
7+
"language": "en",
8+
"layers": [
9+
{
10+
"name": "openstreetmap.org",
11+
"url": "//tile.openstreetmap.org/{z}/{x}/{y}.png",
12+
"attribution": "© <a href=\"https://www.openstreetmap.org/copyright/en\">OpenStreetMap</a> contributors"
13+
},
14+
{
15+
"name": "openstreetmap.de",
16+
"url": "//tile.openstreetmap.de/tiles/osmde/{z}/{x}/{y}.png",
17+
"attribution": "<a target=\"_blank\" href=\"http://www.openstreetmap.org/\">Karte hergestellt aus OpenStreetMap-Daten</a> | Lizenz: <a rel=\"license\" target=\"_blank\" href=\"http://opendatacommons.org/licenses/odbl/\">Open Database License (ODbL)</a>"
18+
}
19+
],
20+
"overlays":[
21+
{
22+
"name": "Hiking",
23+
"url": "//tile.waymarkedtrails.org/hiking/{z}/{x}/{y}.png",
24+
"attribution": "© <a href=\"http://waymarkedtrails.org\">Sarah Hoffmann</a> (<a href=\"https://creativecommons.org/licenses/by-sa/3.0/\">CC-BY-SA</a>)"
25+
},
26+
{
27+
"name": "Bike",
28+
"url": "//tile.waymarkedtrails.org/cycling/{z}/{x}/{y}.png",
29+
"attribution": "© <a href=\"http://waymarkedtrails.org\">Sarah Hoffmann</a> (<a href=\"https://creativecommons.org/licenses/by-sa/3.0/\">CC-BY-SA</a>)"
30+
},
31+
{
32+
"name": "Small Components",
33+
"url": "https://tools.geofabrik.de/osmi/tiles/routing/{z}/{x}/{y}.png"
34+
}
35+
36+
],
37+
"services": [
38+
{
39+
"label": "Car",
40+
"path": "https://routing.openstreetmap.de/routed-car/route/v1",
41+
"debug": "car"
42+
},
43+
{
44+
"label": "Bike",
45+
"path": "https://routing.openstreetmap.de/routed-bike/route/v1",
46+
"debug": "bike"
47+
},
48+
{
49+
"label": "Foot",
50+
"path": "https://routing.openstreetmap.de/routed-foot/route/v1",
51+
"debug": "foot"
52+
}
53+
]
54+
}

docker/Dockerfile

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
FROM alpine:3.21
22

3-
# Enables customized options using environment variables
4-
ENV OSRM_BACKEND='http://localhost:5000'
5-
ENV OSRM_CENTER='38.8995,-77.0269'
6-
ENV OSRM_ZOOM='13'
7-
ENV OSRM_LANGUAGE='en'
8-
ENV OSRM_LABEL='Car (fastest)'
9-
ENV OSRM_MAPBOX_TOKEN='pk.eyJ1IjoibXNsZWUiLCJhIjoiclpiTWV5SSJ9.P_h8r37vD8jpIH1A6i1VRg'
10-
113
# Copy package.json
124
RUN mkdir -p /src
135
COPY package.json /src

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
"main": "src/index.js",
66
"scripts": {
77
"test": "eslint src/*.js i18n/*.js",
8-
"replace": "node ./scripts/replace.js",
98
"compile": "browserify -d src/index.js -s osrm > bundle.raw.js && uglifyjs bundle.raw.js -c -m --source-map filename=bundle.js.map -o bundle.js",
10-
"build": "npm run replace && npm run compile && cp node_modules/leaflet/dist/leaflet.css css/leaflet.css",
9+
"build": "npm run compile && cp node_modules/leaflet/dist/leaflet.css css/leaflet.css",
1110
"start-index": "budo src/index.js --serve=bundle.js --live -d | bistre",
1211
"start": "npm run build && npm run start-index",
1312
"prepub": "npm run build"

scripts/replace.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/leaflet_options.js

Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,39 @@
11
'use strict';
22

33
var L = require('leaflet');
4+
const config = require('../config/config.json');
45

5-
var mapboxTileURL = 'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}',
6-
mapboxAttribution = '© <a href="https://www.mapbox.com/about/maps/">Mapbox</a> © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> <strong><a href="https://www.mapbox.com/map-feedback/" target="_blank">Improve this map</a></strong>',
7-
mapboxToken = 'pk.eyJ1IjoibXNsZWUiLCJhIjoiclpiTWV5SSJ9.P_h8r37vD8jpIH1A6i1VRg',
8-
osmAttribution = '© <a href="https://www.openstreetmap.org/copyright/en">OpenStreetMap</a> contributors',
9-
waymarkedtrailsAttribution = '© <a href="http://waymarkedtrails.org">Sarah Hoffmann</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)';
6+
var layers = {};
7+
var overlays = {};
108

11-
var streets = L.tileLayer(mapboxTileURL, {
12-
attribution: mapboxAttribution,
13-
tileSize: 512,
14-
zoomOffset: -1,
15-
id: 'mapbox/streets-v11',
16-
accessToken: mapboxToken
17-
}),
18-
outdoors = L.tileLayer(mapboxTileURL, {
19-
attribution: mapboxAttribution,
20-
tileSize: 512,
21-
zoomOffset: -1,
22-
id: 'mapbox/outdoors-v11',
23-
accessToken: mapboxToken
24-
}),
25-
satellite = L.tileLayer(mapboxTileURL, {
26-
attribution: mapboxAttribution,
27-
tileSize: 512,
28-
zoomOffset: -1,
29-
id: 'mapbox/satellite-streets-v11',
30-
accessToken: mapboxToken
31-
}),
32-
osm = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
33-
attribution: osmAttribution,
34-
}),
35-
osm_de = L.tileLayer('https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png', {
36-
attribution: osmAttribution,
37-
}),
38-
hiking = L.tileLayer('https://tile.waymarkedtrails.org/hiking/{z}/{x}/{y}.png', {
39-
attribution: waymarkedtrailsAttribution,
40-
}),
41-
bike = L.tileLayer('https://tile.waymarkedtrails.org/cycling/{z}/{x}/{y}.png', {
42-
attribution: waymarkedtrailsAttribution,
43-
}),
44-
small_components = L.tileLayer('https://tools.geofabrik.de/osmi/tiles/routing/{z}/{x}/{y}.png', {});
9+
// Load layers from config
10+
for (let layer of config.layers) {
11+
layers[layer.name] = L.tileLayer(layer.url, {
12+
attribution: layer.attribution
13+
});
14+
}
15+
16+
// Load overlays from config
17+
for (let overlay of config.overlays) {
18+
overlays[overlay.name] = L.tileLayer(overlay.url, {
19+
attribution: overlay.attribution
20+
});
21+
}
4522

4623
module.exports = {
4724
defaultState: {
48-
center: L.latLng(38.8995,-77.0269),
49-
zoom: 13,
25+
center: L.latLng(config.center[0], config.center[1]),
26+
zoom: config.zoom,
5027
waypoints: [],
51-
language: 'en',
28+
language: config.language,
5229
alternative: 0,
53-
layer: streets
54-
},
55-
services: [{
56-
label: 'Car (fastest)',
57-
path: 'https://router.project-osrm.org/route/v1'
58-
}],
59-
layer: [{
60-
'Mapbox Streets': streets,
61-
'Mapbox Outdoors': outdoors,
62-
'Mapbox Streets Satellite': satellite,
63-
'openstreetmap.org': osm,
64-
'openstreetmap.de.org': osm_de
65-
}],
66-
overlay: {
67-
'Hiking': hiking,
68-
'Bike': bike,
69-
'Small Components': small_components
30+
layer: layers[config.layers[0].name], // Set the base layer to the first layer in the list
31+
service: 0
7032
},
33+
services: config.services,
34+
layer: [layers],
35+
overlay: overlays,
7136
baselayer: {
72-
one: streets,
73-
two: outdoors,
74-
three: satellite,
75-
four: osm,
76-
five: osm_de
37+
one: layers[config.layers[0].name], // Set the base layer to the first layer in the list
7738
}
7839
};

0 commit comments

Comments
 (0)