def run(self): # Global as I can't work out a way to get it into PrinterStateConnection global printer global gcodeManager global userManager from tornado.wsgi import WSGIContainer from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop from tornado.web import Application, FallbackHandler # first initialize the settings singleton and make sure it uses given configfile and basedir if available self._initSettings(self._configfile, self._basedir) # then initialize logging self._initLogging(self._debug) logger = logging.getLogger(__name__) gcodeManager = gcodefiles.GcodeManager() printer = Printer(gcodeManager) if settings().getBoolean(["accessControl", "enabled"]): userManagerName = settings().get(["accessControl", "userManager"]) try: clazz = util.getClass(userManagerName) userManager = clazz() except AttributeError, e: logger.exception( "Could not instantiate user manager %s, will run with accessControl disabled!" % userManagerName)
def run(self): if not self._allowRoot: self._checkForRoot() global printer global gcodeManager global userManager global eventManager global loginManager global debug global wifiManager from tornado.wsgi import WSGIContainer from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop from tornado.web import Application, FallbackHandler debug = self._debug # first initialize the settings singleton and make sure it uses given configfile and basedir if available self._initSettings(self._configfile, self._basedir) # then initialize logging self._initLogging(self._debug) logger = logging.getLogger(__name__) logger.info("Starting OctoPrint %s" % DISPLAY_VERSION) eventManager = events.eventManager() gcodeManager = gcodefiles.GcodeManager() printer = Printer(gcodeManager) wifiManager = wifi.WifiManager(printer) # configure timelapse octoprint.timelapse.configureTimelapse() # setup command triggers events.CommandTrigger(printer) if self._debug: events.DebugEventListener() if settings().getBoolean(["accessControl", "enabled"]): userManagerName = settings().get(["accessControl", "userManager"]) try: clazz = util.getClass(userManagerName) userManager = clazz() except AttributeError, e: logger.exception( "Could not instantiate user manager %s, will run with accessControl disabled!" % userManagerName)
class Server(): def __init__(self, configfile=None, basedir=None, host="0.0.0.0", port=5000, debug=False, allowRoot=False, logConf=None): self._configfile = configfile self._basedir = basedir self._host = host self._port = port self._debug = debug self._allowRoot = allowRoot self._logConf = logConf def run(self): if not self._allowRoot: self._checkForRoot() global printer global gcodeManager global userManager global eventManager global loginManager global networkManager global debug global softwareManager global VERSION from tornado.wsgi import WSGIContainer from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop from tornado.web import Application, FallbackHandler debug = self._debug # first initialize the settings singleton and make sure it uses given configfile and basedir if available self._initSettings(self._configfile, self._basedir) s = settings() # then initialize logging self._initLogging(self._debug, self._logConf) logger = logging.getLogger(__name__) if s.getBoolean(["accessControl", "enabled"]): userManagerName = settings().get(["accessControl", "userManager"]) try: clazz = util.getClass(userManagerName) userManager = clazz() except AttributeError, e: logger.exception( "Could not instantiate user manager %s, will run with accessControl disabled!" % userManagerName) softwareManager = swManager() VERSION = softwareManager.versionString logger.info("Starting AstroBox (%s)" % VERSION) eventManager = events.eventManager() gcodeManager = gcodefiles.GcodeManager() printer = Printer(gcodeManager) # configure timelapse #octoprint.timelapse.configureTimelapse() app.wsgi_app = ReverseProxied(app.wsgi_app) app.secret_key = boxrouterManager().boxId loginManager = LoginManager() loginManager.session_protection = "strong" loginManager.user_callback = load_user if userManager is None: loginManager.anonymous_user = users.DummyUser principals.identity_loaders.appendleft(users.dummy_identity_loader) loginManager.init_app(app) # setup command triggers events.CommandTrigger(printer) if self._debug: events.DebugEventListener() from astroprint.network import networkManager as networkManagerLoader networkManager = networkManagerLoader() if networkManager.isOnline(): softwareManager.checkForcedUpdate() if self._host is None: self._host = s.get(["server", "host"]) if self._port is None: self._port = s.getInt(["server", "port"]) logger.info("Listening on http://%s:%d" % (self._host, self._port)) app.debug = self._debug from octoprint.server.api import api app.register_blueprint(api, url_prefix="/api") self._boxrouter = boxrouterManager() self._router = SockJSRouter(self._createSocketConnection, "/sockjs") self._discovery = discoveryManager() def access_validation_factory(validator): """ Creates an access validation wrapper using the supplied validator. :param validator: the access validator to use inside the validation wrapper :return: an access validation wrapper taking a request as parameter and performing the request validation """ def f(request): """ Creates a custom wsgi and Flask request context in order to be able to process user information stored in the current session. :param request: The Tornado request for which to create the environment and context """ wsgi_environ = tornado.wsgi.WSGIContainer.environ(request) with app.request_context(wsgi_environ): app.session_interface.open_session(app, flask.request) loginManager.reload_user() validator(flask.request) return f self._tornado_app = Application(self._router.urls + [ (r"/downloads/timelapse/([^/]*\.mpg)", LargeResponseHandler, { "path": s.getBaseFolder("timelapse"), "as_attachment": True }), (r"/downloads/files/local/([^/]*\.(gco|gcode))", LargeResponseHandler, { "path": s.getBaseFolder("uploads"), "as_attachment": True }), (r"/downloads/logs/([^/]*)", LargeResponseHandler, { "path": s.getBaseFolder("logs"), "as_attachment": True, "access_validation": access_validation_factory(admin_validator) }), (r"/downloads/camera/current", UrlForwardHandler, { "url": s.get(["webcam", "snapshot"]), "as_attachment": True, "access_validation": access_validation_factory(user_validator) }), (r".*", FallbackHandler, { "fallback": WSGIContainer(app.wsgi_app) }) ]) self._server = HTTPServer(self._tornado_app) self._server.listen(self._port, address=self._host) eventManager.fire(events.Events.STARTUP) if s.getBoolean(["serial", "autoconnect"]): (port, baudrate) = s.get(["serial", "port" ]), s.getInt(["serial", "baudrate"]) connectionOptions = getConnectionOptions() if port in connectionOptions["ports"]: printer.connect(port, baudrate) # start up watchdogs observer = Observer() #observer.schedule(GcodeWatchdogHandler(gcodeManager, printer), s.getBaseFolder("watched")) observer.schedule(UploadCleanupWatchdogHandler(gcodeManager), s.getBaseFolder("uploads")) observer.start() try: IOLoop.instance().start() except KeyboardInterrupt: logger.info("Goodbye!") except: logger.fatal( "Now that is embarrassing... Something really really went wrong here. Please report this including the stacktrace below in OctoPrint's bugtracker. Thanks!" ) logger.exception("Stacktrace follows:") finally: observer.stop() observer.join()