Add support for url_prefix

This commit is contained in:
gdudovitch 2024-06-16 17:17:34 +03:00
parent 746a0410d4
commit 8c90348a80
3 changed files with 21 additions and 9 deletions

View File

@ -39,6 +39,7 @@ parser.add_argument("--tls-keyfile", type=str, help="Path to TLS (SSL) key file.
parser.add_argument("--tls-certfile", type=str, help="Path to TLS (SSL) certificate file. Enables TLS, makes app accessible at https://... requires --tls-keyfile to function") parser.add_argument("--tls-certfile", type=str, help="Path to TLS (SSL) certificate file. Enables TLS, makes app accessible at https://... requires --tls-keyfile to function")
parser.add_argument("--enable-cors-header", type=str, default=None, metavar="ORIGIN", nargs="?", const="*", help="Enable CORS (Cross-Origin Resource Sharing) with optional origin or allow all with default '*'.") parser.add_argument("--enable-cors-header", type=str, default=None, metavar="ORIGIN", nargs="?", const="*", help="Enable CORS (Cross-Origin Resource Sharing) with optional origin or allow all with default '*'.")
parser.add_argument("--max-upload-size", type=float, default=100, help="Set the maximum upload size in MB.") parser.add_argument("--max-upload-size", type=float, default=100, help="Set the maximum upload size in MB.")
parser.add_argument("--url-prefix", type=str, default="", help="Specify a URL prefix for the server.")
parser.add_argument("--extra-model-paths-config", type=str, default=None, metavar="PATH", nargs='+', action='append', help="Load one or more extra_model_paths.yaml files.") parser.add_argument("--extra-model-paths-config", type=str, default=None, metavar="PATH", nargs='+', action='append', help="Load one or more extra_model_paths.yaml files.")
parser.add_argument("--output-directory", type=str, default=None, help="Set the ComfyUI output directory.") parser.add_argument("--output-directory", type=str, default=None, help="Set the ComfyUI output directory.")

View File

@ -203,7 +203,7 @@ if __name__ == "__main__":
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
server = server.PromptServer(loop) server = server.PromptServer(loop, url_prefix=args.url_prefix)
q = execution.PromptQueue(server) q = execution.PromptQueue(server)
extra_model_paths_config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "extra_model_paths.yaml") extra_model_paths_config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "extra_model_paths.yaml")
@ -247,7 +247,7 @@ if __name__ == "__main__":
import webbrowser import webbrowser
if os.name == 'nt' and address == '0.0.0.0': if os.name == 'nt' and address == '0.0.0.0':
address = '127.0.0.1' address = '127.0.0.1'
webbrowser.open(f"{scheme}://{address}:{port}") webbrowser.open(f"{scheme}://{address}:{port}/{args.url_prefix}/")
call_on_start = startup_server call_on_start = startup_server
try: try:

View File

@ -62,8 +62,9 @@ def create_cors_middleware(allowed_origin: str):
return cors_middleware return cors_middleware
class PromptServer(): class PromptServer():
def __init__(self, loop): def __init__(self, loop, url_prefix=""):
PromptServer.instance = self PromptServer.instance = self
self.url_prefix = url_prefix
mimetypes.init() mimetypes.init()
mimetypes.types_map['.js'] = 'application/javascript; charset=utf-8' mimetypes.types_map['.js'] = 'application/javascript; charset=utf-8'
@ -81,6 +82,8 @@ class PromptServer():
max_upload_size = round(args.max_upload_size * 1024 * 1024) max_upload_size = round(args.max_upload_size * 1024 * 1024)
self.app = web.Application(client_max_size=max_upload_size, middlewares=middlewares) self.app = web.Application(client_max_size=max_upload_size, middlewares=middlewares)
if self.url_prefix:
self.sub_app = web.Application(client_max_size=max_upload_size, middlewares=middlewares)
self.sockets = dict() self.sockets = dict()
self.web_root = os.path.join(os.path.dirname( self.web_root = os.path.join(os.path.dirname(
os.path.realpath(__file__)), "web") os.path.realpath(__file__)), "web")
@ -529,18 +532,23 @@ class PromptServer():
return web.Response(status=200) return web.Response(status=200)
def add_routes(self): def add_routes(self):
app = self.sub_app if self.url_prefix else self.app
self.user_manager.add_routes(self.routes) self.user_manager.add_routes(self.routes)
self.app.add_routes(self.routes) app.add_routes(self.routes)
for name, dir in nodes.EXTENSION_WEB_DIRS.items(): for name, dir in nodes.EXTENSION_WEB_DIRS.items():
self.app.add_routes([ app.add_routes([
web.static('/extensions/' + urllib.parse.quote(name), dir), web.static(f'/extensions/' + urllib.parse.quote(name), dir),
]) ])
self.app.add_routes([ app.add_routes([
web.static('/', self.web_root), web.static(f'/', self.web_root),
]) ])
if self.url_prefix:
self.app.add_subapp(f'/{self.url_prefix}', self.sub_app)
def get_queue_info(self): def get_queue_info(self):
prompt_info = {} prompt_info = {}
exec_info = {} exec_info = {}
@ -637,7 +645,10 @@ class PromptServer():
if verbose: if verbose:
logging.info("Starting server\n") logging.info("Starting server\n")
logging.info("To see the GUI go to: {}://{}:{}".format(scheme, address, port)) prefix = self.url_prefix
if self.url_prefix:
prefix = f"{self.url_prefix}/"
logging.info("To see the GUI go to: {}://{}:{}/{}".format(scheme, address, port, prefix))
if call_on_start is not None: if call_on_start is not None:
call_on_start(scheme, address, port) call_on_start(scheme, address, port)