Skip to content

Commit dce68b1

Browse files
committed
Server: Set host name to localhost and add access to static docs
1 parent 95f67d8 commit dce68b1

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

ardublocklyserver/server.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def launch_server(ip='localhost', port=8000, document_root_=''):
3737
print('Setting HTTP Server Document Root to:\n\t%s' % document_root_)
3838
document_root = document_root_
3939
print('Launch Server:')
40-
run(app, server='waitress', host=ip, port=port, debug=True)
40+
run(app, server='waitress', host=ip, port=port, debug=False)
4141

4242

4343
@app.hook('before_request')
@@ -120,6 +120,24 @@ def static_closure(file_path):
120120
root=os.path.join(document_root, 'closure-library'))
121121

122122

123+
@app.route('/docs')
124+
def static_docs_index():
125+
"""Set a /docs/Home/index.html redirect from /docs/"""
126+
redirect('/docs/Home/index.html')
127+
128+
129+
@app.route('/docs/<file_path:path>')
130+
def static_docs(file_path):
131+
"""Serve the 'docs' folder static files and redirect folders to index.html.
132+
133+
:param file_path: File path inside the 'docs' folder.
134+
:return: Full HTTPResponse for the static file.
135+
"""
136+
if os.path.isdir(os.path.join(document_root, 'docs', file_path)):
137+
return redirect('/docs/%s/index.html' % file_path)
138+
return static_file(file_path, root=os.path.join(document_root, 'docs'))
139+
140+
123141
#
124142
# Retrieve or update Settings request handlers. Only GET and PUT available.
125143
#
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
coverage>=4.3.4
2+
requests>=2.13.0
3+
flake8>=3.3.0
4+
mock>=2.0.0

ardublocklyserver/tests/server_test.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ def setUp(self):
9090
if not os.path.isdir(self.__class__.temp_folder):
9191
os.makedirs(self.__class__.temp_folder)
9292
reset_settings()
93-
# The configurations in the Circle CI server are weird and it refuses
94-
# HTTP localhost connections if done too quickly.
95-
if os.environ.get('CIRCLECI'):
96-
print("CircleCI detected, waiting 0.5 second...")
93+
# The configurations in the Travis and Circle CI servers refuses HTTP
94+
# localhost connections if done too quickly.
95+
if os.environ.get('CIRCLECI') or os.environ.get('TRAVIS'):
96+
print("Travis or CircleCI detected, waiting 1 second...")
9797
sleep(1)
9898

9999
def tearDown(self):
@@ -152,6 +152,10 @@ def test_static_closure(self):
152152
"""Test the files in /closure-library can be accessed."""
153153
self.helper_test_static_file('closure-library', 'index.js')
154154

155+
def test_static_docs(self):
156+
"""Test the files in /docs can be accessed."""
157+
self.helper_test_static_file('docs', 'index.html')
158+
155159
#
156160
# Test for entry point redirect
157161
#
@@ -194,6 +198,27 @@ def test_ardublockly_redirect(self):
194198
self.assertEqual(response.headers['content-type'],
195199
'text/html; charset=UTF-8')
196200

201+
def test_static_docs_redirect(self):
202+
"""Test docs access to folders redirects to its index.html page."""
203+
docs_dir = os.path.join(self.temp_folder, 'docs')
204+
inner_docs_dir = os.path.join(docs_dir, 'folder')
205+
os.makedirs(docs_dir)
206+
os.makedirs(inner_docs_dir)
207+
open(os.path.join(inner_docs_dir, 'index.html'), 'w').close()
208+
209+
response = requests.get('%s/docs/folder/' % self.SERVER_URL)
210+
211+
self.assertTrue(response.ok)
212+
self.assertEqual(response.status_code, 200)
213+
self.assertEqual(response.url,
214+
'%s/docs/folder/index.html' % self.SERVER_URL)
215+
self.assertTrue(response.history[0].is_redirect)
216+
self.assertEqual(response.history[0].status_code, 303)
217+
self.assertEqual(response.history[0].url.rstrip('/'),
218+
self.SERVER_URL + '/docs/folder')
219+
self.assertEqual(response.headers['content-type'],
220+
'text/html; charset=UTF-8')
221+
197222
#
198223
# Tests for unauthorised access methods
199224
#

start.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
import ardublocklyserver.compilersettings
2121

2222
# Server IP and PORT settings
23-
SERVER_IP = '127.0.0.1'
23+
SERVER_IP = 'localhost'
2424
SERVER_PORT = 8000
2525

2626

2727
def open_browser(ip, port, file_path=''):
2828
"""Start a browser in a separate thread after waiting for half a second.
2929
30-
:param ip:
31-
:param port:
30+
:param ip: IP address or host name to build URL.
31+
:param port: Server port to build the URL.
3232
:param file_path: Path within domain for the browser to open.
33-
:return:
33+
:return: None.
3434
"""
3535
def _open_browser():
3636
webbrowser.get().open('http://%s:%s/%s' % (ip, port, file_path))
@@ -128,7 +128,7 @@ def parsing_cl_args():
128128

129129

130130
def main():
131-
"""Main entry point for the application.
131+
"""Entry point for the application.
132132
133133
Initialises the Settings singleton, resolves paths, and starts the server.
134134
"""
@@ -137,7 +137,7 @@ def main():
137137
if os.path.isdir(ardublocklyserver.local_packages_path):
138138
print('Local packages: %s' % ardublocklyserver.local_packages_path)
139139
else:
140-
print('Not using local-packages.')
140+
print('Not using local-packages, likely running packaged.')
141141

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

0 commit comments

Comments
 (0)