Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI] Merge patch-atomic-adding-tursodb-01-07-2025-1736301866 into dev #3682

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/pydiscordsh/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ __pycache__/
*.log
.git
node_modules
tests
tests
*.env
28 changes: 26 additions & 2 deletions apps/pydiscordsh/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
from fastapi import FastAPI, WebSocket
from pydiscordsh import Routes, CORS, TursoDatabase
from contextlib import asynccontextmanager

import logging

logger = logging.getLogger("uvicorn")

app = FastAPI()

@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("[DB]@PENDING")
await database.start_client()
yield
logger.info("[DB]@DISINT")
await database.stop_client()
logger.info("[DB]@STOPPING")

app = FastAPI(lifespan=lifespan)
routes = Routes(app, templates_dir="templates")
CORS(app)

routes.get("/", TursoDatabase, "start_client")
database = TursoDatabase()


@app.get("/v1/db/start_client")
async def start_client():
return await database.start_client()

@app.get("/v1/db/stop_client")
async def stop_client():
return await database.stop_client()

@app.get("/v1/db/status_client")
async def status_client():
return await database.status_client()
27 changes: 26 additions & 1 deletion apps/pydiscordsh/poetry.lock

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

16 changes: 16 additions & 0 deletions apps/pydiscordsh/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@
}
}
}
},
"orb": {
"executor": "nx:run-commands",
"options": {
"commands": [
{
"command": "pnpm nx container pydiscordsh",
"forwardAllArgs": false
},
{
"command": "docker run --env-file .env -p 3000:3000 kbve/pydiscordsh:1.42",
"forwardAllArgs": false
}
],
"parallel": false
}
}
},
"tags": [],
Expand Down
57 changes: 51 additions & 6 deletions apps/pydiscordsh/pydiscordsh/apps/turso.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,61 @@
import logging
import os
import libsql_experimental as libsql

logger = logging.getLogger("uvicorn")

class TursoDatabase:
async def start_client(self):
return {"status": 200, "message": "Client started successfully."}
def __init__(self):
self.conn = None

def initialize_connection(self):
"""Initialize the database connection."""
url = os.getenv("TURSO_DATABASE_URL")
auth_token = os.getenv("TURSO_AUTH_TOKEN")

if not url or not auth_token:
raise ValueError("TURSO_DATABASE_URL or TURSO_AUTH_TOKEN is not set in environment variables.")

# Initialize the database connection
self.conn = libsql.connect("hello.db", sync_url=url, auth_token=auth_token)
self.conn.sync()
logger.info("Database connection initialized.")

async def start_client(self):
try:
self.initialize_connection()
logger.info("Database client started.")
return {"status": 200, "message": "Client started successfully."}
except Exception as e:
logger.error(f"Error starting client: {e}")
return {"status": 500, "message": f"Error starting client: {e}"}

async def stop_client(self):
return {"status": 200, "message": "Client stopped successfully."}
try:
if self.conn:
self.conn = None
logger.info("Database client stopped.")
return {"status": 200, "message": "Client stopped successfully."}
else:
return {"status": 400, "message": "No active client to stop."}
except Exception as e:
logger.error(f"Error stopping client: {e}")
return {"status": 500, "message": f"Error stopping client: {e}"}

async def status_client(self):
return {"status": 200, "message": "Client is running."}
if self.conn:
return {"status": 200, "message": "Client is running."}
else:
return {"status": 400, "message": "Client is not running."}

async def close(self):
logger.info("Closing the database connection.")
return {"status": 200, "message": "Database connection closed."}
try:
if self.conn:
self.conn = None
logger.info("Closing the database connection.")
return {"status": 200, "message": "Database connection closed."}
else:
return {"status": 400, "message": "No active connection to close."}
except Exception as e:
logger.error(f"Error closing the database connection: {e}")
return {"status": 500, "message": f"Error closing the database connection: {e}"}
1 change: 1 addition & 0 deletions apps/pydiscordsh/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ readme = 'README.md'
supabase = "^2.11.0"
uvicorn = "^0.34.0"
jinja2 = "^3.1.5"
libsql-experimental = "^0.0.41"

[tool.poetry.group.dev.dependencies]
autopep8 = "2.3.1"
Expand Down
Loading