When an Esri Web AppBuilder web app is configured with a portalUrl
value served from HTTPS, the web app automatically redirects users to HTTPS when visited via HTTP. While this is best-practice in production, it can be a burden in development when you want to quickly run a local version of the web app. Below is a quick script written with Python standard libraries to serve a web app over HTTP. It works by serving a config.json
that is modified to use HTTP rather than HTTPS. This allows you to keep config.json
using the HTTPS configuration for production but serve the web app via HTTP during development.
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import argparse | |
import json | |
import os | |
from BaseHTTPServer import HTTPServer | |
from SimpleHTTPServer import SimpleHTTPRequestHandler | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Simple webserver') | |
parser.add_argument('address', type=str, help='Server address', nargs='?', | |
default='127.0.0.1:8080') | |
parser.add_argument('--dir', '-d', type=str, help='Directory to be served', | |
default='./') | |
args = parser.parse_args() | |
server, port = (args.address.split(':')[:2] | |
if ':' in args.address else | |
(args.address, None)) | |
server = server or '127.0.0.1' | |
port = int(port or 8080) | |
dir_to_serve = os.path.abspath(args.dir) | |
class RequestHandler(SimpleHTTPRequestHandler): | |
def do_GET(self): | |
# Serve altered config.json to prevent redirect to HTTPS during dev | |
if self.path.startswith('/config.json'): | |
with open(os.path.join(dir_to_serve, 'config.json')) as f: | |
config = json.load(f) | |
url = config['portalUrl'].replace('https://', 'http://') | |
config['portalUrl'] = url | |
self.send_response(200) | |
self.send_header('Content-Type', 'application/json') | |
self.end_headers() | |
return self.wfile.write(json.dumps(config)) | |
return SimpleHTTPRequestHandler.do_GET(self) | |
server = HTTPServer((server, port), RequestHandler) | |
sa = server.socket.getsockname() | |
print("🌎 Serving %s at http://%s:%s/" % ((dir_to_serve,) + sa)) | |
try: | |
server.serve_forever() | |
except KeyboardInterrupt: | |
print("\nGoodbye.") |
The script should be saved alongside the config.json
in the root of the web app. I would recommend running chmod a+x runserver
to enable you to execute the server directly via ./runserver
. Alternatively, you could install this somewhere on your system path to invoke from any directory (something like cp runserver /usr/local/bin/serve-esri-app
for a unix-based system).