Skip to content

Commit 43ca458

Browse files
committed
Dockerize featureService
1 parent cd58818 commit 43ca458

File tree

8 files changed

+141
-156
lines changed

8 files changed

+141
-156
lines changed

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules/
2+
test/
3+
tools/
4+
.gitignore
5+
LICENSE
6+
README.md

Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM node:6.9
2+
3+
RUN apt-get update && apt-get -qq install -y postgresql-client curl gzip
4+
5+
WORKDIR /app
6+
ADD package.json /app/package.json
7+
RUN npm install
8+
9+
ADD server.js /app/server.js
10+
ADD config.js /app/config.js
11+
ADD controllers /app/controllers
12+
ADD services /app/services
13+
ADD ddl /app/ddl
14+
ADD docker-entrypoint.sh /app/docker-entrypoint.sh
15+
16+
CMD /app/docker-entrypoint.sh
17+
18+
ENV PORT=80
19+
EXPOSE 80
20+
21+
ENV FEATURES_DB_USER=""
22+
ENV FEATURES_DB_PASSWORD=""
23+
ENV FEATURES_DB_HOST=""
24+
ENV FEATURES_DB_PORT="5432"
25+
ENV FEATURES_DB_NAME="features"
26+
ENV FEATURES_DB_DUMP_URL="https://fortiscentral.blob.core.windows.net/locations/dump.fc.gz"

README.md

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,32 @@
11
# featureService #
22

3-
## Development setup ##
3+
## Setup ##
44

5-
### System dependencies ###
5+
In order to run this project on your machine, you will need to set up:
66

7-
In order to install on your local machine, you will need to install:
7+
- [Postgres on Azure](https://azure.microsoft.com/en-us/services/postgresql/)
8+
- [Docker](https://docs.docker.com/docker-for-windows/)
89

9-
- Postgres + postgis ([instructions for Ubuntu 16.04](http://www.gis-blog.com/how-to-install-postgis-2-3-on-ubuntu-16-04-lts/))
10-
- Node ([instructions for Ubuntu 16.04](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04))
11-
12-
### Database setup ###
13-
14-
Once the system dependencies are installed, go open a bash shell on the project directory and set up the database:
15-
16-
```sh
17-
cat << EOF | sudo -u postgres psql
18-
CREATE DATABASE features;
19-
CREATE USER frontend WITH login password 'your_password_here';
20-
CREATE USER ops WITH login password 'your_other_password_here';
21-
GRANT ops TO postgres;
22-
GRANT frontend TO postgres;
23-
EOF
24-
sudo -u postgres psql -d features < schema.sql
25-
```
26-
27-
### Application setup ###
28-
29-
First, make sure the environment variables for the service are set:
30-
31-
```sh
32-
export FEATURES_CONNECTION_STRING='postgres://frontend:[email protected]/features'
33-
export PORT=3035
34-
```
35-
36-
Now you're ready to install and start the service:
10+
Once the system dependencies are installed, you can run the project via Docker:
3711

3812
```sh
39-
npm install
40-
node server.js
13+
docker build -t featureservice .
14+
15+
docker run \
16+
-p 8080:80 \
17+
-e FEATURES_DB_USER="----CHANGEME----" \
18+
-e FEATURES_DB_PASSWORD="----CHANGEME----" \
19+
-e FEATURES_DB_HOST="----CHANGEME----" \
20+
-t featureservice
4121
```
4222

43-
## Production setup ##
23+
The first time that you run this command, it will take a while as your Postgres
24+
on Azure instance is getting populated with over 2GB of global geo-spatial
25+
features.
4426

45-
You can run the script `scripts/install.sh` to set up a production machine with the featureService and all its dependencies. The script will:
27+
After starting the service, you will be able to call the featureService, for
28+
example via the following requests:
4629

47-
- Install postgres and postgis
48-
- Populate the postgres features database
49-
- Install the featureService
50-
- Autostart the featureService on port 80
30+
- http://localhost:8080/features/name/bogota
31+
- http://localhost:8080/features/point/18.678/15.123
32+
- http://localhost:8080/features/bbox/12.3/22.3/12.4/22.4

ddl/indices.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE INDEX features_hull_index ON features USING gist (hull);
2+
CREATE INDEX features_name_lower_index ON features(lower(name));
3+
CREATE INDEX features_namespace_index ON features(lower(split_part(id, '-', 1)));
4+
CREATE INDEX features_layer_lower_index ON features(lower(layer));
5+
6+
CREATE INDEX visits_start_index ON visits (start);
7+
CREATE INDEX visits_userid_index ON visits (user_id);

schema.sql renamed to ddl/schema.sql

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ CREATE TABLE features
2323
CONSTRAINT enforce_srid_hull CHECK (st_srid(hull) = 4326)
2424
);
2525

26-
CREATE INDEX features_hull_index ON features USING gist (hull);
27-
CREATE INDEX features_name_lower_index ON features(lower(name));
28-
CREATE INDEX features_namespace_index ON features(lower(split_part(id, '-', 1)));
29-
CREATE INDEX features_layer_lower_index ON features(lower(layer));
30-
3126
GRANT SELECT, UPDATE, INSERT, DELETE ON features TO frontend;
3227

3328
------------------------------------------------------------------------------
@@ -50,6 +45,3 @@ CREATE TABLE visits
5045
);
5146

5247
GRANT SELECT, UPDATE, INSERT, DELETE ON visits TO frontend;
53-
54-
CREATE INDEX visits_start_index ON visits (start);
55-
CREATE INDEX visits_userid_index ON visits (user_id);

docker-entrypoint.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env bash
2+
3+
run_sql() {
4+
PGSSLMODE="require" \
5+
PGPASSWORD="${FEATURES_DB_PASSWORD}" \
6+
psql \
7+
--username="${FEATURES_DB_USER}" \
8+
--port="${FEATURES_DB_PORT}" \
9+
--host="${FEATURES_DB_HOST}" \
10+
--dbname="$1" \
11+
}
12+
13+
load_features_dump() {
14+
PGSSLMODE="require" \
15+
PGPASSWORD="${FEATURES_DB_PASSWORD}" \
16+
pg_restore \
17+
--username="${FEATURES_DB_USER}" \
18+
--port="${FEATURES_DB_PORT}" \
19+
--host="${FEATURES_DB_HOST}" \
20+
--dbname="${FEATURES_DB_NAME}" \
21+
--jobs="$(grep -c ^processor /proc/cpuinfo)" \
22+
"$1"
23+
}
24+
25+
features_database_exists() {
26+
echo "SELECT 'yes' FROM pg_database WHERE datname='${FEATURES_DB_NAME}';" \
27+
| run_sql 'postgres' \
28+
| grep -q 'yes'
29+
}
30+
31+
log() {
32+
echo "[$(date)] $1"
33+
}
34+
35+
fail() {
36+
echo "$1" >&2
37+
exit 1
38+
}
39+
40+
if [ -z "${FEATURES_DB_USER}" ]; then fail "Need to provide FEATURES_DB_USER environment variable"; fi
41+
if [ -z "${FEATURES_DB_PASSWORD}" ]; then fail "Need to provide FEATURES_DB_PASSWORD environment variable"; fi
42+
if [ -z "${FEATURES_DB_HOST}" ]; then fail "Need to provide FEATURES_DB_HOST environment variable"; fi
43+
44+
if ! features_database_exists; then
45+
dump_file="/tmp/db.fc.gz"
46+
47+
log "Setting up database..."
48+
echo "CREATE DATABASE ${FEATURES_DB_NAME};" | run_sql 'postgres'
49+
echo "CREATE USER ops WITH login password 'changeme';" | run_sql 'postgres'
50+
echo "CREATE USER frontend WITH login password 'changeme';" | run_sql 'postgres'
51+
echo "ALTER USER ops WITH password '${FEATURES_DB_PASSWORD}';" | run_sql 'postgres'
52+
echo "ALTER USER frontend WITH password '${FEATURES_DB_PASSWORD}';" | run_sql 'postgres'
53+
echo "GRANT ops TO ${FEATURES_DB_USER%@*};" | run_sql 'postgres'
54+
echo "GRANT frontend TO ${FEATURES_DB_USER%@*};" | run_sql 'postgres'
55+
log "...done, database is now set up"
56+
57+
log "Setting up schema..."
58+
< /app/ddl/schema.sql run_sql "${FEATURES_DB_NAME}"
59+
log "...done, schema is now set up"
60+
61+
log "Fetching database dump..."
62+
curl --silent "${FEATURES_DB_DUMP_URL}" > "${dump_file}"
63+
log "...done, database dump is now available"
64+
65+
log "Ingesting database dump..."
66+
load_features_dump "${dump_file}"
67+
rm "${dump_file}"
68+
log "...done, database dump is now ingested"
69+
70+
log "Setting up indices..."
71+
< /app/ddl/indices.sql run_sql "${FEATURES_DB_NAME}"
72+
log "...done, indices are now set up"
73+
74+
log "Improving query planner..."
75+
echo "ANALYZE;" | run_sql "${FEATURES_DB_NAME}"
76+
log "...done, query planner is now ready"
77+
fi
78+
79+
FEATURES_CONNECTION_STRING="postgres://frontend:${FEATURES_DB_PASSWORD}@${FEATURES_DB_HOST}:${FEATURES_DB_PORT}/${FEATURES_DB_NAME}?ssl=true" \
80+
npm start

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"uuid": "^3.0.1"
2727
},
2828
"scripts": {
29+
"start": "node server.js",
2930
"test": "PORT=3035 FEATURES_CONNECTION_STRING='postgres://frontend:[euro4sure]@127.0.0.1/geofeatures' mocha"
3031
},
3132
"devDependencies": {

scripts/install.sh

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

0 commit comments

Comments
 (0)