You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+15-11Lines changed: 15 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -265,6 +265,7 @@ poetry install
265
265
Ensuring it ran without any problem.
266
266
267
267
#### 4.2.2. Running PostgreSQL With Docker
268
+
> [!NOTE]
268
269
> If you already have a PostgreSQL running, you may skip this step.
269
270
270
271
Install docker if you don't have it yet, then run:
@@ -293,6 +294,7 @@ docker run -d \
293
294
```
294
295
295
296
#### 4.2.3. Running redis With Docker
297
+
> [!NOTE]
296
298
> If you already have a redis running, you may skip this step.
297
299
298
300
Install docker if you don't have it yet, then run:
@@ -321,12 +323,13 @@ While in the `src` folder, run to start the application with uvicorn server:
321
323
```sh
322
324
poetry run uvicorn app.main:app --reload
323
325
```
326
+
> [!TIP]
324
327
> The --reload flag enables auto-reload once you change (and save) something in the project
325
328
326
329
### 4.3 Creating the first superuser
327
330
#### 4.3.1 Docker Compose
328
331
329
-
> **Warning**
332
+
> [!WARNING]
330
333
> Make sure DB and tables are created before running create_superuser (db should be running and the api should run at least once before)
331
334
332
335
If you are using docker compose, you should uncomment this part of the docker-compose.yml:
@@ -384,7 +387,7 @@ poetry run python -m scripts.create_first_superuser
384
387
385
388
### 4.3.3 Creating the first tier
386
389
387
-
> **Warning**
390
+
> [!WARNING]
388
391
> Make sure DB and tables are created before running create_tier (db should be running and the api should run at least once before)
389
392
390
393
To create the first tier it's similar, you just replace `create_superuser` for `create_tier` service or `create_first_superuser` to `create_first_tier` for scripts. If using `docker compose`, do not forget to uncomment the `create_tier` service in `docker-compose.yml`.
@@ -400,6 +403,7 @@ And to apply the migration
400
403
poetry run alembic upgrade head
401
404
```
402
405
406
+
[!NOTE]
403
407
> If you do not have poetry, you may run it without poetry after running `pip install alembic`
404
408
405
409
## 5. Extending
@@ -505,7 +509,7 @@ Create the new entities and relationships and add them to the model
505
509
### 5.3 SQLAlchemy Models
506
510
Inside `app/models`, create a new `entity.py` for each new entity (replacing entity with the name) and define the attributes according to [SQLAlchemy 2.0 standards](https://docs.sqlalchemy.org/en/20/orm/mapping_styles.html#orm-mapping-styles):
507
511
508
-
> **Warning**
512
+
> [!WARNING]
509
513
> Note that since it inherits from `Base`, the new model is mapped as a python `dataclass`, so optional attributes (arguments with a default value) should be defined after required attributes.
510
514
511
515
```python
@@ -612,7 +616,7 @@ user = await crud_users.get_multi(
612
616
name="User Userson"
613
617
)
614
618
```
615
-
> **Warning**
619
+
> [!WARNING]
616
620
> Note that get_multi returns a python `dict`.
617
621
618
622
Which will return a python dict with the following structure:
@@ -812,7 +816,7 @@ The `cache` decorator allows you to cache the results of FastAPI endpoint functi
812
816
813
817
Caching the response of an endpoint is really simple, just apply the `cache` decorator to the endpoint function.
814
818
815
-
> **Warning**
819
+
> [!WARNING]
816
820
> Note that you should always pass request as a variable to your endpoint function if you plan to use the cache decorator.
817
821
818
822
```python
@@ -912,7 +916,7 @@ async def patch_post(
912
916
...
913
917
```
914
918
915
-
> **Warning**
919
+
> [!WARNING]
916
920
> Note that adding `to_invalidate_extra` will not work for **GET** requests.
917
921
918
922
#### Invalidate Extra By Pattern
@@ -974,7 +978,7 @@ async def patch_post(
974
978
975
979
Now it will invalidate all caches with a key that matches the pattern `"{username}_posts:*`, which will work for the paginated responses.
976
980
977
-
> **Warning**
981
+
> [!CAUTION]
978
982
> Using `pattern_to_invalidate_extra` can be resource-intensive on large datasets. Use it judiciously and consider the potential impact on Redis performance. Be cautious with patterns that could match a large number of keys, as deleting many keys simultaneously may impact the performance of the Redis server.
979
983
980
984
#### Client-side Caching
@@ -1058,7 +1062,7 @@ And a `pro` tier:
1058
1062
1059
1063
Then I'll associate a `rate_limit` for the path `api/v1/tasks/task` for each of them, I'll associate a `rate limit` for the path `api/v1/tasks/task`.
1060
1064
1061
-
> **Warning**
1065
+
> [!WARNING]
1062
1066
> Do not forget to add `api/v1/...` or any other prefix to the beggining of your path. For the structure of the boilerplate, `api/v1/<rest_of_the_path>`
1063
1067
1064
1068
1 request every hour (3600 seconds) for the free tier:
@@ -1124,13 +1128,13 @@ curl -X POST 'http://127.0.0.1:8000/api/v1/tasks/task?message=test' \
1124
1128
-H 'Authorization: Bearer <your-token-here>'
1125
1129
```
1126
1130
1127
-
> **Warning**
1131
+
> [!TIP]
1128
1132
> Since the `rate_limiter` dependency uses the `get_optional_user` dependency instead of `get_current_user`, it will not require authentication to be used, but will behave accordingly if the user is authenticated (and token is passed in header). If you want to ensure authentication, also use `get_current_user` if you need.
1129
1133
1130
1134
To change a user's tier, you may just use the `PATCH api/v1/user/{username}/tier` endpoint.
1131
1135
Note that for flexibility (since this is a boilerplate), it's not necessary to previously inform a tier_id to create a user, but you probably should set every user to a certain tier (let's say `free`) once they are created.
1132
1136
1133
-
> **Warning**
1137
+
> [!WARNING]
1134
1138
> If a user does not have a `tier` or the tier does not have a defined `rate limit` for the path and the token is still passed to the request, the default `limit` and `period` will be used, this will be saved in `app/logs`.
0 commit comments