def run( self ): port = RESTConf.port() if self.__sslops: url = "https://0.0.0.0:%s" % port else: url = "http://0.0.0.0:%s" % port gLogger.always( "Starting REST server on %s" % url ) autoreload.add_reload_hook( self.__reloadAppCB ) ioloop.IOLoop.instance().start()
def get( self ): if not self.__tmpDir: base = os.path.join( RESTConf.getWorkDir(), "tmp" ) if not os.path.isdir( base ): try: os.makedirs( base ) except Exception, e: gLogger.exception( "Cannot create work dir %s: %s" % ( base, e) ) raise self.__tmpDir = tempfile.mkdtemp( dir = base )
def bootstrap( self ): gLogger.always( "\n === Bootstrapping REST Server === \n" ) ol = ObjectLoader( [ 'DIRAC', 'RESTDIRAC' ] ) result = ol.getObjects( "RESTSystem.API", parentClass = RESTHandler, recurse = True ) if not result[ 'OK' ]: return result self.__handlers = result[ 'Value' ] if not self.__handlers: return S_ERROR( "No handlers found" ) self.__routes = [ ( self.__handlers[ k ].getRoute(), self.__handlers[k] ) for k in self.__handlers if self.__handlers[ k ].getRoute() ] gLogger.info( "Routes found:" ) for t in sorted( self.__routes ): gLogger.info( " - %s : %s" % ( t[0], t[1].__name__ ) ) balancer = RESTConf.balancer() kw = dict( debug = RESTConf.debug(), log_function = self._logRequest ) if balancer and RESTConf.numProcesses not in ( 0, 1 ): process.fork_processes( RESTConf.numProcesses(), max_restarts = 0 ) kw[ 'debug' ] = False if kw[ 'debug' ]: gLogger.always( "Starting in debug mode" ) self.__app = web.Application( self.__routes, **kw ) port = RESTConf.port() if balancer: gLogger.notice( "Configuring REST HTTP service for balancer %s on port %s" % ( balancer, port ) ) self.__sslops = False else: gLogger.notice( "Configuring REST HTTPS service on port %s" % port ) self.__sslops = dict( certfile = RESTConf.cert(), keyfile = RESTConf.key(), cert_reqs = ssl.CERT_OPTIONAL, ca_certs = RESTConf.generateCAFile() ) self.__httpSrv = httpserver.HTTPServer( self.__app, ssl_options = self.__sslops ) self.__httpSrv.listen( port ) return S_OK()
localCfg.setConfigurationForWeb( "REST" ) localCfg.addMandatoryEntry( "/DIRAC/Setup" ) localCfg.addDefaultEntry( "/DIRAC/Security/UseServerCertificate", "yes" ) localCfg.addDefaultEntry( "LogLevel", "INFO" ) localCfg.addDefaultEntry( "LogColor", True ) resultDict = localCfg.loadUserData() if not resultDict[ 'OK' ]: gLogger.initialize( serverName, "/" ) gLogger.error( "There were errors when loading configuration", resultDict[ 'Message' ] ) sys.exit( 1 ) from RESTDIRAC.ConfigurationSystem.Client.Helpers import RESTConf from RESTDIRAC.RESTSystem.private.RESTApp import RESTApp result = RESTConf.isOK() if not result[ 'OK' ]: gLogger.fatal( result[ 'Message' ] ) sys.exit(1) restApp = RESTApp() result = restApp.bootstrap() if not result[ 'OK' ]: gLogger.fatal( result[ 'Message' ] ) sys.exit(1) restApp.run()