-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
37 lines (32 loc) · 1.59 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
try:
# try to import flask, or return error if has not been installed
from flask import Flask
from flask import send_from_directory
except ImportError:
print("You don't have Flask installed, run `$ pip3 install flask` and try again")
exit(1)
import os, subprocess
static_file_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), './')
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 #disable cache
# Serving the index file
@app.route('/', methods=['GET'])
def serve_dir_directory_index():
if os.path.exists("app.py"):
# if app.py exists we use the render function
out = subprocess.Popen(['python3','app.py'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout,stderr = out.communicate()
return stdout if out.returncode == 0 else f"<pre style='color: red;'>{stdout.decode('utf-8')}</pre>"
if os.path.exists("index.html"):
return send_from_directory(static_file_dir, 'index.html')
else:
return "<h1 align='center'>404</h1><h2 align='center'>Missing index.html file</h2><p align='center'><img src='https://github.com/4GeeksAcademy/html-hello/blob/main/.vscode/rigo-baby.jpeg?raw=true' /></p>"
# Serving any other image
@app.route('/<path:path>', methods=['GET'])
def serve_any_other_file(path):
if not os.path.isfile(os.path.join(static_file_dir, path)):
path = os.path.join(path, 'index.html')
response = send_from_directory(static_file_dir, path)
response.cache_control.max_age = 0 # avoid cache memory
return response
app.run(host='0.0.0.0',port=3000, debug=True, extra_files=['./',])