Skip to content

Commit

Permalink
Port 4256 allow setting the application port through the cli config (#11
Browse files Browse the repository at this point in the history
)

Added a way to pass port & initialize-port-resources through the cli
  • Loading branch information
yairsimantov20 authored Jul 18, 2023
1 parent 478a34a commit 2b00fb2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 17 deletions.
30 changes: 25 additions & 5 deletions port_ocean/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,31 @@ def version(short: bool) -> None:
type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]),
default="DEBUG",
help="""Set the logging level for the integration.
Supported levels are DEBUG, INFO, WARNING, ERROR,
and CRITICAL. If not specified, the default level
is DEBUG.""",
Supported levels are DEBUG, INFO, WARNING, ERROR,
and CRITICAL. If not specified, the default level
is DEBUG.""",
)
def sail(path: str, log_level: LogLevelType) -> None:
@click.option(
"-p",
"--port",
"port",
type=int,
default=8000,
help="""Set the port for the integration to run on.
If not specified, the default port is 8000.""",
)
@click.option(
"-i",
"--initialize-port-resources",
"initialize_port_resources",
type=bool,
default=False,
help="""Set to true to create default resources on installation.
If not specified, the default value is false.""",
)
def sail(
path: str, log_level: LogLevelType, port: int, initialize_port_resources: bool
) -> None:
"""
Runs the integration in the given PATH. if no PATH is provided, the current directory will be used.
Expand All @@ -82,7 +102,7 @@ def sail(path: str, log_level: LogLevelType) -> None:
print_logo()

console.print("Setting sail... ⛵️⚓️⛵️⚓️ All hands on deck! ⚓️")
run(path, log_level)
run(path, log_level, port, initialize_port_resources)


@cli_start.command()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.gitignore
11 changes: 7 additions & 4 deletions port_ocean/config/integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any, Literal

from pydantic import BaseModel, Field, BaseSettings
from pydantic import Field, BaseSettings

from port_ocean.config.base import BaseOceanSettings
from port_ocean.core.event_listener import EventListenerSettingsType
Expand Down Expand Up @@ -31,6 +31,9 @@ class IntegrationConfiguration(BaseOceanSettings):
LogLevelType = Literal["ERROR", "WARNING", "INFO", "DEBUG", "CRITICAL"]


class LoggerConfiguration(BaseModel):
level: LogLevelType = "DEBUG"
serialize: bool = False
class ApplicationSettings(BaseSettings):
log_level: LogLevelType = "DEBUG"
port: int = 8000

class Config:
env_prefix = "APPLICATION_"
6 changes: 2 additions & 4 deletions port_ocean/logger_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

from loguru import logger

from port_ocean.config.integration import LoggerConfiguration, LogLevelType
from port_ocean.config.integration import LogLevelType


def setup_logger(level: LogLevelType) -> None:
settings = LoggerConfiguration(level=level)
logger_format = (
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
"<level>{level: <8}</level> | "
Expand All @@ -16,9 +15,8 @@ def setup_logger(level: LogLevelType) -> None:
logger.remove()
logger.add(
sys.stdout,
level=settings.level.upper(),
level=level.upper(),
format=logger_format,
serialize=settings.serialize,
enqueue=True, # process logs in background
diagnose=False, # hide variable values in log backtrace
)
21 changes: 17 additions & 4 deletions port_ocean/ocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

from port_ocean.clients.port.client import PortClient
from port_ocean.config.dynamic import default_config_factory
from port_ocean.config.integration import IntegrationConfiguration, LogLevelType
from port_ocean.config.integration import (
IntegrationConfiguration,
LogLevelType,
ApplicationSettings,
)
from port_ocean.context.ocean import (
PortOceanContext,
ocean,
Expand Down Expand Up @@ -95,8 +99,15 @@ async def startup() -> None:
await self.fast_api_app(scope, receive, send)


def run(path: str = ".", log_level: LogLevelType = "DEBUG") -> None:
setup_logger(log_level)
def run(
path: str = ".",
log_level: LogLevelType = "DEBUG",
port: int = 8000,
initialize_port_resources: bool = False,
) -> None:
application_settings = ApplicationSettings(log_level=log_level, port=port)

setup_logger(application_settings.log_level)
sys.path.append(".")
try:
integration_path = f"{path}/integration.py" if path else "integration.py"
Expand All @@ -119,9 +130,11 @@ def run(path: str = ".", log_level: LogLevelType = "DEBUG") -> None:
"app", default_app
)

# Override config with arguments
app.config.initialize_port_resources = initialize_port_resources
if app.config.initialize_port_resources:
initialize_defaults(
app.integration.AppConfigHandlerClass.CONFIG_CLASS, app.config
)

uvicorn.run(app, host="0.0.0.0", port=8000)
uvicorn.run(app, host="0.0.0.0", port=application_settings.port)

0 comments on commit 2b00fb2

Please sign in to comment.