-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"""FastAPI app. | ||
Connect all routers to the app and add error handling. | ||
""" | ||
|
||
from fastapi import FastAPI, Request | ||
from fastapi.responses import JSONResponse | ||
from fastapi.middleware import Middleware | ||
from starlette.middleware.cors import CORSMiddleware | ||
|
||
from tansu.events.log import logger | ||
from tansu.events.routers import system, events | ||
|
||
ORIGINS = [ | ||
"https://testnet.tansu.dev", | ||
"https://app.tansu.dev", | ||
] | ||
|
||
|
||
async def unhandled_exception_handler(request: Request, exc: Exception) -> JSONResponse: | ||
"""Log all unhandled exceptions.""" | ||
message = f"Internal API error: {exc}" | ||
logger.error(message) | ||
return JSONResponse( | ||
status_code=500, | ||
content={"message": "Internal API error. The error was reported to our team."}, | ||
) | ||
|
||
|
||
def create_app(debug: bool = False): | ||
app = FastAPI( | ||
title="Tansu - events backend", | ||
debug=debug, | ||
description="", | ||
version="1.0.0", | ||
middleware=[ | ||
Middleware( | ||
CORSMiddleware, | ||
**{ | ||
"allow_origins": ORIGINS, | ||
"allow_credentials": True, | ||
"allow_methods": ["GET", "POST", "DELETE", "OPTIONS"], | ||
"allow_headers": ["*"], | ||
}, | ||
), | ||
], | ||
) | ||
|
||
app.add_exception_handler(Exception, unhandled_exception_handler) | ||
|
||
app.include_router(system.router, tags=["system"]) | ||
app.include_router(events.router, tags=["events"]) | ||
|
||
logger.info("Tansu RPC is running...") | ||
|
||
return app | ||
|
||
|
||
if __name__ == "__main__": | ||
import uvicorn | ||
|
||
uvicorn.run( | ||
"tansu.event.main:app", | ||
host="127.0.0.1", | ||
reload=True, | ||
port=8080, | ||
access_log=True, | ||
log_level="debug", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import sqlalchemy | ||
|
||
from tansu.events import models | ||
from tansu.events import api_models | ||
from tansu.events.database import db_models | ||
from tansu.events.log import logger | ||
|
||
|
||
@sqlalchemy.event.listens_for(db_models.Event, "after_insert") | ||
def event_handler(mapper, connection, target: db_models.Event): | ||
event = models.Event( | ||
event = api_models.Event( | ||
project_key=target.project_key, action=target.action, value=target.value | ||
) | ||
|
||
logger.info( | ||
f"Event listener: {event.project_key} :: {event.action} :: {event.value}" | ||
logger.debug( | ||
f"Event listener: {event.project_key} :: {event.action} :: {event.value} :: {event.ledger}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import logging | ||
|
||
logger = logging.getLogger("soroban-versioning-events-events") | ||
logger = logging.getLogger("tansu-events") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"""Main entry point for the FastAPI server.""" | ||
|
||
import os | ||
from tansu.events.app import create_app | ||
|
||
env = os.getenv("ENV", "production") | ||
debug = True if env == "testing" else False | ||
app = create_app(debug=debug) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from fastapi import APIRouter | ||
|
||
from tansu.events.database import SessionFactory, db_models | ||
from tansu.events import api_models | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/events") | ||
async def events(request: api_models.EventRequest) -> list[api_models.Event | None]: | ||
async with SessionFactory() as session: | ||
events_ = ( | ||
session.query(db_models.Event) | ||
.where(db_models.Event.project_key == request.project_key) | ||
.where(db_models.Event.action == request.action) | ||
.limit(request.limit) | ||
.all() | ||
) | ||
|
||
result = [ | ||
api_models.Event( | ||
project_key=event_.project_key, action=event_.action, value=event_.value | ||
) | ||
for event_ in events_ | ||
] | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from fastapi import APIRouter | ||
from fastapi.responses import PlainTextResponse | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/favicon.ico") | ||
@router.get("/health") | ||
@router.get("/") | ||
async def health_check() -> PlainTextResponse: | ||
return PlainTextResponse("OK") |