how to update this endpoint to optionally I can get records from database with specific…
- User
how to update this endpoint to optionally I can get records from database with specific request_url, esponse_status_code, response_error_code, phone_id logs = Table( "logs", metadata, Column("id", Integer, primary_key=True), Column("request_datetime", TIMESTAMP(timezone=True), nullable=False), Column("request_url", Text, nullable=False), Column("request_method", String(8), nullable=False), Column("response_datetime", TIMESTAMP(timezone=True), nullable=False), Column("response_status_code", SmallInteger, nullable=False), Column("response_error_code", String(10), nullable=True), Column("phone_id", Uuid, nullable=True), Column("duration", INTERVAL, nullable=False), ) @router.get('/get-log') async def get_log(limit: Optional[int] = 10, session: AsyncSession = Depends(get_async_session)) -> List: """ Get logs from database
code- **limit**: number of logs to get from database (default=10) """ query = select(logs).limit(limit) result = await session.execute(query) # Convert the result to a list of dictionaries results_list = [dict(zip(result.keys(), row)) for row in result.fetchall()] return results_list - Assistant
To update the endpoint to optionally get records with specific request_url, response_status_code, response_error_code, and phone_id, modify the endpoint function code as follows:
python - User
I have main.py with this code import uvicorn from fastapi import FastAPI
from config import HOST, PORT from src.logging.router import router
app = FastAPI(title="Logging") app.include_router(router)
@app.get("/health") def health() -> str: """ Endpoint to check if the app works """ return "App is working"
if name == "main": uvicorn.run("main:app", host=HOST, port=int(PORT), reload=True) I moved it to src director. Now when I run uvicorn main:app --reload I got en error Error loading ASGI app. Could not import module "src/main".
- Assistant
The error message indicates that Uvicorn was unable to import your FastAPI app from the module
src.main. This is most likely because the file path or module name has changed, but theuvicorncommand has not been updated accordingly.To fix this error, try running the
uvicorncommand from the directory containing thesrcfolder. Assuming yourmain.pyfile is now located insrc/main.py, the command to start the server would be:codeuvicorn src.main:app --reloadThis tells
uvicornto look for theappinstance in themainmodule, which is in thesrcpackage.Make sure you are running the command in the correct directory. Also, check if the packages that your application depends on are installed.
- User
how to stop uvicorn
- Assistant
To stop a running Uvicorn server, you can use one of the following methods:
-
Press
Ctrl+Cin the terminal or command prompt where the server is running. This should send a SIGINT signal to the process, which will cause it to shut down gracefully. -
If the terminal or command prompt where the server is running is closed or cannot receive the SIGINT signal for some reason, you can find the process ID (PID) of the server and use a system-specific command to terminate it. On Unix-like systems, you can use the
killcommand followed by the PID. For example:
codekill 1234where
1234is the PID of the Uvicorn process.On Windows, you can use the
taskkillcommand followed by the PID. For example:codetaskkill /pid 1234 /fwhere
/pidis the PID of the Uvicorn process and/fforces the process to be terminated.Note that forcibly terminating a running Uvicorn process may result in data loss or other unexpected behavior, so it is recommended to stop the server gracefully if possible (i.e. by using
Ctrl+Cor a similar method). -