Let users choose hostname and port

This commit is contained in:
Ian Arawjo 2023-07-20 11:17:58 -04:00
parent 31340bedc5
commit 07150fc6ae
3 changed files with 27 additions and 9 deletions

View File

@ -19,7 +19,10 @@ def main():
# action='store_true')
# TODO: Reimplement this where the React server is given the backend's port before loading.
# serve_parser.add_argument('--port', help='The port to run the server on. Defaults to 8000.', type=int, default=8000, nargs='?')
serve_parser.add_argument('--port', help='The port to run the server on. Defaults to 8000.', type=int, default=8000, nargs='?')
serve_parser.add_argument('--host',
help="The host to run the server on. Defaults to 'localhost'.",
type=str, default="localhost", nargs='?')
args = parser.parse_args()
@ -28,10 +31,11 @@ def main():
parser.print_help()
exit(0)
port = 8000 # args.port if args.port else 8000
port = args.port if args.port else 8000
host = args.host if args.host else "localhost"
print(f"Serving Flask server on port {port}...")
run_server(host="localhost", port=port, cmd_args=args)
print(f"Serving Flask server on {host} on port {port}...")
run_server(host=host, port=port, cmd_args=args)
if __name__ == "__main__":
main()

View File

@ -14,6 +14,8 @@ import requests as py_requests
"""
# Setup Flask app to serve static version of React front-end
HOSTNAME = "localhost"
PORT = 8000
BUILD_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'react-server', 'build')
STATIC_DIR = os.path.join(BUILD_DIR, 'static')
app = Flask(__name__, static_folder=STATIC_DIR, template_folder=BUILD_DIR)
@ -228,7 +230,14 @@ def run_over_responses(eval_func, responses: list, scope: str) -> list:
# Serve React app (static; no hot reloading)
@app.route("/")
def index():
return render_template("index.html")
# Get the index.html HTML code
html_str = render_template("index.html")
# Inject global JS variables __CF_HOSTNAME and __CF_PORT at the top so that the application knows
# that it's running from a Flask server, and what the hostname and port of that server is:
html_str = html_str[:60] + f'<script>window.__CF_HOSTNAME="{HOSTNAME}"; window.__CF_PORT={PORT};</script>' + html_str[60:]
return html_str
@app.route('/app/executepy', methods=['POST'])
def executepy():
@ -489,7 +498,10 @@ async def callDalai():
return ret
def run_server(host="", port=8000, cmd_args=None):
def run_server(host="", port=8000, cmd_args=None):
global HOSTNAME, PORT
HOSTNAME = host
PORT = port
app.run(host=host, port=port)
if __name__ == '__main__':

View File

@ -17,8 +17,9 @@ const AI_PROMPT = "\n\nAssistant:";
const fetch = require('node-fetch');
/** Where the ChainForge Flask server is being hosted. */
export const FLASK_BASE_URL = 'http://localhost:8000/';
/** Where the ChainForge Flask server is being hosted, if any. */
// @ts-ignore
export const FLASK_BASE_URL = (window.__CF_HOSTNAME !== undefined && window.__CF_PORT !== undefined) ? `http://${window.__CF_HOSTNAME}:${window.__CF_PORT}/` : 'http://localhost:8000/';
export async function call_flask_backend(route: string, params: Dict | string): Promise<Dict> {
return fetch(`${FLASK_BASE_URL}app/${route}`, {
@ -42,7 +43,8 @@ export function APP_IS_RUNNING_LOCALLY(): boolean {
// Calculate whether we're running the app locally or not, and save the result
try {
const location = window.location;
_APP_IS_RUNNING_LOCALLY = location.hostname === "localhost" || location.hostname === "127.0.0.1" || location.hostname === "0.0.0.0" || location.hostname === "";
// @ts-ignore
_APP_IS_RUNNING_LOCALLY = location.hostname === "localhost" || location.hostname === "127.0.0.1" || location.hostname === "0.0.0.0" || location.hostname === "" || window.__CF_HOSTNAME !== undefined;
} catch (e) {
// ReferenceError --window or location does not exist.
// We must not be running client-side in a browser, in this case (e.g., we are running a Node.js server)