Skip to content

Commit

Permalink
allow setting port, make all cli option flag only
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebCourier committed Jun 24, 2024
1 parent ca9ca82 commit ee4b59d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ jobs:
pip install build;
continue-on-error: false

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20

- name: Build the module
env:
PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
Expand Down
7 changes: 6 additions & 1 deletion guardrails_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def register_config(config: Optional[str] = None):
SourceFileLoader("config", config_file_path).load_module()


def create_app(env: Optional[str] = None, config: Optional[str] = None):
def create_app(env: Optional[str] = None, config: Optional[str] = None, port: Optional[int] = None):
if os.environ.get("APP_ENVIRONMENT") != "production":
from dotenv import load_dotenv

Expand All @@ -56,6 +56,11 @@ def create_app(env: Optional[str] = None, config: Optional[str] = None):
env_file_path = os.path.abspath(env_file)
load_dotenv(env_file_path)

set_port = port or os.environ.get("PORT", 8000)
host = os.environ.get("HOST", "http://localhost")
self_endpoint = os.environ.get("SELF_ENDPOINT", f"{host}:{set_port}")
os.environ["SELF_ENDPOINT"] = self_endpoint

register_config(config)

app = Flask(__name__)
Expand Down
22 changes: 16 additions & 6 deletions guardrails_api/cli/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ def start(
env: Optional[str] = typer.Option(
default="",
help="An env file to load environment variables from.",
prompt=".env file (optional)",
),
config: Optional[str] = typer.Option(
default="",
help="A config file to load Guards from.",
prompt="config file (optional)",
),
timeout: Optional[int] = typer.Option(
default=5,
help="Gunicorn worker timeout.",
),
threads: Optional[int] = typer.Option(
default=10,
help="Number of Gunicorn worker threads.",
),
port: Optional[int] = typer.Option(
default=8000,
help="The port to run the server on.",
),
):
# TODO: If these are empty,
Expand All @@ -43,8 +53,8 @@ def start(
config = config or None

options = {
"bind": "0.0.0.0:8000",
"timeout": 5,
"threads": 10,
"bind": f"0.0.0.0:{port}",
"timeout": timeout,
"threads": threads,
}
StandaloneApplication(create_app(env, config), options).run()
StandaloneApplication(create_app(env, config, port), options).run()
3 changes: 1 addition & 2 deletions guardrails_api/clients/cache_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ def initialize(self, app):
self.cache = Cache(
app,
config={
"CACHE_TYPE": "simple",
"CACHE_TYPE": "SimpleCache",
"CACHE_DEFAULT_TIMEOUT": 300,
"CACHE_NO_NULL_WARNING": True,
"CACHE_THRESHOLD": 50
}
)
Expand Down
3 changes: 2 additions & 1 deletion guardrails_api/default.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ PYTHONUNBUFFERED=1
LOGLEVEL="INFO"
GUARDRAILS_LOG_LEVEL="INFO"
GUARDRAILS_PROCESS_COUNT=1
SELF_ENDPOINT=http://localhost:8000
HOST=http://localhost
PORT=8000
OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES

0 comments on commit ee4b59d

Please sign in to comment.