# Import your application as:
# from wsgi import application
# Example:

# Import CherryPy
import os
import cherrypy

from django_sso_app.core.settings.backend import DJANGO_SSO_SERVER_PORT
from django_sso_app.backend.config.wsgi import application


def run(host="0.0.0.0", port=DJANGO_SSO_SERVER_PORT):
    os.environ.setdefault("DJANGO_SSO_APP_SERVER", "True")

    # Mount the application
    cherrypy.tree.graft(application, "/")

    # Unsubscribe the default server
    cherrypy.server.unsubscribe()

    # Instantiate a new server object
    server = cherrypy._cpserver.Server()

    # Configure the server object
    server.socket_host = host
    server.socket_port = port
    server.thread_pool = 30

    # For SSL Support
    # server.ssl_module            = 'pyopenssl'
    # server.ssl_certificate       = 'ssl/certificate.crt'
    # server.ssl_private_key       = 'ssl/private.key'
    # server.ssl_certificate_chain = 'ssl/bundle.crt'

    # Subscribe this server
    server.subscribe()

    # Example for a 2nd server (same steps as above):
    # Remember to use a different port

    # server2             = cherrypy._cpserver.Server()

    # server2.socket_host = "0.0.0.0"
    # server2.socket_port = 8081
    # server2.thread_pool = 30
    # server2.subscribe()

    # Start the server engine (Option 1 *and* 2)

    cherrypy.engine.start()
    cherrypy.engine.block()


if __name__ == '__main__':
    run()
