import cherrypy
import mako
import mimetypes
import os
import girder.events
from girder import constants, logprint, __version__, logStdoutStderr, _setupCache
from girder.models.setting import Setting
from girder import plugin
from girder.settings import SettingKey
from girder.utility import config
from girder.constants import ServerMode
from . import webroot
with open(os.path.join(os.path.dirname(__file__), 'error.mako')) as f:
_errorTemplate = f.read()
def _errorDefault(status, message, *args, **kwargs):
"""
This is used to render error pages outside of the normal Girder app, such as
404's. This overrides the default cherrypy error pages.
"""
return mako.template.Template(_errorTemplate).render(status=status, message=message)
def getApiRoot():
return config.getConfig()['server']['api_root']
def getStaticPublicPath():
return config.getConfig()['server']['static_public_path']
[docs]def loadRouteTable(reconcileRoutes=False):
"""
Retrieves the route table from Girder and reconciles the state of it with the current
application state.
Reconciliation ensures that every enabled plugin has a route by assigning default routes for
plugins that have none, such as newly-enabled plugins.
:returns: The non empty routes (as a dict of name -> route) to be mounted by CherryPy
during Girder's setup phase.
"""
pluginWebroots = plugin.getPluginWebroots()
routeTable = Setting().get(SettingKey.ROUTE_TABLE)
def reconcileRouteTable(routeTable):
hasChanged = False
# Migration for the removed static root setting
if 'core_static_root' in routeTable:
del routeTable['core_static_root']
hasChanged = True
for name in pluginWebroots.keys():
if name not in routeTable:
routeTable[name] = os.path.join('/', name)
hasChanged = True
if hasChanged:
Setting().set(SettingKey.ROUTE_TABLE, routeTable)
return routeTable
if reconcileRoutes:
routeTable = reconcileRouteTable(routeTable)
return {name: route for (name, route) in routeTable.items() if route}
[docs]def setup(mode=None, plugins=None, curConfig=None):
"""
Configure and mount the Girder server and plugins under the
appropriate routes.
See ROUTE_TABLE setting.
:param mode: The server mode to start in.
:type mode: string
:param plugins: List of plugins to enable.
:param curConfig: The config object to update.
"""
logStdoutStderr()
pluginWebroots = plugin.getPluginWebroots()
girderWebroot, appconf = configureServer(mode, plugins, curConfig)
routeTable = loadRouteTable(reconcileRoutes=True)
# Mount Girder
application = cherrypy.tree.mount(
girderWebroot, str(routeTable[constants.GIRDER_ROUTE_ID]), appconf)
# Mount static files
cherrypy.tree.mount(None, '/static',
{'/':
{'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(constants.STATIC_ROOT_DIR),
'request.show_tracebacks': appconf['/']['request.show_tracebacks'],
'response.headers.server': 'Girder %s' % __version__,
'error_page.default': _errorDefault}})
if routeTable[constants.GIRDER_ROUTE_ID] != '/':
# Mount API (special case)
# The API is always mounted at /api AND at api relative to the Girder root
cherrypy.tree.mount(girderWebroot.api, '/api', appconf)
# Mount everything else in the routeTable
for name, route in routeTable.items():
if name != constants.GIRDER_ROUTE_ID and name in pluginWebroots:
cherrypy.tree.mount(pluginWebroots[name], route, appconf)
return application
class _StaticFileRoute:
exposed = True
def __init__(self, path, contentType=None):
self.path = os.path.abspath(path)
self.contentType = contentType
def GET(self):
return cherrypy.lib.static.serve_file(self.path, content_type=self.contentType)
[docs]def staticFile(path, contentType=None):
"""
Helper function to serve a static file. This should be bound as the route
object, i.e. info['serverRoot'].route_name = staticFile('...')
:param path: The path of the static file to serve from this route.
:type path: str
:param contentType: The MIME type of the static file. If set to None, the
content type will be guessed by the file extension of
the 'path' argument.
"""
return _StaticFileRoute(path, contentType)