Skip to content

Commit

Permalink
Use self reported hostname and port when building URLs
Browse files Browse the repository at this point in the history
In efforts to decrease the complexity of server configuration (see #48)
and to increase flexibility with DNS or IP addresses, we decided to
experiment with letting gunicorn/flask return with the URL root that was
used to make the request.
  • Loading branch information
tkuester committed Nov 22, 2021
1 parent e45f801 commit 6e5c4c3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
6 changes: 0 additions & 6 deletions taky/dps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@
load_config(os.environ.get("TAKY_CONFIG"))

application = app = Flask(__name__)
app.config["HOSTNAME"] = config.get("taky", "hostname")
app.config["NODEID"] = config.get("taky", "node_id")
app.config["UPLOAD_PATH"] = os.path.realpath(config.get("dp_server", "upload_path"))

app.config["PUBLIC_IP"] = config.get("taky", "public_ip")
app.config["COT_PORT"] = config.getint("cot_server", "port")
if config.getboolean("ssl", "enabled"):
app.config["PROTO"] = "https://"
app.config["COT_CONN_STR"] = 'ssl:{app.config["HOSTNAME"]}:{app.config["COT_PORT"]}'
app.config["DPS_PORT"] = 8443
else:
app.config["PROTO"] = "http://"
app.config["COT_CONN_STR"] = 'tcp:{app.config["HOSTNAME"]}:{app.config["COT_PORT"]}'
app.config["DPS_PORT"] = 8080
# TODO: Configurable?

from taky.dps import views # pylint: disable=wrong-import-position
6 changes: 1 addition & 5 deletions taky/dps/views/datapackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ def url_for(f_hash):
"""
Returns the URL for the given hash
"""
proto = app.config["PROTO"]
ip_addr = app.config["PUBLIC_IP"]
port = app.config["DPS_PORT"]

return f"{proto}{ip_addr}:{port}/Marti/sync/content?hash={f_hash}"
return f"{request.host_url}Marti/sync/content?hash={f_hash}"


def get_meta(f_hash=None, f_name=None):
Expand Down
6 changes: 5 additions & 1 deletion taky/dps/views/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from flask import request

from taky import __version__
from taky.dps import app

Expand All @@ -9,13 +11,15 @@ def marti_api_version():

@app.route("/Marti/api/version/config")
def marti_api_version_config():
hostname = request.host.split(":")

return {
"version": "2",
"type": "ServerConfig",
"data": {
"version": f"taky-{__version__}",
"api": "2",
"hostname": app.config["HOSTNAME"],
"hostname": hostname[0],
},
"nodeId": app.config["NODEID"],
}

0 comments on commit 6e5c4c3

Please sign in to comment.