Skip to content

Commit a258fd6

Browse files
committed
Add CORS-allow policy launch argument using regex
1 parent 804d9fb commit a258fd6

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

modules/shared.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@
8181
parser.add_argument("--enable-console-prompts", action='store_true', help="print prompts to console when generating with txt2img and img2img", default=False)
8282
parser.add_argument('--vae-path', type=str, help='Path to Variational Autoencoders model', default=None)
8383
parser.add_argument("--disable-safe-unpickle", action='store_true', help="disable checking pytorch models for malicious code", default=False)
84-
parser.add_argument("--api", action='store_true', help="use api=True to launch the api with the webui")
85-
parser.add_argument("--nowebui", action='store_true', help="use api=True to launch the api instead of the webui")
84+
parser.add_argument("--api", action='store_true', help="use api=True to launch the API together with the webui (use --nowebui instead for only the API)")
85+
parser.add_argument("--nowebui", action='store_true', help="use api=True to launch the API instead of the webui")
8686
parser.add_argument("--ui-debug-mode", action='store_true', help="Don't load model to quickly launch UI")
8787
parser.add_argument("--device-id", type=str, help="Select the default CUDA device to use (export CUDA_VISIBLE_DEVICES=0,1,etc might be needed before)", default=None)
8888
parser.add_argument("--administrator", action='store_true', help="Administrator rights", default=False)
89-
parser.add_argument("--cors-allow-origins", type=str, help="Allowed CORS origins", default=None)
89+
parser.add_argument("--cors-allow-origins", type=str, help="Allowed CORS origin(s) in the form of a comma-separated list (no spaces)", default=None)
90+
parser.add_argument("--cors-allow-origins-regex", type=str, help="Allowed CORS origin(s) in the form of a single regular expression", default=None)
9091
parser.add_argument("--tls-keyfile", type=str, help="Partially enables TLS, requires --tls-certfile to fully function", default=None)
9192
parser.add_argument("--tls-certfile", type=str, help="Partially enables TLS, requires --tls-keyfile to fully function", default=None)
9293
parser.add_argument("--server-name", type=str, help="Sets hostname of server", default=None)

webui.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ def sigint_handler(sig, frame):
107107

108108

109109
def setup_cors(app):
110-
if cmd_opts.cors_allow_origins:
110+
if cmd_opts.cors_allow_origins and cmd_opts.cors_allow_origins_regex:
111+
app.add_middleware(CORSMiddleware, allow_origins=cmd_opts.cors_allow_origins.split(','), allow_origin_regex=cmd_opts.cors_allow_origins_regex, allow_methods=['*'])
112+
elif cmd_opts.cors_allow_origins:
111113
app.add_middleware(CORSMiddleware, allow_origins=cmd_opts.cors_allow_origins.split(','), allow_methods=['*'])
114+
elif cmd_opts.cors_allow_origins_regex:
115+
app.add_middleware(CORSMiddleware, allow_origin_regex=cmd_opts.cors_allow_origins_regex, allow_methods=['*'])
112116

113117

114118
def create_api(app):

0 commit comments

Comments
 (0)