Skip to content

Commit

Permalink
Change only starlette
Browse files Browse the repository at this point in the history
  • Loading branch information
talsabagport committed Nov 3, 2024
1 parent 438d8ed commit 295564d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 30 deletions.
14 changes: 14 additions & 0 deletions integrations/jira/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import json
from enum import StrEnum
from typing import Any

from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route

from jira.client import JiraClient
from loguru import logger
from port_ocean.context.ocean import ocean
Expand Down Expand Up @@ -59,6 +64,15 @@ async def on_resync_issues(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
yield issues


async def handle_webhook_request(request: Request) -> JSONResponse:
return JSONResponse({"ok": True})


ocean.router.routes.append(
Route("/webhook", methods=["post"], endpoint=handle_webhook_request)
)


# Called once when the integration starts.
@ocean.on_start()
async def on_start() -> None:
Expand Down
5 changes: 5 additions & 0 deletions port_ocean/context/ocean.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Callable, TYPE_CHECKING, Any, Literal, Union

from pydantic.main import BaseModel
from starlette.routing import Router
from werkzeug.local import LocalProxy

from port_ocean.clients.port.types import UserAgentType
Expand Down Expand Up @@ -43,6 +44,10 @@ def initialized(self) -> bool:
def config(self) -> "IntegrationConfiguration":
return self.app.config

@property
def router(self) -> Router:
return self.app.integration_router

@property
def integration(self) -> "BaseIntegration":
return self.app.integration
Expand Down
61 changes: 32 additions & 29 deletions port_ocean/ocean.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import asyncio
import contextlib
import sys
import threading
from typing import Callable, Any, Dict, Type
from typing import Callable, Any, Dict, AsyncIterator, Type

from fastapi import FastAPI, APIRouter
from loguru import logger
from pydantic import BaseModel
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.responses import JSONResponse
from starlette.routing import Route
from starlette.routing import Route, Mount, Router
from starlette.types import Scope, Receive, Send

from port_ocean.clients.port.client import PortClient
Expand All @@ -27,45 +30,21 @@
from port_ocean.utils.repeat import repeat_every
from port_ocean.utils.signal import signal_handler
from port_ocean.version import __integration_version__
import contextlib

from starlette.applications import Starlette


class Ocean:
def __init__(
self,
app: Starlette | None = None,
integration_class: Callable[[PortOceanContext], BaseIntegration] | None = None,
integration_router: None = None,
integration_router: Router | None = None,
config_factory: Type[BaseModel] | None = None,
config_override: Dict[str, Any] | None = None,
):
initialize_port_ocean_context(self)

@contextlib.asynccontextmanager
async def lifespan(app):
try:
await self.integration.start()
await self._setup_scheduled_resync()
yield None
except Exception:
logger.exception("Integration had a fatal error. Shutting down.")
sys.exit("Server stopped")
finally:
signal_handler.exit()

async def handle_webhook_request(data: dict[str, Any]) -> dict[str, Any]:
return JSONResponse({"ok": True})

self.starlette_app = Starlette(
routes=[
Route("/docs", endpoint=handle_webhook_request),
Route("/integration/webhook", endpoint=handle_webhook_request),
],
# middleware=[Middleware(RequestHandlerMiddleware)],
lifespan=lifespan,
)
self.starlette_app = app or Starlette()
self.integration_router = integration_router or Router()

self.config = IntegrationConfiguration(
# type: ignore
Expand Down Expand Up @@ -137,4 +116,28 @@ async def execute_resync_all() -> None:
await repeated_function()

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
@contextlib.asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
try:
await self.integration.start()
await self._setup_scheduled_resync()
yield None
except Exception:
logger.exception("Integration had a fatal error. Shutting down.")
sys.exit("Server stopped")
finally:
signal_handler.exit()

async def health() -> JSONResponse:
return JSONResponse({"ok": True})

self.starlette_app = Starlette(
routes=[
Route("/docs", endpoint=health),
Mount("/integration", routes=self.integration_router.routes),
],
middleware=[Middleware(RequestHandlerMiddleware)],
lifespan=lifespan,
)

await self.starlette_app(scope, receive, send)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "port-ocean"
version = "0.12.2-dev21"
version = "0.12.2-dev22"
description = "Port Ocean is a CLI tool for managing your Port projects."
readme = "README.md"
homepage = "https://app.getport.io"
Expand Down

0 comments on commit 295564d

Please sign in to comment.