Commit dce68b11 authored by carlosperate's avatar carlosperate

Server: Set host name to localhost and add access to static docs

parent 95f67d87
...@@ -37,7 +37,7 @@ def launch_server(ip='localhost', port=8000, document_root_=''): ...@@ -37,7 +37,7 @@ def launch_server(ip='localhost', port=8000, document_root_=''):
print('Setting HTTP Server Document Root to:\n\t%s' % document_root_) print('Setting HTTP Server Document Root to:\n\t%s' % document_root_)
document_root = document_root_ document_root = document_root_
print('Launch Server:') print('Launch Server:')
run(app, server='waitress', host=ip, port=port, debug=True) run(app, server='waitress', host=ip, port=port, debug=False)
@app.hook('before_request') @app.hook('before_request')
...@@ -120,6 +120,24 @@ def static_closure(file_path): ...@@ -120,6 +120,24 @@ def static_closure(file_path):
root=os.path.join(document_root, 'closure-library')) root=os.path.join(document_root, 'closure-library'))
@app.route('/docs')
def static_docs_index():
"""Set a /docs/Home/index.html redirect from /docs/"""
redirect('/docs/Home/index.html')
@app.route('/docs/<file_path:path>')
def static_docs(file_path):
"""Serve the 'docs' folder static files and redirect folders to index.html.
:param file_path: File path inside the 'docs' folder.
:return: Full HTTPResponse for the static file.
"""
if os.path.isdir(os.path.join(document_root, 'docs', file_path)):
return redirect('/docs/%s/index.html' % file_path)
return static_file(file_path, root=os.path.join(document_root, 'docs'))
# #
# Retrieve or update Settings request handlers. Only GET and PUT available. # Retrieve or update Settings request handlers. Only GET and PUT available.
# #
......
coverage>=4.3.4
requests>=2.13.0
flake8>=3.3.0
mock>=2.0.0
...@@ -90,10 +90,10 @@ class ServerTestCase(unittest.TestCase): ...@@ -90,10 +90,10 @@ class ServerTestCase(unittest.TestCase):
if not os.path.isdir(self.__class__.temp_folder): if not os.path.isdir(self.__class__.temp_folder):
os.makedirs(self.__class__.temp_folder) os.makedirs(self.__class__.temp_folder)
reset_settings() reset_settings()
# The configurations in the Circle CI server are weird and it refuses # The configurations in the Travis and Circle CI servers refuses HTTP
# HTTP localhost connections if done too quickly. # localhost connections if done too quickly.
if os.environ.get('CIRCLECI'): if os.environ.get('CIRCLECI') or os.environ.get('TRAVIS'):
print("CircleCI detected, waiting 0.5 second...") print("Travis or CircleCI detected, waiting 1 second...")
sleep(1) sleep(1)
def tearDown(self): def tearDown(self):
...@@ -152,6 +152,10 @@ class ServerTestCase(unittest.TestCase): ...@@ -152,6 +152,10 @@ class ServerTestCase(unittest.TestCase):
"""Test the files in /closure-library can be accessed.""" """Test the files in /closure-library can be accessed."""
self.helper_test_static_file('closure-library', 'index.js') self.helper_test_static_file('closure-library', 'index.js')
def test_static_docs(self):
"""Test the files in /docs can be accessed."""
self.helper_test_static_file('docs', 'index.html')
# #
# Test for entry point redirect # Test for entry point redirect
# #
...@@ -194,6 +198,27 @@ class ServerTestCase(unittest.TestCase): ...@@ -194,6 +198,27 @@ class ServerTestCase(unittest.TestCase):
self.assertEqual(response.headers['content-type'], self.assertEqual(response.headers['content-type'],
'text/html; charset=UTF-8') 'text/html; charset=UTF-8')
def test_static_docs_redirect(self):
"""Test docs access to folders redirects to its index.html page."""
docs_dir = os.path.join(self.temp_folder, 'docs')
inner_docs_dir = os.path.join(docs_dir, 'folder')
os.makedirs(docs_dir)
os.makedirs(inner_docs_dir)
open(os.path.join(inner_docs_dir, 'index.html'), 'w').close()
response = requests.get('%s/docs/folder/' % self.SERVER_URL)
self.assertTrue(response.ok)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.url,
'%s/docs/folder/index.html' % self.SERVER_URL)
self.assertTrue(response.history[0].is_redirect)
self.assertEqual(response.history[0].status_code, 303)
self.assertEqual(response.history[0].url.rstrip('/'),
self.SERVER_URL + '/docs/folder')
self.assertEqual(response.headers['content-type'],
'text/html; charset=UTF-8')
# #
# Tests for unauthorised access methods # Tests for unauthorised access methods
# #
......
...@@ -20,17 +20,17 @@ import ardublocklyserver.server ...@@ -20,17 +20,17 @@ import ardublocklyserver.server
import ardublocklyserver.compilersettings import ardublocklyserver.compilersettings
# Server IP and PORT settings # Server IP and PORT settings
SERVER_IP = '127.0.0.1' SERVER_IP = 'localhost'
SERVER_PORT = 8000 SERVER_PORT = 8000
def open_browser(ip, port, file_path=''): def open_browser(ip, port, file_path=''):
"""Start a browser in a separate thread after waiting for half a second. """Start a browser in a separate thread after waiting for half a second.
:param ip: :param ip: IP address or host name to build URL.
:param port: :param port: Server port to build the URL.
:param file_path: Path within domain for the browser to open. :param file_path: Path within domain for the browser to open.
:return: :return: None.
""" """
def _open_browser(): def _open_browser():
webbrowser.get().open('http://%s:%s/%s' % (ip, port, file_path)) webbrowser.get().open('http://%s:%s/%s' % (ip, port, file_path))
...@@ -128,7 +128,7 @@ def parsing_cl_args(): ...@@ -128,7 +128,7 @@ def parsing_cl_args():
def main(): def main():
"""Main entry point for the application. """Entry point for the application.
Initialises the Settings singleton, resolves paths, and starts the server. Initialises the Settings singleton, resolves paths, and starts the server.
""" """
...@@ -137,7 +137,7 @@ def main(): ...@@ -137,7 +137,7 @@ def main():
if os.path.isdir(ardublocklyserver.local_packages_path): if os.path.isdir(ardublocklyserver.local_packages_path):
print('Local packages: %s' % ardublocklyserver.local_packages_path) print('Local packages: %s' % ardublocklyserver.local_packages_path)
else: else:
print('Not using local-packages.') print('Not using local-packages, likely running packaged.')
print('\n======= Parsing Command line arguments =======') print('\n======= Parsing Command line arguments =======')
find_project_root, launch_browser, server_root = parsing_cl_args() find_project_root, launch_browser, server_root = parsing_cl_args()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment