def startWebServer(options, config): # import pdb; pdb.set_trace() # Extract properties from config log_dir = str(config["configuration"]["log_dir"]) content = str(config["configuration"]["content"]) # twisted expects byte for uris. endpoint = str(config["configuration"]["endpoint"]).encode('utf-8') host = str(config["configuration"]["host"]) port = int(config["configuration"]["port"]) sanitize = config["configuration"]["sanitize"] # Setup logging logFileName = log_dir + os.sep + "launcherLog.log" formatting = '%(asctime)s:%(levelname)s:%(name)s:%(message)s' logging.basicConfig(level=logging.DEBUG, filename=logFileName, filemode='w', format=formatting) observer = log.PythonLoggingObserver() observer.start() if options.debug: console = logging.StreamHandler(sys.stdout) console.setLevel(logging.INFO) formatter = logging.Formatter(formatting) console.setFormatter(formatter) logging.getLogger('').addHandler(console) # Initialize web resource web_resource = File(content) if (len(content) > 0) else resource.Resource() # Attach launcher web_resource.putChild(endpoint, LauncherResource(options, config)) # Check if launcher should act as a file upload server as well if "upload_dir" in config["configuration"]: from upload import UploadPage updir = replaceVariables(config['configuration']['upload_dir'], [config['properties']], sanitize) uploadResource = UploadPage(updir) web_resource.putChild("upload", uploadResource) site = server.Site(web_resource) reactor.listenTCP(port, site, interface=host) reactor.run()
def start_webserver(options, protocol=wamp.ServerProtocol, disableLogging=False): """ Starts the web-server with the given protocol. Options must be an object with the following members: options.host : the interface for the web-server to listen on options.port : port number for the web-server to listen on options.timeout : timeout for reaping process on idle in seconds options.content : root for web-pages to serve. """ from twisted.internet import reactor from twisted.web.server import Site from twisted.web.static import File import sys if not disableLogging: log.startLogging(sys.stdout) contextFactory = None use_SSL = False if options.sslKey and options.sslCert: use_SSL = True wsProtocol = "wss" from twisted.internet import ssl contextFactory = ssl.DefaultOpenSSLContextFactory( options.sslKey, options.sslCert) else: wsProtocol = "ws" # setup the server-factory wampFactory = wamp.ReapingWampServerFactory( "%s://%s:%d" % (wsProtocol, options.host, options.port), options.debug, options.timeout) wampFactory.protocol = protocol # Do we serve static content or just websocket ? if len(options.content) == 0: # Only WebSocket listenWS(wampFactory, contextFactory) else: # Static HTTP + WebSocket wsResource = WebSocketResource(wampFactory) root = File(options.content) root.putChild("ws", wsResource) if options.uploadPath != None : from upload import UploadPage uploadResource = UploadPage(options.uploadPath) root.putChild("upload", uploadResource) site = Site(root) if use_SSL: reactor.listenSSL(options.port, site, contextFactory) else: reactor.listenTCP(options.port, site) # Work around to force the output buffer to be flushed # This allow the process launcher to parse the output and # wait for "Start factory" to know that the WebServer # is running. if options.forceFlush : for i in range(200): log.msg("+"*80, logLevel=logging.CRITICAL) # Give test client a chance to initialize a thread for itself # testing.initialize(opts=options) # Start the factory wampFactory.startFactory() # Initialize testing: checks if we're doing a test and sets it up testing.initialize(options, reactor) # Start the reactor if options.nosignalhandlers: reactor.run(installSignalHandlers=0) else: reactor.run() # Stope the factory wampFactory.stopFactory() # Give the testing module a chance to finalize, if necessary testing.finalize()
def start_webserver(options, protocol=vtk_wamp.ServerProtocol, disableLogging=False): """ Starts the web-server with the given protocol. Options must be an object with the following members: options.host : the interface for the web-server to listen on options.port : port number for the web-server to listen on options.timeout : timeout for reaping process on idle in seconds options.content : root for web-pages to serve. """ from twisted.internet import reactor from twisted.web.server import Site from twisted.web.static import File import sys if not disableLogging: log.startLogging(sys.stdout) contextFactory = None use_SSL = False if options.sslKey and options.sslCert: use_SSL = True wsProtocol = "wss" from twisted.internet import ssl contextFactory = ssl.DefaultOpenSSLContextFactory( options.sslKey, options.sslCert) else: wsProtocol = "ws" # Create WAMP router factory from autobahn.twisted.wamp import RouterFactory router_factory = RouterFactory() # create a user DB authdb = vtk_wamp.AuthDb() # create a WAMP router session factory from autobahn.twisted.wamp import RouterSessionFactory session_factory = RouterSessionFactory(router_factory) session_factory.session = vtk_wamp.CustomWampCraRouterSession session_factory.authdb = authdb # Create ApplicationSession and register protocols appSession = protocol(types.ComponentConfig(realm="vtkweb")) appSession.setAuthDB(authdb) session_factory.add(appSession) # create a WAMP-over-WebSocket transport server factory transport_factory = vtk_wamp.TimeoutWampWebSocketServerFactory(session_factory, \ url = "%s://%s:%d" % (wsProtocol, options.host, options.port), \ debug = options.debug, \ debug_wamp = options.debug, \ timeout = options.timeout ) root = Resource() # Do we serve static content or just websocket ? if len(options.content) > 0: # Static HTTP + WebSocket root = File(options.content) # Handle possibly complex ws endpoint if not options.nows: wsResource = WebSocketResource(transport_factory) handle_complex_resource_path(options.ws, root, wsResource) # Handle possibly complex lp endpoint if not options.nolp: lpResource = WampLongPollResource(session_factory) handle_complex_resource_path(options.lp, root, lpResource) if options.uploadPath != None: from upload import UploadPage uploadResource = UploadPage(options.uploadPath) root.putChild("upload", uploadResource) site = Site(root) if use_SSL: reactor.listenSSL(options.port, site, contextFactory) else: reactor.listenTCP(options.port, site) # Work around to force the output buffer to be flushed # This allow the process launcher to parse the output and # wait for "Start factory" to know that the WebServer # is running. if options.forceFlush: for i in range(200): log.msg("+" * 80, logLevel=logging.CRITICAL) # Initialize testing: checks if we're doing a test and sets it up testing.initialize(options, reactor, stop_webserver) # Start the reactor if options.nosignalhandlers: reactor.run(installSignalHandlers=0) else: reactor.run() # Give the testing module a chance to finalize, if necessary testing.finalize()