From a11ae01d4e63b4b9a670b24ba9d4377815944b81 Mon Sep 17 00:00:00 2001
From: Martijn Vermaat <martijn@vermaat.name>
Date: Mon, 7 Sep 2015 18:31:29 +0200
Subject: [PATCH] Fix service location in SOAP WSDL document

By default, the first request to the SOAP service will trigger a
build of the WSDL document, using the context (service location)
from that request. For example, if the first request is on
`http://localhost/` and subsequent requests are on
`https://mutalyzer.nl/services/`, they will not have a valid WSDL
document.

This is actually what we do on our production infrastructure,
where the service is tested (on localhost) after it has been
started.

The fix is to force a build of the WSDL document and specifying
the location to use.

http://spyne.io/docs/2.10/reference/server.html#spyne.server.wsgi.WsgiApplication
---
 doc/config.rst                        |  8 ++++----
 mutalyzer/config/default_settings.py  |  4 ++--
 mutalyzer/entrypoints/service_soap.py | 16 ++++++++++++++++
 mutalyzer/website/views.py            |  7 ++++---
 4 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/doc/config.rst b/doc/config.rst
index 43535b13..cd8ebf97 100644
--- a/doc/config.rst
+++ b/doc/config.rst
@@ -185,10 +185,10 @@ REVERSE_PROXIED
 .. _config-soap-wsdl-url:
 
 SOAP_WSDL_URL
-  URL to the SOAP webservice WSDL document. Used for linking to it from the
-  documentation page on the website.
+  URL to the SOAP webservice WSDL document. Used to build the WSDL document
+  and for linking to it from the documentation page on the website.
 
-  `Default value:` ``https://mutalyzer.nl/services/?wsdl``
+  `Default value:` `None`
 
 .. _config-json-root-url:
 
@@ -196,7 +196,7 @@ JSON_ROOT_URL
   URL to the HTTP/RPC+JSON webservice root (without trailing slash). Used for
   linking to it from the documentation page on the website.
 
-  `Default value:` ``https://mutalyzer.nl/json``
+  `Default value:` `None`
 
 
 Piwik settings
diff --git a/mutalyzer/config/default_settings.py b/mutalyzer/config/default_settings.py
index c01d47a5..84c2373e 100644
--- a/mutalyzer/config/default_settings.py
+++ b/mutalyzer/config/default_settings.py
@@ -72,8 +72,8 @@ PROTEIN_LINK_EXPIRATION = 60 * 60 * 24 * 30
 # seconds).
 NEGATIVE_PROTEIN_LINK_EXPIRATION = 60 * 60 * 24 * 5
 
-# URL to the SOAP webservice WSDL document. Used for linking to it from the
-# documentation page on the website.
+# URL to the SOAP webservice WSDL document. Used to build the WSDL document
+# and for linking to it from the documentation page on the website.
 SOAP_WSDL_URL = 'https://mutalyzer.nl/services/?wsdl'
 
 # URL to the HTTP/RPC+JSON webservice root (without trailing slash). Used for
diff --git a/mutalyzer/entrypoints/service_soap.py b/mutalyzer/entrypoints/service_soap.py
index 8179faa3..40da7294 100644
--- a/mutalyzer/entrypoints/service_soap.py
+++ b/mutalyzer/entrypoints/service_soap.py
@@ -40,6 +40,22 @@ logging.getLogger('spyne.protocol.xml').setLevel(log_level)
 
 #: WSGI application instance.
 application = WsgiApplication(soap.application)
+
+# By default, the first request will trigger a build of the WSDL document,
+# using the context (service location) from that request. For example, if the
+# first request is on `http://localhost/` and subsequent requests are on
+# `https://mutalyzer.nl/services/`, they will not have a valid WSDL document.
+#
+# This is actually what we do on our production infrastructure, where the
+# service is tested (on localhost) after it has been started.
+#
+# The fix is to force a build of the WSDL document and specifying the location
+# to use.
+#
+# http://spyne.io/docs/2.10/reference/server.html#spyne.server.wsgi.WsgiApplication
+if settings.SOAP_WSDL_URL:
+    application.doc.wsdl11.build_interface_document(settings.SOAP_WSDL_URL)
+
 if settings.REVERSE_PROXIED:
     application = _ReverseProxied(application)
 
diff --git a/mutalyzer/website/views.py b/mutalyzer/website/views.py
index bc0a339e..db179140 100644
--- a/mutalyzer/website/views.py
+++ b/mutalyzer/website/views.py
@@ -50,8 +50,8 @@ def global_context():
         'release'             : mutalyzer.__version_info__[-1] != 'dev',
         'copyright_years'     : mutalyzer.COPYRIGHT_YEARS,
         'contact_email'       : settings.EMAIL,
-        'soap_wsdl_url'       : settings.SOAP_WSDL_URL,
-        'json_root_url'       : settings.JSON_ROOT_URL,
+        'soap_wsdl_url'       : settings.SOAP_WSDL_URL or 'https://mutalyzer.nl/services/?wsdl',
+        'json_root_url'       : settings.JSON_ROOT_URL or 'https://mutalyzer.nl/json',
         'piwik'               : settings.PIWIK,
         'piwik_base_url'      : settings.PIWIK_BASE_URL,
         'piwik_site_id'       : settings.PIWIK_SITE_ID,
@@ -144,7 +144,8 @@ def soap_api():
            .documentation { white-space: pre-line; }
     """
     soap_server = HttpBase(soap.application)
-    soap_server.doc.wsdl11.build_interface_document(settings.SOAP_WSDL_URL)
+    soap_server.doc.wsdl11.build_interface_document(
+        settings.SOAP_WSDL_URL or 'https://mutalyzer.nl/services/?wsdl')
     wsdl_string = soap_server.doc.wsdl11.get_interface_document()
 
     xsl_file = os.path.join(
-- 
GitLab