Skip to content

Commit 71d5c04

Browse files
Read the database and listen settings from config.json with env overrides
server.py loads its connection (DB_HOST/DB_PORT/DB_NAME/DB_USER/DB_PASS) and listen settings (API_HOST/API_PORT) from config.json, with environment variables of the same name taking precedence, instead of hard-coded values.
1 parent 575c7a1 commit 71d5c04

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

‎server.py‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,27 @@
2929
from resource.temporal_geom_query.distance import get_distance
3030
from resource.temporal_geom_query.velocity import get_velocity
3131
from resource.temporal_geom_query.acceleration import get_acceleration
32+
import json
33+
import os
3234
import psycopg2
3335

34-
hostName = "localhost"
35-
serverPort = 8080
36-
host = 'localhost'
37-
port = 25431
38-
db = 'postgres'
39-
user = 'postgres'
40-
password = 'mysecretpassword'
36+
# Connection and listen settings come from config.json (next to this file);
37+
# environment variables of the same name override individual entries.
38+
with open(os.path.join(os.path.dirname(__file__), "config.json")) as _f:
39+
_cfg = json.load(_f)
40+
41+
42+
def _setting(key, default):
43+
return os.environ.get(key, _cfg.get(key, default))
44+
45+
46+
hostName = _setting("API_HOST", "localhost")
47+
serverPort = int(_setting("API_PORT", 8080))
48+
host = _setting("DB_HOST", "localhost")
49+
port = int(_setting("DB_PORT", 5432))
50+
db = _setting("DB_NAME", "postgres")
51+
user = _setting("DB_USER", "postgres")
52+
password = _setting("DB_PASS", "")
4153

4254
connection = psycopg2.connect(
4355
host=host, port=port, database=db, user=user, password=password)

0 commit comments

Comments
 (0)