def __init__( self ): self.__routes = Routes( Conf.rootURL() ) self.__servers = {} self.log = gLogger.getSubLogger( "Web" )
class App( object ): def __init__( self ): self.__routes = Routes( Conf.rootURL() ) self.__servers = {} self.log = gLogger.getSubLogger( "Web" ) def _logRequest( self, handler ): status = handler.get_status() if status < 400: logm = self.log.notice elif status < 500: logm = self.log.warn else: logm = self.log.error request_time = 1000.0 * handler.request.request_time() logm( "%d %s %.2fms" % ( status, handler._request_summary(), request_time ) ) def __reloadAppCB( self ): gLogger.notice( "\n !!!!!! Reloading web app...\n" ) def bootstrap( self ): """ Configure and create web app """ self.log.always( "\n ====== Starting DIRAC web app ====== \n" ) debug = Conf.debug() if debug: self.log.info( "Configuring in debug mode..." ) #Calculating routes result = self.__routes.getRoutes() if not result[ 'OK' ]: return result routes = result[ 'Value' ] #Create the app tLoader = TemplateLoader( self.__routes.getPaths( "template" ) ) kw = dict( debug = debug, template_loader = tLoader, cookie_secret = Conf.cookieSecret(), log_function = self._logRequest ) #Check processes if we're under a load balancert if Conf.balancer() and Conf.numProcesses() not in ( 0, 1 ): tornado.process.fork_processes( Conf.numProcesses(), max_restarts=0 ) kw[ 'debug' ] = False #Configure tornado app self.__app = tornado.web.Application( routes, **kw ) self.log.notice( "Configuring HTTP on port %s" % ( Conf.HTTPPort() ) ) #Create the web servers srv = tornado.httpserver.HTTPServer( self.__app ) port = Conf.HTTPPort() srv.listen( port ) self.__servers[ ( 'http', port ) ] = srv if Conf.HTTPS(): self.log.notice( "Configuring HTTPS on port %s" % Conf.HTTPSPort() ) sslops = dict( certfile = Conf.HTTPSCert(), keyfile = Conf.HTTPSKey(), cert_reqs = ssl.CERT_OPTIONAL, ca_certs = Conf.generateCAFile() ) self.log.debug( " - %s" % "\n - ".join( [ "%s = %s" % ( k, sslops[k] ) for k in sslops ] ) ) srv = tornado.httpserver.HTTPServer( self.__app, ssl_options = sslops ) port = Conf.HTTPSPort() srv.listen( port ) self.__servers[ ( 'https', port ) ] = srv return result def run( self ): """ Start web servers """ bu = Conf.rootURL().strip( "/" ) urls = [] for proto, port in self.__servers: urls.append("%s://0.0.0.0:%s/%s/" % ( proto, port, bu ) ) self.log.always( "Listening on %s" % " and ".join( urls ) ) tornado.autoreload.add_reload_hook( self.__reloadAppCB ) tornado.ioloop.IOLoop.instance().start()