]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/server/tileserver-mapnik/run.sh
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / server / tileserver-mapnik / run.sh
1 #!/bin/bash
2 set -o errexit
3 set -o pipefail
4 set -o nounset
5
6 readonly DOMAINS=${DOMAINS}
7 readonly SOURCE_DATA_DIR=${SOURCE_DATA_DIR:-/data}
8 readonly DEST_DATA_DIR=${DEST_DATA_DIR:-/project}
9 readonly TESSERA_CONFIG="$DEST_DATA_DIR/config.json"
10
11 readonly PORT=${PORT:-80}
12 readonly CACHE_SIZE=${CACHE_SIZE:-10}
13 readonly SOURCE_CACHE_SIZE=${SOURCE_CACHE_SIZE:-10}
14
15 function serve_xray() {
16     local mbtiles_file=$1
17     exec bin/tessera.js "xray+mbtiles://$mbtiles_file" \
18         --PORT $PORT \
19         --cache-size $CACHE_SIZE \
20         --source-cache-size $SOURCE_CACHE_SIZE
21 }
22
23 function find_first_mbtiles() {
24     for mbtiles_file in "$SOURCE_DATA_DIR"/*.mbtiles; do
25         echo "${mbtiles_file}"
26         break
27     done
28 }
29
30 function find_first_tm2() {
31     for tm2project in "$SOURCE_DATA_DIR"/*.tm2/; do
32         echo "${tm2project}"
33         break
34     done
35 }
36
37 function tessera_config_entry() {
38     local tm2project=$1
39     local serve_dir=${tm2project%.tm2}
40     local serve_path=${serve_dir##*/}
41
42     echo "\"/${serve_path}\": {\"source\":\"tmstyle://${tm2project}\", \"domains\": \"${DOMAINS}\"}," >> "$TESSERA_CONFIG"
43     echo "Serving ${tm2project##*/} at $serve_path"
44 }
45
46 function create_tessera_config() {
47     local mbtiles_file=$1
48     rm -f "$TESSERA_CONFIG"
49     echo '{' >> "$TESSERA_CONFIG"
50     for tm2project in "$DEST_DATA_DIR"/*.tm2; do
51         tessera_config_entry "${tm2project}"
52     done
53
54     ## Remove trailing comma unless adding vector tile source (below)
55     #truncate --size=-2 "$TESSERA_CONFIG"
56     # Always serve the vector tile source
57     echo "\"/$(basename ${mbtiles_file%.*})\": {\"source\":\"mbtiles://${mbtiles_file}\", \"domains\": \"${DOMAINS}\"}" >> "$TESSERA_CONFIG"
58
59     echo '}' >> "$TESSERA_CONFIG"
60 }
61
62 function replace_sources() {
63     mbtiles_file=$1
64     local vectortiles_name=${mbtiles_file%.mbtiles}
65
66     for project_dir in "$SOURCE_DATA_DIR"/*.tm2; do
67         local project_name="${project_dir##*/}"
68         local project_config_file="${project_dir%%/}/project.yml"
69
70         if [ "${vectortiles_name##*/}" = "${project_name%.*}" ]; then
71           echo "Name collision. The mbtiles and tm2 project can not have the same name."
72           exit 403
73         fi
74
75         # project config will be copied to new folder because we
76         # modify the source configuration of the style and don't want
77         # that to effect the original file
78         dest_project_dir="${DEST_DATA_DIR%%/}/$project_name"
79         local dest_project_config_file="${dest_project_dir%%/}/project.yml"
80         cp -rf "$project_dir" "$dest_project_dir"
81
82         # replace external vector tile sources with mbtiles source
83         # this allows developing rapidyl with an external source and then use the
84         # mbtiles for dependency free deployment
85         echo "Associating $vectortiles_name with $project_name"
86         replace_expr="s|source: \".*\"|source: \"mbtiles://$mbtiles_file\"|g"
87         sed -e "$replace_expr" $project_config_file > $dest_project_config_file
88     done
89 }
90
91 function serve_config() {
92     exec bin/tessera.js -c "$TESSERA_CONFIG" \
93         --PORT "$PORT" \
94         --cache-size "$CACHE_SIZE" \
95         --source-cache-size "$SOURCE_CACHE_SIZE"
96 }
97
98 function serve() {
99     local mbtiles_file=$(find_first_mbtiles)
100     local tm2project=$(find_first_tm2)
101     echo "------- Find additional information on how to use this container under the following link: http://osm2vectortiles.org/docs/start"
102     if [ -f "$mbtiles_file" ]; then
103         echo "Using $mbtiles_file as vector tile source"
104         if [ -d "$tm2project" ]; then
105             replace_sources "$mbtiles_file"
106             create_tessera_config "$mbtiles_file"
107             serve_config
108         else
109             echo "The mbtiles file is now served with X-Ray styles"
110             serve_xray "$mbtiles_file"
111         fi
112     else
113         # Serve empty config
114         rm -f "$TESSERA_CONFIG"
115         echo '{}' >> "$TESSERA_CONFIG"
116         serve_config
117     fi
118 }
119
120 serve