|
1 | 1 | import click |
2 | 2 | import datetime |
| 3 | +import urllib.parse |
| 4 | + |
3 | 5 | from rich.console import Console |
4 | 6 |
|
5 | 7 | from . import flow, lib |
@@ -151,30 +153,50 @@ def evaluate(flow_name: str | None, output_dir: str | None, cache: bool = True): |
151 | 153 |
|
152 | 154 | _default_server_settings = lib.ServerSettings.from_env() |
153 | 155 |
|
| 156 | +COCOINDEX_HOST = 'https://cocoindex.io' |
| 157 | + |
154 | 158 | @cli.command() |
155 | 159 | @click.option( |
156 | 160 | "-a", "--address", type=str, default=_default_server_settings.address, |
157 | 161 | help="The address to bind the server to, in the format of IP:PORT.") |
158 | 162 | @click.option( |
159 | | - "-c", "--cors-origin", type=str, default=_default_server_settings.cors_origin, |
160 | | - help="The origin of the client (e.g. CocoInsight UI) to allow CORS from. " |
161 | | - "e.g. `http://cocoindex.io` if you want to allow CocoInsight to access the server.") |
| 163 | + "-c", "--cors-origin", type=str, |
| 164 | + default=_default_server_settings.cors_origins and ','.join(_default_server_settings.cors_origins), |
| 165 | + help="The origins of the clients (e.g. CocoInsight UI) to allow CORS from. " |
| 166 | + "Multiple origins can be specified as a comma-separated list. " |
| 167 | + "e.g. `https://cocoindex.io,http://localhost:3000`") |
| 168 | +@click.option( |
| 169 | + "-ci", "--cors-cocoindex", is_flag=True, show_default=True, default=False, |
| 170 | + help=f"Allow {COCOINDEX_HOST} to access the server.") |
| 171 | +@click.option( |
| 172 | + "-cl", "--cors-local", type=int, |
| 173 | + help=f"Allow http://localhost:<port> to access the server.") |
162 | 174 | @click.option( |
163 | 175 | "-L", "--live-update", is_flag=True, show_default=True, default=False, |
164 | 176 | help="Continuously watch changes from data sources and apply to the target index.") |
165 | 177 | @click.option( |
166 | 178 | "-q", "--quiet", is_flag=True, show_default=True, default=False, |
167 | 179 | help="Avoid printing anything to the standard output, e.g. statistics.") |
168 | | -def server(address: str, live_update: bool, quiet: bool, cors_origin: str | None): |
| 180 | +def server(address: str, live_update: bool, quiet: bool, cors_origin: str | None, |
| 181 | + cors_cocoindex: bool, cors_local: int | None): |
169 | 182 | """ |
170 | 183 | Start a HTTP server providing REST APIs. |
171 | 184 |
|
172 | 185 | It will allow tools like CocoInsight to access the server. |
173 | 186 | """ |
174 | | - lib.start_server(lib.ServerSettings(address=address, cors_origin=cors_origin)) |
| 187 | + cors_origins : set[str] = set() |
| 188 | + if cors_origin is not None: |
| 189 | + cors_origins.update(s for o in cors_origin.split(',') if (s:= o.strip()) != '') |
| 190 | + if cors_cocoindex: |
| 191 | + cors_origins.add(COCOINDEX_HOST) |
| 192 | + if cors_local is not None: |
| 193 | + cors_origins.add(f"http://localhost:{cors_local}") |
| 194 | + lib.start_server(lib.ServerSettings(address=address, cors_origins=list(cors_origins))) |
175 | 195 | if live_update: |
176 | 196 | options = flow.FlowLiveUpdaterOptions(live_mode=True, print_stats=not quiet) |
177 | 197 | execution_context.run(flow.update_all_flows(options)) |
| 198 | + if COCOINDEX_HOST in cors_origins: |
| 199 | + click.echo(f"Open CocoInsight at: {COCOINDEX_HOST}/cocoinsight") |
178 | 200 | input("Press Enter to stop...") |
179 | 201 |
|
180 | 202 |
|
|
0 commit comments