API's giving 404.
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
Additional context Add any other context about the problem here.
Hey, @rithin, I'll take a look and get back to you as soon as possible!
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.
@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 "
create_superuser no logs but container is starting and exiting. is that the expected behaviour?
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?
@igorbenav you can find it here -> https://github.com/rithin/kappitrails_api/tree/development_v1
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
@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
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
Same, it's started and exited.
Logs
2024-02-22 16:57:19 ERROR:main:Error creating tier:
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
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.
@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: - ./src/.env volumes: - postgres-data:/var/lib/postgresql/data expose: - "5432" ports: - 5432:5432 web: build: context: . dockerfile: Dockerfile command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload env_file: - ./src/.env ports: - "8000:8000"
depends_on:
- db
- redis
volumes:
- ./src/app:/code/app
- ./src/.env:/code/.env
worker: build: context: . dockerfile: Dockerfile command: arq app.core.worker.settings.WorkerSettings env_file: - ./src/.env depends_on: - db - redis volumes: - ./src/app:/code/app - ./src/.env:/code/.env
redis: image: redis:alpine volumes: - redis-data:/data expose: - "6379"
pgadmin: container_name: pgadmin4 image: dpage/pgadmin4:latest restart: always ports: - "5050:80" volumes: - pgadmin-data:/var/lib/pgadmin env_file: - ./src/.env depends_on: - db
create_superuser: build: context: . dockerfile: Dockerfile env_file: - ./src/.env depends_on: - db - web command: python -m src.scripts.create_first_superuser volumes: - ./src:/code/src
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
volumes: postgres-data: redis-data: pgadmin-data:
@rithin were you able to fix this? Sorry, I missed the last reply
@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.