Skip to content

Commit 9e78d2c

Browse files
Merge pull request #4416 from Keavon/cors-regex
Add CORS-allow policy launch argument using regex
2 parents 9e8c4e2 + 2f90496 commit 9e78d2c

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

modules/shared.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,14 @@
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("--api-auth", type=str, help='Set authentication for api like "username:password"; or comma-delimit multiple like "u1:p1,u2:p2,u3:p3"', default=None)
86-
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("--api-auth", type=str, help='Set authentication for API like "username:password"; or comma-delimit multiple like "u1:p1,u2:p2,u3:p3"', default=None)
86+
parser.add_argument("--nowebui", action='store_true', help="use api=True to launch the API instead of the webui")
8787
parser.add_argument("--ui-debug-mode", action='store_true', help="Don't load model to quickly launch UI")
8888
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)
8989
parser.add_argument("--administrator", action='store_true', help="Administrator rights", default=False)
90-
parser.add_argument("--cors-allow-origins", type=str, help="Allowed CORS origins", default=None)
90+
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)
91+
parser.add_argument("--cors-allow-origins-regex", type=str, help="Allowed CORS origin(s) in the form of a single regular expression", default=None)
9192
parser.add_argument("--tls-keyfile", type=str, help="Partially enables TLS, requires --tls-certfile to fully function", default=None)
9293
parser.add_argument("--tls-certfile", type=str, help="Partially enables TLS, requires --tls-keyfile to fully function", default=None)
9394
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
@@ -113,8 +113,12 @@ def sigint_handler(sig, frame):
113113

114114

115115
def setup_cors(app):
116-
if cmd_opts.cors_allow_origins:
116+
if cmd_opts.cors_allow_origins and cmd_opts.cors_allow_origins_regex:
117+
app.add_middleware(CORSMiddleware, allow_origins=cmd_opts.cors_allow_origins.split(','), allow_origin_regex=cmd_opts.cors_allow_origins_regex, allow_methods=['*'])
118+
elif cmd_opts.cors_allow_origins:
117119
app.add_middleware(CORSMiddleware, allow_origins=cmd_opts.cors_allow_origins.split(','), allow_methods=['*'])
120+
elif cmd_opts.cors_allow_origins_regex:
121+
app.add_middleware(CORSMiddleware, allow_origin_regex=cmd_opts.cors_allow_origins_regex, allow_methods=['*'])
118122

119123

120124
def create_api(app):

0 commit comments

Comments
 (0)