Skip to content

Commit

Permalink
feat: added --cors-origin cli param to gptme-server
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Nov 3, 2024
1 parent 970fc47 commit a7190ed
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
13 changes: 11 additions & 2 deletions gptme/server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import flask
from flask import current_app, request
from flask_cors import CORS

from ..commands import execute_cmd
from ..dirs import get_logs_dir
Expand Down Expand Up @@ -165,8 +166,16 @@ def favicon():
return flask.send_from_directory(media_path, "logo.png")


def create_app() -> flask.Flask:
"""Create the Flask app."""
def create_app(cors_origin: str | None = None) -> flask.Flask:
"""Create the Flask app.
Args:
cors_origin: CORS origin to allow. Use '*' to allow all origins.
"""
app = flask.Flask(__name__, static_folder=static_path)
app.register_blueprint(api)

if cors_origin:
CORS(app, resources={r"/api/*": {"origins": cors_origin}})

return app
8 changes: 7 additions & 1 deletion gptme/server/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@
help="Port to run the server on.",
)
@click.option("--tools", default=None, help="Tools to enable, comma separated.")
@click.option(
"--cors-origin",
default=None,
help="CORS origin to allow. Use '*' to allow all origins.",
)
def main(
debug: bool,
verbose: bool,
model: str | None,
host: str,
port: str,
tools: str | None,
cors_origin: str | None,
): # pragma: no cover
"""
Starts a server and web UI for gptme.
Expand All @@ -58,5 +64,5 @@ def main(
exit(1)
click.echo("Initialization complete, starting server")

app = create_app()
app = create_app(cors_origin=cors_origin)
app.run(debug=debug, host=host, port=int(port))
22 changes: 18 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pillow = {version = "*", optional=true}

# server
flask = {version = "^3.0", optional=true}
flask-cors = {version = "^4.0", optional=true}

[tool.poetry.group.dev.dependencies]
# lint
Expand Down Expand Up @@ -83,13 +84,13 @@ types-tabulate = "*"
types-lxml = "*"

[tool.poetry.extras]
server = ["flask"]
server = ["flask", "flask-cors"]
browser = ["playwright"]
datascience = ["matplotlib", "pandas", "numpy", "pillow"]
computer = ["python-xlib", "pillow"] # pillow already in datascience but listed for clarity
all = [
# server
"flask",
"flask", "flask-cors",
# browser
"playwright",
# datascience
Expand Down

0 comments on commit a7190ed

Please sign in to comment.