igorbenav / FastAPI-boilerplate

An extendable async API using FastAPI, Pydantic V2, SQLAlchemy 2.0, PostgreSQL and Redis.
MIT License
589 stars 69 forks source link

API's giving 404. #120

Open rithin opened 7 months ago

rithin commented 7 months ago

API's giving 404. I build using docker-compose

My latest code can see here-> https://github.com/rithin/kappitrails_api/tree/development_v1

Description only apis are working if i wrote in the main.py but what in api/v1/users.py or any other

Screenshots image

image image

Additional context Add any other context about the problem here.

igorbenav commented 7 months ago

Hey, @rithin, I'll take a look and get back to you as soon as possible!

igorbenav commented 7 months ago

Ok, found the issue. It's this bug: #117 which was fixed in #118.

All you need to do is add this part: application.include_router(router) in the if isinstance(settings, EnvironmentSettings): condition in app/core/setup.py

if isinstance(settings, EnvironmentSettings):
    if settings.ENVIRONMENT != EnvironmentOption.PRODUCTION:
        docs_router = APIRouter()
        if settings.ENVIRONMENT != EnvironmentOption.LOCAL:
            docs_router = APIRouter(dependencies=[Depends(get_current_superuser)])
        ...
        ...
        application.include_router(docs_router)

        # --------------- adding this line fixes the bug ---------------
        application.include_router(router)

Tell me if it works for you.

rithin commented 7 months ago

@igorbenav Thanks for the quick reply. That was the issue and it's resolved.

I'm getting one more issue like create_tier and create_superuser containers are creating and exiting In the user table, I can see the admin entry but in the tier table, there is no entry.

logs-create_tier asyncio.exceptions.CancelledError 2024-02-21 07:51:19 2024-02-21 07:51:19 The above exception was the direct cause of the following exception: 2024-02-21 07:51:19 2024-02-21 07:51:19 Traceback (most recent call last): 2024-02-21 07:51:19 File "", line 198, in _run_module_as_main 2024-02-21 07:51:19 File "", line 88, in _run_code 2024-02-21 07:51:19 File "/code/src/scripts/create_first_tier.py", line 30, in 2024-02-21 07:51:19 loop.run_until_complete(main()) 2024-02-21 07:51:19 File "/usr/local/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete 2024-02-21 07:51:19 return future.result() 2024-02-21 07:51:19 ^^^^^^^^^^^^^^^ 2024-02-21 07:51:19 File "/code/src/scripts/create_first_tier.py", line 25, in main 2024-02-21 07:51:19 await create_first_tier(session) 2024-02-21 07:51:19 File "/code/src/scripts/create_first_tier.py", line 14, in create_first_tier 2024-02-21 07:51:19 result = await session.execute(query)


create_superuser no logs but container is starting and exiting. is that the expected behaviour?

igorbenav commented 7 months ago

Weird. There are actually no logs indeed, but the tier should be created. I'm running it here and it's creating. Can you please share your docker-compose, Dockerfile and the relevant parts of your .env?

rithin commented 7 months ago

@igorbenav you can find it here -> https://github.com/rithin/kappitrails_api/tree/development_v1

igorbenav commented 7 months ago

I think you forgot to uncomment this part in docker-compose:

# #   #-------- uncomment to create first tier --------
#   create_tier:
#     build:
#       context: .
#       dockerfile: Dockerfile
#     env_file:
#       - ./src/.env
#     depends_on:
#       - db
#       - web
#     command: python -m src.scripts.create_first_tier
#     volumes:
#       - ./src:/code/src

If you want logging, the latest update I merged adds it to the scripts. Just replace the old scripts with the new ones

rithin commented 7 months ago

@igorbenav sorry i didn't push the latest code to git. I enabled and the container got created. But its keep exiting Now i have pushed the code -> https://github.com/rithin/kappitrails_api/tree/development_v1 image

igorbenav commented 7 months ago

Weird. Can you please replace the script with the new one and try again so we can see the logs? Run a docker compose build --no-cache, then docker compose up

rithin commented 7 months ago

Same, it's started and exited. Logs 2024-02-22 16:57:19 ERROR:main:Error creating tier: image image

igorbenav commented 7 months ago

I can't reproduce the error, so it might be something with your network setup. Something I see that you can fix is this:

# app/core/setup.py

def create_application(
    router: APIRouter,
    settings: (
        DatabaseSettings
        | RedisCacheSettings
        | AppSettings
        | ClientSideCacheSettings
        | RedisQueueSettings
        | RedisRateLimiterSettings
        | EnvironmentSettings
    ),
    create_tables_on_start: bool = True,
    **kwargs: Any,
) -> FastAPI:
...

    application = FastAPI(lifespan=lifespan, **kwargs)
    # --------- add the following line here ---------
    application.include_router(router)

This was fixed in #118

Edit: we went through this already, sorry

igorbenav commented 7 months ago

Yeah, when I remove the networks part in docker-compose it works:

kappitrails_api-development_v1-create_tier-1       | INFO:__main__:Tier 'free' created successfully.
kappitrails_api-development_v1-create_tier-1 exited with code 0

So there's something wrong with your docker-compose networks settings, when you let docker do it automatically it works. Not actually a boilerplate issue, but I'll leave it open for now to see if you can fix, then maybe convert into a discussion.

rithin commented 7 months ago

@igorbenav , thanks for reply. When I remove the network setting from docker compose am get another error.

File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 734, in lifespan 2024-02-24 11:10:13 async with self.lifespan_context(app) as maybe_state: 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/contextlib.py", line 210, in aenter 2024-02-24 11:10:13 return await anext(self.gen) 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/code/app/core/setup.py", line 92, in lifespan 2024-02-24 11:10:13 await create_tables() 2024-02-24 11:10:13 File "/code/app/core/setup.py", line 34, in create_tables 2024-02-24 11:10:13 async with engine.begin() as conn: 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/contextlib.py", line 210, in aenter 2024-02-24 11:10:13 return await anext(self.gen) 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/ext/asyncio/engine.py", line 1054, in begin 2024-02-24 11:10:13 async with conn: 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/ext/asyncio/base.py", line 121, in aenter 2024-02-24 11:10:13 return await self.start(is_ctxmanager=True) 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/ext/asyncio/engine.py", line 270, in start 2024-02-24 11:10:13 await greenlet_spawn(self.sync_engine.connect) 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 197, in greenlet_spawn 2024-02-24 11:10:13 result = context.throw(sys.exc_info()) 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 3258, in connect 2024-02-24 11:10:13 return self._connection_cls(self) 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 145, in init 2024-02-24 11:10:13 self._dbapi_connection = engine.raw_connection() 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 3282, in raw_connection 2024-02-24 11:10:13 return self.pool.connect() 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 449, in connect 2024-02-24 11:10:13 return _ConnectionFairy._checkout(self) 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 1263, in _checkout 2024-02-24 11:10:13 fairy = _ConnectionRecord.checkout(pool) 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 712, in checkout 2024-02-24 11:10:13 rec = pool._do_get() 2024-02-24 11:10:13 ^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/pool/impl.py", line 168, in _do_get 2024-02-24 11:10:13 with util.safe_reraise(): 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 146, in exit 2024-02-24 11:10:13 raise exc_value.with_traceback(exc_tb) 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/pool/impl.py", line 166, in _do_get 2024-02-24 11:10:13 return self._create_connection() 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 390, in _create_connection 2024-02-24 11:10:13 return _ConnectionRecord(self) 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 674, in init 2024-02-24 11:10:13 self.connect() 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 900, in connect 2024-02-24 11:10:13 with util.safe_reraise(): 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 146, in exit 2024-02-24 11:10:13 raise exc_value.with_traceback(exc_tb) 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 896, in __connect 2024-02-24 11:10:13 self.dbapi_connection = connection = pool._invoke_creator(self) 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/create.py", line 643, in connect 2024-02-24 11:10:13 return dialect.connect(cargs, cparams) 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/default.py", line 616, in connect 2024-02-24 11:10:13 return self.loaded_dbapi.connect(*cargs, *cparams) 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 937, in connect 2024-02-24 11:10:13 await_only(creator_fn(arg, kw)), 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 127, in await_only 2024-02-24 11:10:13 return current.driver.switch(awaitable) # type: ignore[no-any-return] 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 192, in greenlet_spawn 2024-02-24 11:10:13 value = await result 2024-02-24 11:10:13 ^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/asyncpg/connection.py", line 2329, in connect 2024-02-24 11:10:13 return await connect_utils._connect( 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/asyncpg/connect_utils.py", line 1017, in _connect 2024-02-24 11:10:13 raise last_error or exceptions.TargetServerAttributeNotMatched( 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/asyncpg/connect_utils.py", line 991, in _connect 2024-02-24 11:10:13 conn = await _connect_addr( 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/asyncpg/connect_utils.py", line 828, in _connect_addr 2024-02-24 11:10:13 return await connect_addr(params, True, *args) 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/asyncpg/connect_utils.py", line 873, in connect_addr 2024-02-24 11:10:13 tr, pr = await connector 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "/usr/local/lib/python3.11/site-packages/asyncpg/connect_utils.py", line 744, in _create_ssl_connection 2024-02-24 11:10:13 tr, pr = await loop.create_connection( 2024-02-24 11:10:13 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-02-24 11:10:13 File "uvloop/loop.pyx", line 2042, in create_connection 2024-02-24 11:10:13 OSError: Multiple exceptions: [Errno 111] Connection refused, [Errno 99] Cannot assign requested address

version: '3.8' services: db: image: postgres:13 env_file:

volumes: postgres-data: redis-data: pgadmin-data:

igorbenav commented 6 months ago

@rithin were you able to fix this? Sorry, I missed the last reply

rithin commented 6 months ago

@igorbenav no, still create_superuser-1 and create_tier-1 not running and trhowing error.

2024-03-10 16:52:06 INFO:main:Admin user rithin already exists. 2024-03-17 17:12:47 INFO:main:Admin user rithin already exists.

2024-03-10 16:53:05 ERROR:main:Error creating tier: 2024-03-17 17:13:46 ERROR:main:Error creating tier:

create_superuser I guess it is a valid error, there is one user called "rithin" already there as a superuser. But surprisingly it's starting and exiting.