Skip to content

Commit

Permalink
Server: Set host name to localhost and add access to static docs
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosperate committed May 7, 2017
1 parent 95f67d8 commit dce68b1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
20 changes: 19 additions & 1 deletion ardublocklyserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
document_root = document_root_
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')
Expand Down Expand Up @@ -120,6 +120,24 @@ def static_closure(file_path):
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.
#
Expand Down
4 changes: 4 additions & 0 deletions ardublocklyserver/tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage>=4.3.4
requests>=2.13.0
flake8>=3.3.0
mock>=2.0.0
33 changes: 29 additions & 4 deletions ardublocklyserver/tests/server_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ def setUp(self):
if not os.path.isdir(self.__class__.temp_folder):
os.makedirs(self.__class__.temp_folder)
reset_settings()
# The configurations in the Circle CI server are weird and it refuses
# HTTP localhost connections if done too quickly.
if os.environ.get('CIRCLECI'):
print("CircleCI detected, waiting 0.5 second...")
# The configurations in the Travis and Circle CI servers refuses HTTP
# localhost connections if done too quickly.
if os.environ.get('CIRCLECI') or os.environ.get('TRAVIS'):
print("Travis or CircleCI detected, waiting 1 second...")
sleep(1)

def tearDown(self):
Expand Down Expand Up @@ -152,6 +152,10 @@ def test_static_closure(self):
"""Test the files in /closure-library can be accessed."""
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
#
Expand Down Expand Up @@ -194,6 +198,27 @@ def test_ardublockly_redirect(self):
self.assertEqual(response.headers['content-type'],
'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
#
Expand Down
12 changes: 6 additions & 6 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
import ardublocklyserver.compilersettings

# Server IP and PORT settings
SERVER_IP = '127.0.0.1'
SERVER_IP = 'localhost'
SERVER_PORT = 8000


def open_browser(ip, port, file_path=''):
"""Start a browser in a separate thread after waiting for half a second.
:param ip:
:param port:
:param ip: IP address or host name to build URL.
:param port: Server port to build the URL.
:param file_path: Path within domain for the browser to open.
:return:
:return: None.
"""
def _open_browser():
webbrowser.get().open('http://%s:%s/%s' % (ip, port, file_path))
Expand Down Expand Up @@ -128,7 +128,7 @@ def parsing_cl_args():


def main():
"""Main entry point for the application.
"""Entry point for the application.
Initialises the Settings singleton, resolves paths, and starts the server.
"""
Expand All @@ -137,7 +137,7 @@ def main():
if os.path.isdir(ardublocklyserver.local_packages_path):
print('Local packages: %s' % ardublocklyserver.local_packages_path)
else:
print('Not using local-packages.')
print('Not using local-packages, likely running packaged.')

print('\n======= Parsing Command line arguments =======')
find_project_root, launch_browser, server_root = parsing_cl_args()
Expand Down

0 comments on commit dce68b1

Please sign in to comment.