Ejemplo n.º 1
0
def configure_http_server(port):
    # Mount the application
    from kolibri.deployment.default.wsgi import application

    cherrypy.tree.graft(application, "/")

    # Mount static files
    cherrypy.tree.mount(
        cherrypy.tools.staticdir.handler(section="/",
                                         dir=settings.STATIC_ROOT),
        settings.STATIC_URL,
        config={
            "/": {
                "tools.gzip.on": True,
                "tools.gzip.mime_types": ["text/*", "application/javascript"],
            }
        },
    )

    # Mount media files
    cherrypy.tree.mount(
        cherrypy.tools.staticdir.handler(section="/", dir=settings.MEDIA_ROOT),
        settings.MEDIA_URL,
    )

    # Mount content files
    CONTENT_ROOT = "/" + paths.get_content_url(
        conf.OPTIONS["Deployment"]["URL_PATH_PREFIX"]).lstrip("/")
    content_dirs = [paths.get_content_dir_path()
                    ] + paths.get_content_fallback_paths()
    dispatcher = MultiStaticDispatcher(content_dirs)
    cherrypy.tree.mount(
        None,
        CONTENT_ROOT,
        config={
            "/": {
                "tools.caching.on": False,
                "request.dispatch": dispatcher
            }
        },
    )

    # Configure the server
    cherrypy.config.update({
        "server.socket_host":
        LISTEN_ADDRESS,
        "server.socket_port":
        port,
        "server.thread_pool":
        conf.OPTIONS["Server"]["CHERRYPY_THREAD_POOL"],
        "server.socket_timeout":
        conf.OPTIONS["Server"]["CHERRYPY_SOCKET_TIMEOUT"],
        "server.accepted_queue_size":
        conf.OPTIONS["Server"]["CHERRYPY_QUEUE_SIZE"],
        "server.accepted_queue_timeout":
        conf.OPTIONS["Server"]["CHERRYPY_QUEUE_TIMEOUT"],
    })
    # Subscribe this server
    cherrypy.server.subscribe()
Ejemplo n.º 2
0
def configure_http_server(port):
    # Mount the application
    from kolibri.deployment.default.wsgi import application

    cherrypy.tree.graft(application, "/")

    # Mount static files
    cherrypy.tree.mount(
        cherrypy.tools.staticdir.handler(section="/",
                                         dir=settings.STATIC_ROOT),
        settings.STATIC_URL,
        config={
            "/": {
                "tools.gzip.on": True,
                "tools.gzip.mime_types": ["text/*", "application/javascript"],
            }
        },
    )

    # Mount media files
    cherrypy.tree.mount(
        cherrypy.tools.staticdir.handler(section="/", dir=settings.MEDIA_ROOT),
        settings.MEDIA_URL,
    )

    # Mount content files
    content_files_handler = cherrypy.tools.staticdir.handler(
        section="/", dir=paths.get_content_dir_path())

    url_path_prefix = conf.OPTIONS["Deployment"]["URL_PATH_PREFIX"]

    cherrypy.tree.mount(
        content_files_handler,
        "/{}".format(paths.get_content_url(url_path_prefix).lstrip("/")),
        config={"/": {
            "tools.caching.on": False
        }},
    )

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

    # Configure the server
    server.socket_host = LISTEN_ADDRESS
    server.socket_port = port
    server.thread_pool = conf.OPTIONS["Server"]["CHERRYPY_THREAD_POOL"]
    server.socket_timeout = conf.OPTIONS["Server"]["CHERRYPY_SOCKET_TIMEOUT"]
    server.accepted_queue_size = conf.OPTIONS["Server"]["CHERRYPY_QUEUE_SIZE"]
    server.accepted_queue_timeout = conf.OPTIONS["Server"][
        "CHERRYPY_QUEUE_TIMEOUT"]

    # Subscribe this server
    server.subscribe()
Ejemplo n.º 3
0
def generate_wsgi_application():
    django_application = get_wsgi_application()
    base_content_path = "/" + paths.get_content_url(
        conf.OPTIONS["Deployment"]["URL_PATH_PREFIX"]).lstrip("/")
    content_dirs = [paths.get_content_dir_path()
                    ] + paths.get_content_fallback_paths()

    # Mount static files
    return DjangoWhiteNoise(
        django_application,
        static_prefix=settings.STATIC_URL,
        dynamic_locations=[(base_content_path, content_dir)
                           for content_dir in content_dirs] +
        [(settings.MEDIA_URL, settings.MEDIA_ROOT)],
    )
Ejemplo n.º 4
0
def generate_alt_wsgi_application():
    alt_content_path = "/" + paths.get_content_url(
        paths.zip_content_path_prefix()).lstrip("/")

    content_dirs = [paths.get_content_dir_path()
                    ] + paths.get_content_fallback_paths()

    content_static_path = os.path.join(
        os.path.dirname(kolibri.core.content.__file__), "static")

    # Mount static files
    return DynamicWhiteNoise(
        get_application(),
        dynamic_locations=[(alt_content_path, content_dir)
                           for content_dir in content_dirs] +
        [(paths.zip_content_static_root(), content_static_path)],
    )
def save_nginx_conf_include(static_root, nginx_conf=None):
    """
    Automatically writes the dynamic Nginx configuration include from Kolibri
    configuration.

    This function is called from within the DJANGO_SETTINGS_MODULE after it
    has defined STATIC_ROOT.

    The nginx configuration snippet is loaded with in the main Nginx
    configuration which contains::

        include /path/to/.kolibri/nginx.conf;

    Note that because we cannot load django.conf.settings from this module
    (because Django settings depend on Kolibri conf), we have a sort of
    circular issue with STATIC_ROOT. We cannot fetch settings.STATIC_ROOT
    unless we start writing the Nginx configuration elsewhere during the load
    order.
    """

    if nginx_conf is None:
        nginx_conf = os.path.join(KOLIBRI_HOME, "nginx.conf")

    with open(nginx_conf, 'w') as nginx_conf_file:
        configuration = (
            "# This file is maintained AUTOMATICALLY and will be overwritten\n"
            "#\n"
            "# Do not edit this file. If you are using the kolibri-server"
            "package,\n"
            "# please write custom configurations in /etc/kolibri/nginx.d/\n"
            "\n"
            "location {path_prefix}static {{\n"
            "    alias  {static_dir};\n"
            "}}\n"
            "location {path_prefix}content {{\n"
            "    alias  {content_dir}/;\n"
            "}}\n"
        ).format(static_dir=static_root,
                 content_dir=get_content_dir_path(),
                 path_prefix=path_prefix)
        nginx_conf_file.write(configuration)
Ejemplo n.º 6
0
# Patterns that we want to prefix because they need access to the current language
lang_prefixed_patterns = [
    url(r"^i18n/setlang/$", set_language, name="set_language"),
    url(r"^logout/$", logout_view, name="logout"),
    url(r"^redirectuser/$",
        RootURLRedirectView.as_view(),
        name="redirect_user"),
    url(r"^guestaccess/$", GuestRedirectView.as_view(), name="guest"),
    url(r"^unsupported/$",
        UnsupportedBrowserView.as_view(),
        name="unsupported"),
    url(r"^$", RootURLRedirectView.as_view(), name="root_redirect"),
]

core_urlpatterns = [
    url(r"^api/", include("kolibri.core.api_urls")),
    url(r"", include(i18n_patterns(lang_prefixed_patterns))),
    url(r"", include("kolibri.core.content.urls")),
    url(r"^status/", StatusCheckView.as_view(), name="status_check"),
]

urlpatterns = [url(r"", include(core_urlpatterns, namespace="core"))]

urlpatterns += plugin_urls()

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += static(paths.get_content_url("/"),
                      document_root=paths.get_content_dir_path())
Ejemplo n.º 7
0
def configure_http_server(port):
    # Mount the application
    from kolibri.deployment.default.wsgi import application

    whitenoise_settings = {
        "static_root": settings.STATIC_ROOT,
        "static_prefix": settings.STATIC_URL,
        # Use 1 day as the default cache time for static assets
        "max_age": 24 * 60 * 60,
        # Add a test for any file name that contains a semantic version number
        # or a 32 digit number (assumed to be a file hash)
        # these files will be cached indefinitely
        "immutable_file_test":
        r"((0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)|[a-f0-9]{32})",
        "autorefresh": getattr(settings, "DEVELOPER_MODE", False),
    }

    # Mount static files
    application = DjangoWhiteNoise(application, **whitenoise_settings)

    cherrypy.tree.graft(application, "/")

    # Mount media files
    cherrypy.tree.mount(
        cherrypy.tools.staticdir.handler(section="/", dir=settings.MEDIA_ROOT),
        settings.MEDIA_URL,
    )

    # Mount content files
    CONTENT_ROOT = "/" + paths.get_content_url(
        conf.OPTIONS["Deployment"]["URL_PATH_PREFIX"]).lstrip("/")
    content_dirs = [paths.get_content_dir_path()
                    ] + paths.get_content_fallback_paths()
    dispatcher = MultiStaticDispatcher(content_dirs)
    content_handler = cherrypy.tree.mount(
        None,
        CONTENT_ROOT,
        config={
            "/": {
                "tools.caching.on": False,
                "request.dispatch": dispatcher
            }
        },
    )

    cherrypy_server_config = {
        "server.socket_host":
        LISTEN_ADDRESS,
        "server.socket_port":
        port,
        "server.thread_pool":
        conf.OPTIONS["Server"]["CHERRYPY_THREAD_POOL"],
        "server.socket_timeout":
        conf.OPTIONS["Server"]["CHERRYPY_SOCKET_TIMEOUT"],
        "server.accepted_queue_size":
        conf.OPTIONS["Server"]["CHERRYPY_QUEUE_SIZE"],
        "server.accepted_queue_timeout":
        conf.OPTIONS["Server"]["CHERRYPY_QUEUE_TIMEOUT"],
    }

    # Configure the server
    cherrypy.config.update(cherrypy_server_config)

    alt_port_addr = (
        LISTEN_ADDRESS,
        conf.OPTIONS["Deployment"]["ZIP_CONTENT_PORT"],
    )

    # Mount static files
    alt_port_app = wsgi.PathInfoDispatcher({
        "/": get_application(),
        CONTENT_ROOT: content_handler
    })
    alt_port_app = DjangoWhiteNoise(alt_port_app, **whitenoise_settings)

    alt_port_server = ServerAdapter(
        cherrypy.engine,
        wsgi.Server(
            alt_port_addr,
            alt_port_app,
            numthreads=conf.OPTIONS["Server"]["CHERRYPY_THREAD_POOL"],
            request_queue_size=conf.OPTIONS["Server"]["CHERRYPY_QUEUE_SIZE"],
            timeout=conf.OPTIONS["Server"]["CHERRYPY_SOCKET_TIMEOUT"],
            accepted_queue_size=conf.OPTIONS["Server"]["CHERRYPY_QUEUE_SIZE"],
            accepted_queue_timeout=conf.OPTIONS["Server"]
            ["CHERRYPY_QUEUE_TIMEOUT"],
        ),
        alt_port_addr,
    )
    # Subscribe these servers
    cherrypy.server.subscribe()
    alt_port_server.subscribe()
Ejemplo n.º 8
0
lang_prefixed_patterns = [
    url(r"^i18n/setlang/$", set_language, name="set_language"),
    url(r"^logout/$", logout_view, name="logout"),
    url(r"^redirectuser/$",
        RootURLRedirectView.as_view(),
        name="redirect_user"),
    url(r"^guestaccess/$", GuestRedirectView.as_view(), name="guest"),
    url(r"^unsupported/$",
        UnsupportedBrowserView.as_view(),
        name="unsupported"),
    url(r"^$", RootURLRedirectView.as_view(), name="root_redirect"),
]

core_urlpatterns = [
    url(r"^api/", include("kolibri.core.api_urls")),
    url(r"", include(i18n_patterns(lang_prefixed_patterns))),
    url(r"", include("kolibri.core.content.urls")),
    url(r"^status/", StatusCheckView.as_view(), name="status_check"),
]

urlpatterns = [url(r"", include(core_urlpatterns, namespace="core"))]

urlpatterns += plugin_urls()

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

content_dirs = [paths.get_content_dir_path()
                ] + paths.get_content_fallback_paths()
urlpatterns += static(paths.get_content_url("/"),
                      view=static_serve_with_fallbacks(content_dirs))
Ejemplo n.º 9
0
def run_server(port):

    # Mount the application
    from kolibri.deployment.default.wsgi import application

    cherrypy.tree.graft(application, "/")

    cherrypy.config.update({
        "environment": "production",
        "tools.expires.on": True,
        "tools.expires.secs": 31536000,
        "tools.caching.on": True,
        "tools.caching.maxobj_size": 2000000,
        "tools.caching.maxsize": calculate_cache_size(),
        "log.screen": False,
        "log.access_file": "",
        "log.error_file": "",
    })
    cherrypy.engine.unsubscribe("graceful", cherrypy.log.reopen_files)

    # Mount static files
    cherrypy.tree.mount(
        cherrypy.tools.staticdir.handler(section="/",
                                         dir=settings.STATIC_ROOT),
        settings.STATIC_URL,
        config={
            "/": {
                "tools.gzip.on": True,
                "tools.gzip.mime_types": ["text/*", "application/javascript"],
            }
        },
    )

    # Mount media files
    cherrypy.tree.mount(
        cherrypy.tools.staticdir.handler(section="/", dir=settings.MEDIA_ROOT),
        settings.MEDIA_URL,
    )

    # Mount content files
    content_files_handler = cherrypy.tools.staticdir.handler(
        section="/", dir=paths.get_content_dir_path())

    url_path_prefix = conf.OPTIONS["Deployment"]["URL_PATH_PREFIX"]

    cherrypy.tree.mount(
        content_files_handler,
        "/{}".format(paths.get_content_url(url_path_prefix).lstrip("/")),
        config={"/": {
            "tools.caching.on": False
        }},
    )

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

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

    # Configure the server
    server.socket_host = LISTEN_ADDRESS
    server.socket_port = port
    server.thread_pool = conf.OPTIONS["Server"]["CHERRYPY_THREAD_POOL"]
    server.socket_timeout = conf.OPTIONS["Server"]["CHERRYPY_SOCKET_TIMEOUT"]
    server.accepted_queue_size = conf.OPTIONS["Server"]["CHERRYPY_QUEUE_SIZE"]
    server.accepted_queue_timeout = conf.OPTIONS["Server"][
        "CHERRYPY_QUEUE_TIMEOUT"]

    # Subscribe this server
    server.subscribe()

    # Start the server engine (Option 1 *and* 2)
    unlock_after_vacuum()  # don't start the server until vacuum finishes
    cherrypy.engine.start()
    cherrypy.engine.block()
Ejemplo n.º 10
0
def get_content_share_dir_path():
    """
    Returns the path to the directory where XDG files, like .desktop launchers
    and AppData, are located. By default, this is $KOLIBRI_HOME/content/xdg/share.
    """
    return os.path.join(get_content_dir_path(), "xdg", "share")