Exemplo n.º 1
0
def _start(root, host=None, port=None, use_simple_log_format=True):
  logging.info('Serving pod with root: {}'.format(root))
  logger_format = ('[%(time)s] "%(REQUEST_METHOD)s %(REQUEST_URI)s" %(status)s'
                   if use_simple_log_format else None)
  root = os.path.abspath(os.path.normpath(root))
  handlers.set_pod_root(root)
  app = main_lib.application
  app = translogger.TransLogger(app, format=logger_format)
  httpserver.serve(app, host=host, port=port)
Exemplo n.º 2
0
  def test_request(self):
    # Verify application errors when no pod root is set.
    handlers.set_pod_root(None)
    request = webapp2.Request.blank('/')
    response = request.get_response(main.application)
    self.assertEqual(response.status_int, 500)

    # When serving a pod, should 200.
    root = os.path.join(os.path.dirname(__file__), '..', 'pods', 'testdata', 'pod')
    handlers.set_pod_root(root)
    request = webapp2.Request.blank('/')
    response = request.get_response(main.application)
    self.assertEqual(response.status_int, 200)

    # Verify 404 is sent for blank page.
    request = webapp2.Request.blank('/dummy/page/')
    response = request.get_response(main.application)
    self.assertEqual(response.status_int, 404)
Exemplo n.º 3
0
def _start(pod, host=None, port=None, open_browser=False):
  root = pod.root
  preprocessors = pod.list_preprocessors()

  # Add the translation preprocessor as a builtin.
  preprocessors.insert(0, translation_preprocessor.TranslationPreprocessor(pod=pod))

  try:
    # TODO(jeremydw): Custom server logs.
    # logger_format = ('[%(time)s] "%(REQUEST_METHOD)s %(REQUEST_URI)s" %(status)s'
    #                 if use_simple_log_format else None)

    # Map directory names to preprocessors.
    dirs_to_preprocessors = {}
    for preprocessor in preprocessors:
      for watched_dir in preprocessor.list_watched_dirs():
        if watched_dir not in dirs_to_preprocessors:
          dirs_to_preprocessors[watched_dir] = []
        dirs_to_preprocessors[watched_dir].append(preprocessor)

    # Run all preprocessors for the pod.
    [preprocessor.first_run() for preprocessor in preprocessors]

    # Create file watchers for each preprocessor.
    file_watchers_to_preprocessors = {}
    for dirname, preprocessors in dirs_to_preprocessors.iteritems():
      dirname = os.path.join(pod.root, dirname.lstrip('/'))
      change_watcher = file_watchers.get_file_watcher([dirname])
      change_watcher.start()
      file_watchers_to_preprocessors[change_watcher] = preprocessors

    # Start a thread where preprocessors can run if there are changes.
    quit_event = threading.Event()
    change_watcher_thread = threading.Thread(
        target=_loop_watching_for_changes,
        args=(pod, file_watchers_to_preprocessors, quit_event))
    change_watcher_thread.start()

    # Create the development server.
    root = os.path.abspath(os.path.normpath(root))
    handlers.set_pod_root(root)
    app = main_lib.application
    port = 8080 if port is None else port
    host = 'localhost' if host is None else host
    httpd = simple_server.make_server(host, int(port), app)
  except:
    logging.exception('Failed to start server.')
    quit_event.set()
    change_watcher_thread.join()
    sys.exit()

  try:
    root_path = pod.get_root_path()
    url = 'http://{}:{}{}'.format(host, port, root_path)
    logging.info('---')
    logging.info(utils.colorize('{blue}The Grow SDK is experimental.{/blue} Expect backwards incompatibility until v0.1.0.'))
    logging.info('Thank you for testing and contributing! Visit http://growsdk.org for resources.')
    logging.info('---')
    logging.info('Serving pod {} => {}'.format(root, url))
    text = '{green}READY!{/green} Press Ctrl+C to shut down. Tip: Use --open to open a browser automatically.'
    logging.info(utils.colorize(text))

    def start_browser(server_ready_event):
      server_ready_event.wait()
      if open_browser:
        webbrowser.open(url)

    server_ready_event = threading.Event()
    browser_thread = threading.Thread(target=start_browser, args=(server_ready_event,))
    browser_thread.start()
    server_ready_event.set()
    httpd.serve_forever()
    browser_thread.join()

  except KeyboardInterrupt:
    logging.info('Shutting down...')
    httpd.server_close()

  # Clean up once serve exits.
  quit_event.set()
  change_watcher_thread.join()
  sys.exit()