zrb 1.0.0b1__py3-none-any.whl → 1.0.0b2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. zrb/__main__.py +0 -3
  2. zrb/builtin/__init__.py +3 -0
  3. zrb/builtin/group.py +1 -0
  4. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/config.py +1 -1
  5. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/add_entity_task.py +66 -21
  6. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/add_entity_util.py +67 -41
  7. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/template/app_template/module/my_module/service/my_entity/my_entity_service.py +69 -15
  8. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/template/app_template/module/my_module/service/my_entity/my_entity_service_factory.py +2 -1
  9. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/template/app_template/module/my_module/service/my_entity/repository/my_entity_db_repository.py +0 -10
  10. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/template/app_template/module/my_module/service/my_entity/repository/my_entity_repository.py +37 -16
  11. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/template/app_template/module/my_module/service/my_entity/repository/my_entity_repository_factory.py +2 -2
  12. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/template/app_template/schema/my_entity.py +16 -6
  13. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/template/client_method.py +57 -0
  14. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/template/gateway_subroute.py +63 -28
  15. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/add_module_task.py +1 -0
  16. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/template/app_template/module/my_module/client/my_module_api_client.py +6 -0
  17. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/template/app_template/module/my_module/client/{any_client.py → my_module_client.py} +1 -1
  18. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/template/app_template/module/my_module/client/my_module_client_factory.py +11 -0
  19. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/template/app_template/module/my_module/client/my_module_direct_client.py +5 -0
  20. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/template/app_template/module/my_module/route.py +1 -1
  21. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/template/module_task_definition.py +2 -2
  22. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/task.py +4 -4
  23. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/util.py +47 -20
  24. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/app_factory.py +29 -0
  25. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/base_db_repository.py +185 -101
  26. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/base_service.py +99 -108
  27. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/{db_engine.py → db_engine_factory.py} +1 -1
  28. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/error.py +12 -0
  29. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/logger_factory.py +10 -0
  30. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/parser_factory.py +7 -0
  31. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/util/app.py +47 -0
  32. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/util/parser.py +105 -0
  33. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/util/user_agent.py +58 -0
  34. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/config.py +1 -1
  35. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/main.py +1 -1
  36. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/client/auth_api_client.py +16 -0
  37. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/client/auth_client.py +163 -0
  38. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/client/auth_client_factory.py +9 -0
  39. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/client/auth_direct_client.py +15 -0
  40. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/migration/versions/3093c7336477_add_auth_tables.py +160 -0
  41. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/migration_metadata.py +18 -1
  42. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/route.py +5 -1
  43. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/permission/__init__.py +0 -0
  44. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/permission/permission_service.py +117 -0
  45. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/permission/permission_service_factory.py +11 -0
  46. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/permission/repository/permission_db_repository.py +26 -0
  47. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/permission/repository/permission_repository.py +61 -0
  48. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/permission/repository/permission_repository_factory.py +13 -0
  49. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/role/__init__.py +0 -0
  50. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/role/repository/role_db_repository.py +75 -0
  51. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/role/repository/role_repository.py +59 -0
  52. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/role/repository/role_repository_factory.py +13 -0
  53. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/role/role_service.py +105 -0
  54. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/role/role_service_factory.py +7 -0
  55. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/user/repository/user_db_repository.py +42 -13
  56. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/user/repository/user_repository.py +38 -17
  57. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/user/repository/user_repository_factory.py +2 -2
  58. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/user/user_service.py +69 -17
  59. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/user/user_service_factory.py +2 -1
  60. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/gateway/route.py +1 -1
  61. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/gateway/subroute/auth.py +198 -28
  62. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/gateway/util/view.py +1 -1
  63. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/requirements.txt +1 -1
  64. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/schema/permission.py +17 -5
  65. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/schema/role.py +50 -4
  66. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/schema/session.py +52 -0
  67. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/schema/user.py +30 -5
  68. zrb/builtin/random.py +61 -0
  69. zrb/cmd/cmd_val.py +6 -5
  70. zrb/runner/cli.py +10 -1
  71. zrb/runner/web_util/token.py +7 -3
  72. zrb/task/base_task.py +24 -2
  73. zrb/task/cmd_task.py +7 -5
  74. zrb/util/cmd/command.py +1 -0
  75. zrb/util/file.py +7 -1
  76. {zrb-1.0.0b1.dist-info → zrb-1.0.0b2.dist-info}/METADATA +1 -1
  77. {zrb-1.0.0b1.dist-info → zrb-1.0.0b2.dist-info}/RECORD +80 -61
  78. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/template/any_client_method.py +0 -27
  79. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/template/app_template/module/my_module/client/api_client.py +0 -6
  80. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/template/app_template/module/my_module/client/direct_client.py +0 -5
  81. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/template/app_template/module/my_module/client/factory.py +0 -9
  82. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/app.py +0 -57
  83. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/client/any_client.py +0 -33
  84. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/client/api_client.py +0 -7
  85. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/client/direct_client.py +0 -6
  86. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/client/factory.py +0 -9
  87. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/migration/versions/3093c7336477_add_user_table.py +0 -37
  88. /zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/{view.py → util/view.py} +0 -0
  89. {zrb-1.0.0b1.dist-info → zrb-1.0.0b2.dist-info}/WHEEL +0 -0
  90. {zrb-1.0.0b1.dist-info → zrb-1.0.0b2.dist-info}/entry_points.txt +0 -0
@@ -1,4 +1,4 @@
1
- from my_app_name.common.db_engine import engine
1
+ from my_app_name.common.db_engine_factory import db_engine
2
2
  from my_app_name.config import APP_REPOSITORY_TYPE
3
3
  from my_app_name.module.my_module.service.my_entity.repository.my_entity_db_repository import (
4
4
  MyEntityDBRepository,
@@ -8,6 +8,6 @@ from my_app_name.module.my_module.service.my_entity.repository.my_entity_reposit
8
8
  )
9
9
 
10
10
  if APP_REPOSITORY_TYPE == "db":
11
- my_entity_repository: MyEntityRepository = MyEntityDBRepository(engine)
11
+ my_entity_repository: MyEntityRepository = MyEntityDBRepository(db_engine)
12
12
  else:
13
13
  my_entity_repository: MyEntityRepository = None
@@ -1,6 +1,7 @@
1
1
  import datetime
2
2
 
3
3
  import ulid
4
+ from pydantic import BaseModel
4
5
  from sqlmodel import Field, SQLModel
5
6
 
6
7
 
@@ -9,7 +10,8 @@ class MyEntityBase(SQLModel):
9
10
 
10
11
 
11
12
  class MyEntityCreate(MyEntityBase):
12
- pass
13
+ def with_audit(self, created_by: str) -> "MyEntityCreateWithAudit":
14
+ return MyEntityCreateWithAudit(**self.model_dump(), created_by=created_by)
13
15
 
14
16
 
15
17
  class MyEntityCreateWithAudit(MyEntityCreate):
@@ -19,6 +21,9 @@ class MyEntityCreateWithAudit(MyEntityCreate):
19
21
  class MyEntityUpdate(SQLModel):
20
22
  my_column: str | None = None
21
23
 
24
+ def with_audit(self, updated_by: str) -> "MyEntityUpdateWithAudit":
25
+ return MyEntityUpdateWithAudit(**self.model_dump(), updated_by=updated_by)
26
+
22
27
 
23
28
  class MyEntityUpdateWithAudit(MyEntityUpdate):
24
29
  updated_by: str
@@ -28,10 +33,15 @@ class MyEntityResponse(MyEntityBase):
28
33
  id: str
29
34
 
30
35
 
36
+ class MultipleMyEntityResponse(BaseModel):
37
+ data: list[MyEntityResponse]
38
+ count: int
39
+
40
+
31
41
  class MyEntity(SQLModel, table=True):
32
42
  id: str = Field(default_factory=lambda: ulid.new().str, primary_key=True)
33
- created_at: datetime.datetime
34
- created_by: str
35
- updated_at: datetime.datetime
36
- updated_by: str
37
- my_column: str
43
+ created_at: datetime.datetime = Field(index=True)
44
+ created_by: str = Field(index=True)
45
+ updated_at: datetime.datetime | None = Field(index=True)
46
+ updated_by: str | None = Field(index=True)
47
+ my_column: str = Field(index=True)
@@ -0,0 +1,57 @@
1
+ # MyEntity related methods
2
+
3
+
4
+ @abstractmethod
5
+ async def get_my_entity_by_id(self, my_entity_id: str) -> MyEntityResponse:
6
+ """Get my entity by id"""
7
+
8
+
9
+ @abstractmethod
10
+ async def get_my_entities(
11
+ self,
12
+ page: int = 1,
13
+ page_size: int = 10,
14
+ sort: str | None = None,
15
+ filter: str | None = None,
16
+ ) -> MultipleMyEntityResponse:
17
+ """Get my entities by filter and sort"""
18
+
19
+
20
+ @abstractmethod
21
+ async def create_my_entity(self, data: MyEntityCreateWithAudit) -> MyEntityResponse:
22
+ """Create a new my entities"""
23
+
24
+
25
+ @abstractmethod
26
+ async def create_my_entity(
27
+ self, data: list[MyEntityCreateWithAudit]
28
+ ) -> list[MyEntityResponse]:
29
+ """Create new my entities"""
30
+
31
+
32
+ @abstractmethod
33
+ async def update_my_entity_bulk(
34
+ self, my_entity_ids: list[str], data: MyEntityUpdateWithAudit
35
+ ) -> MyEntityResponse:
36
+ """Update some my entities"""
37
+
38
+
39
+ @abstractmethod
40
+ async def update_my_entity(
41
+ self, my_entity_id: str, data: MyEntityUpdateWithAudit
42
+ ) -> MyEntityResponse:
43
+ """Update a my entity"""
44
+
45
+
46
+ @abstractmethod
47
+ async def delete_my_entity_bulk(
48
+ self, my_entity_ids: str, deleted_by: str
49
+ ) -> MyEntityResponse:
50
+ """Delete some my entities"""
51
+
52
+
53
+ @abstractmethod
54
+ async def delete_my_entity(
55
+ self, my_entity_id: str, deleted_by: str
56
+ ) -> MyEntityResponse:
57
+ """Delete a my entity"""
@@ -1,37 +1,72 @@
1
- @app.get("/api/v1/my_entities", response_model=list[MyEntityResponse])
2
- async def get_all_my_entities() -> MyEntityResponse:
3
- return await my_module_client.get_all_my_entities()
1
+ # MyEntity routes
4
2
 
5
3
 
6
- @app.get("/api/v1/my_entities/{my_entity_id}", response_model=MyEntityResponse)
4
+ @app.get("/api/v1/my-entities", response_model=MultipleMyEntityResponse)
5
+ async def get_my_entities(
6
+ page: int = 1,
7
+ page_size: int = 10,
8
+ sort: str | None = None,
9
+ filter: str | None = None,
10
+ ) -> MultipleMyEntityResponse:
11
+ return await my_module_client.get_my_entities(
12
+ page=page, page_size=page_size, sort=sort, filter=filter
13
+ )
14
+
15
+
16
+ @app.get("/api/v1/my-entities/{my_entity_id}", response_model=MyEntityResponse)
7
17
  async def get_my_entity_by_id(my_entity_id: str) -> MyEntityResponse:
8
18
  return await my_module_client.get_my_entity_by_id(my_entity_id)
9
19
 
10
20
 
11
21
  @app.post(
12
- "/api/v1/my_entities", response_model=MyEntityResponse | list[MyEntityResponse]
22
+ "/api/v1/my-entities/bulk",
23
+ response_model=list[MyEntityResponse],
24
+ )
25
+ async def create_my_entity_bulk(data: list[MyEntityCreate]):
26
+ return await my_module_client.create_my_entity_bulk(
27
+ [row.with_audit(created_by="system") for row in data]
28
+ )
29
+
30
+
31
+ @app.post(
32
+ "/api/v1/my-entities",
33
+ response_model=MyEntityResponse,
34
+ )
35
+ async def create_my_entity(data: MyEntityCreate):
36
+ return await my_module_client.create_my_entity(data.with_audit(created_by="system"))
37
+
38
+
39
+ @app.put(
40
+ "/api/v1/my-entities/bulk",
41
+ response_model=list[MyEntityResponse],
42
+ )
43
+ async def update_my_entity_bulk(my_entity_ids: list[str], data: MyEntityUpdate):
44
+ return await my_module_client.update_my_entity_bulk(
45
+ my_entity_ids, data.with_audit(updated_by="system")
46
+ )
47
+
48
+
49
+ @app.put(
50
+ "/api/v1/my-entities/{my_entity_id}",
51
+ response_model=MyEntityResponse,
52
+ )
53
+ async def update_my_entity(my_entity_id: str, data: MyEntityUpdate):
54
+ return await my_module_client.update_my_entity(data.with_audit(updated_by="system"))
55
+
56
+
57
+ @app.delete(
58
+ "/api/v1/my-entities/bulk",
59
+ response_model=list[MyEntityResponse],
60
+ )
61
+ async def delete_my_entity_bulk(my_entity_ids: list[str]):
62
+ return await my_module_client.delete_my_entity_bulk(
63
+ my_entity_ids, deleted_by="system"
64
+ )
65
+
66
+
67
+ @app.delete(
68
+ "/api/v1/my-entities/{my_entity_id}",
69
+ response_model=MyEntityResponse,
13
70
  )
14
- async def create_my_entity(data: MyEntityCreate | list[MyEntityCreate]):
15
- if isinstance(data, MyEntityCreate):
16
- data_dict = data.model_dump(exclude_unset=True)
17
- audited_data = MyEntityCreateWithAudit(**data_dict, created_by="system")
18
- return await my_module_client.create_my_entity(audited_data)
19
- audited_data = [
20
- MyEntityCreateWithAudit(
21
- **row.model_dump(exclude_unset=True), created_by="system"
22
- )
23
- for row in data
24
- ]
25
- return await my_module_client.create_my_entity(audited_data)
26
-
27
-
28
- @app.put("/api/v1/my_entities/{my_entity_id}", response_model=MyEntityResponse)
29
- async def update_my_entity(my_entity_id: str, data: MyEntityUpdate) -> MyEntityResponse:
30
- data_dict = data.model_dump(exclude_unset=True)
31
- audited_data = MyEntityUpdateWithAudit(**data_dict, updated_by="system")
32
- return await my_module_client.update_my_entity(my_entity_id, audited_data)
33
-
34
-
35
- @app.delete("/api/v1/my_entities/{my_entity_id}", response_model=MyEntityResponse)
36
- async def delete_my_entity(my_entity_id: str) -> MyEntityResponse:
71
+ async def delete_my_entity(my_entity_id: str):
37
72
  return await my_module_client.delete_my_entity(my_entity_id, deleted_by="system")
@@ -50,6 +50,7 @@ scaffold_my_app_name_module = Scaffolder(
50
50
  transform={
51
51
  "MY_MODULE": "{to_snake_case(ctx.input.module).upper()}",
52
52
  "my_module": "{to_snake_case(ctx.input.module)}",
53
+ "MyModule": "{to_pascal_case(ctx.input.module)}",
53
54
  },
54
55
  ),
55
56
  # Gateway's module subroute (my_app_name/module/gateway/subroute/snake_module_name.py)
@@ -0,0 +1,6 @@
1
+ from my_app_name.config import APP_MY_MODULE_BASE_URL
2
+ from my_app_name.module.my_module.client.my_module_client import MyModuleClient
3
+
4
+
5
+ class MyModuleAPIClient(MyModuleClient):
6
+ pass
@@ -1,7 +1,7 @@
1
1
  from abc import ABC, abstractmethod
2
2
 
3
3
 
4
- class AnyClient(ABC):
4
+ class MyModuleClient(ABC):
5
5
  """
6
6
  Defining client methods
7
7
  """
@@ -0,0 +1,11 @@
1
+ from my_app_name.config import APP_COMMUNICATION
2
+ from my_app_name.module.my_module.client.my_module_api_client import MyModuleAPIClient
3
+ from my_app_name.module.my_module.client.my_module_client import MyModuleClient
4
+ from my_app_name.module.my_module.client.my_module_direct_client import (
5
+ MyModuleDirectClient,
6
+ )
7
+
8
+ if APP_COMMUNICATION == "direct":
9
+ my_module_client: MyModuleClient = MyModuleDirectClient()
10
+ elif APP_COMMUNICATION == "api":
11
+ my_module_client: MyModuleClient = MyModuleAPIClient()
@@ -0,0 +1,5 @@
1
+ from my_app_name.module.my_module.client.my_module_client import MyModuleClient
2
+
3
+
4
+ class MyModuleDirectClient(MyModuleClient):
5
+ pass
@@ -1,5 +1,5 @@
1
1
  from fastapi import FastAPI
2
- from my_app_name.common.app import app
2
+ from my_app_name.common.app_factory import app
3
3
  from my_app_name.common.schema import BasicResponse
4
4
  from my_app_name.config import APP_MAIN_MODULE, APP_MODE, APP_MODULES
5
5
 
@@ -1,7 +1,7 @@
1
1
  # 🔐 Run/Migrate My Module ==========================================================
2
2
 
3
3
  run_my_module = app_run_group.add_task(
4
- run_microservice("my-module", 3000, "my_module"), alias="microservices-my_module"
4
+ run_microservice("my-module", 3000, "my_module"), alias="svc-my_module"
5
5
  )
6
6
  prepare_venv >> run_my_module >> run_microservices
7
7
 
@@ -17,7 +17,7 @@ prepare_venv >> migrate_monolith_my_module >> [migrate_monolith, run_monolith]
17
17
 
18
18
  migrate_microservices_my_module = app_migrate_group.add_task(
19
19
  migrate_module("my-module", "my_module", as_microservices=True),
20
- alias="microservices-my-module",
20
+ alias="svc-my-module",
21
21
  )
22
22
  (
23
23
  prepare_venv
@@ -100,7 +100,7 @@ migrate_microservices >> migrate_all
100
100
  # 📡 Run/Migrate Gateway =======================================================
101
101
 
102
102
  run_gateway = app_run_group.add_task(
103
- run_microservice("gateway", 3001, "gateway"), alias="microservices-gateway"
103
+ run_microservice("gateway", 3001, "gateway"), alias="svc-gateway"
104
104
  )
105
105
  prepare_venv >> run_gateway >> run_microservices
106
106
 
@@ -114,14 +114,14 @@ prepare_venv >> migrate_monolith_gateway >> [migrate_monolith, run_monolith]
114
114
 
115
115
  migrate_microservices_gateway = app_migrate_group.add_task(
116
116
  migrate_module("gateway", "gateway", as_microservices=True),
117
- alias="microservices-gateway",
117
+ alias="svc-gateway",
118
118
  )
119
119
  prepare_venv >> migrate_microservices_gateway >> [migrate_microservices, run_gateway]
120
120
 
121
121
  # 🔐 Run/Migrate Auth ==========================================================
122
122
 
123
123
  run_auth = app_run_group.add_task(
124
- run_microservice("auth", 3002, "auth"), alias="microservices-auth"
124
+ run_microservice("auth", 3002, "auth"), alias="svc-auth"
125
125
  )
126
126
  prepare_venv >> run_auth >> run_microservices
127
127
 
@@ -134,6 +134,6 @@ migrate_monolith_auth = migrate_module("auth", "auth", as_microservices=False)
134
134
  prepare_venv >> migrate_monolith_auth >> [migrate_monolith, run_monolith]
135
135
 
136
136
  migrate_microservices_auth = app_migrate_group.add_task(
137
- migrate_module("auth", "auth", as_microservices=True), alias="microservices-auth"
137
+ migrate_module("auth", "auth", as_microservices=True), alias="svc-auth"
138
138
  )
139
139
  prepare_venv >> migrate_microservices_auth >> [migrate_microservices, run_auth]
@@ -1,4 +1,5 @@
1
1
  import os
2
+ import platform
2
3
 
3
4
  from my_app_name._zrb.config import (
4
5
  ACTIVATE_VENV_SCRIPT,
@@ -8,6 +9,7 @@ from my_app_name._zrb.config import (
8
9
  )
9
10
 
10
11
  from zrb import Cmd, CmdTask, EnvFile, EnvMap, StrInput, Task
12
+ from zrb.util.string.conversion import double_quote, to_snake_case
11
13
 
12
14
 
13
15
  def create_migration(name: str, module: str) -> Task:
@@ -20,19 +22,13 @@ def create_migration(name: str, module: str) -> Task:
20
22
  prompt="Migration message",
21
23
  allow_empty=False,
22
24
  ),
23
- env=[
24
- EnvFile(path=os.path.join(APP_DIR, "template.env")),
25
- EnvMap(
26
- vars={
27
- "APP_DB_URL": f"sqlite:///{APP_DIR}/.migration.{module}.db",
28
- "MY_APP_NAME_MODULES": f"{module}",
29
- }
30
- ),
31
- ],
25
+ env=EnvFile(path=os.path.join(APP_DIR, "template.env")),
32
26
  cwd=APP_DIR,
33
27
  cmd=[
34
28
  ACTIVATE_VENV_SCRIPT,
35
- f"cd {os.path.join(APP_DIR, 'module', module)}",
29
+ set_create_migration_db_url_env(module),
30
+ set_module_env(module),
31
+ cd_module_script(module),
36
32
  "alembic upgrade head",
37
33
  Cmd(
38
34
  "alembic revision --autogenerate -m {double_quote(ctx.input.message)}",
@@ -45,7 +41,11 @@ def create_migration(name: str, module: str) -> Task:
45
41
 
46
42
 
47
43
  def migrate_module(name: str, module: str, as_microservices: bool) -> Task:
48
- env_vars = MICROSERVICES_ENV_VARS if as_microservices else MONOLITH_ENV_VARS
44
+ env_vars = (
45
+ dict(MICROSERVICES_ENV_VARS) if as_microservices else dict(MONOLITH_ENV_VARS)
46
+ )
47
+ if as_microservices:
48
+ env_vars["MY_APP_NAME_MODULES"] = to_snake_case(module)
49
49
  return CmdTask(
50
50
  name=(
51
51
  f"migrate-my-app-name-{name}"
@@ -55,17 +55,12 @@ def migrate_module(name: str, module: str, as_microservices: bool) -> Task:
55
55
  description=f"🧩 Run My App Name {name.capitalize()} DB migration",
56
56
  env=[
57
57
  EnvFile(path=os.path.join(APP_DIR, "template.env")),
58
- EnvMap(
59
- vars={
60
- **env_vars,
61
- "MY_APP_NAME_MODULES": f"{module}",
62
- }
63
- ),
58
+ EnvMap(vars=env_vars),
64
59
  ],
65
60
  cwd=APP_DIR,
66
61
  cmd=[
67
62
  ACTIVATE_VENV_SCRIPT,
68
- f"cd {os.path.join(APP_DIR, 'module', module)}",
63
+ cd_module_script(module),
69
64
  "alembic upgrade head",
70
65
  ],
71
66
  render_cmd=False,
@@ -82,14 +77,14 @@ def run_microservice(name: str, port: int, module: str) -> Task:
82
77
  EnvMap(
83
78
  vars={
84
79
  **MICROSERVICES_ENV_VARS,
85
- "MY_APP_NAME_PORT": f"{port}",
86
- "MY_APP_NAME_MODULES": f"{module}",
87
80
  }
88
81
  ),
89
82
  ],
90
83
  cwd=APP_DIR,
91
84
  cmd=[
92
85
  ACTIVATE_VENV_SCRIPT,
86
+ set_env("MY_APP_NAME_MODULES", module),
87
+ set_env("MY_APP_NAME_PORT", f"{port}"),
93
88
  'fastapi dev main.py --port "${MY_APP_NAME_PORT}"',
94
89
  ],
95
90
  render_cmd=False,
@@ -109,3 +104,35 @@ def get_existing_schema_names() -> list[str]:
109
104
  for entry in os.scandir(module_dir_path)
110
105
  if entry.is_file() and entry.name.endswith(".py")
111
106
  ]
107
+
108
+
109
+ def set_create_migration_db_url_env(module_name: str) -> str:
110
+ return set_env(
111
+ "MY_APP_NAME_DB_URL",
112
+ f"sqlite:///{APP_DIR}/.migration.{to_snake_case(module_name)}.db",
113
+ )
114
+
115
+
116
+ def set_module_env(module_name: str) -> str:
117
+ return (set_env("MY_APP_NAME_MODULES", to_snake_case(module_name)),)
118
+
119
+
120
+ def cd_module_script(module_name: str) -> str:
121
+ module_dir_path = os.path.join(APP_DIR, "module", to_snake_case(module_name))
122
+ return f"cd {module_dir_path}"
123
+
124
+
125
+ def set_env(var_name: str, var_value: str) -> str:
126
+ """
127
+ Generates a script to set an environment variable depending on the OS.
128
+ :param var_name: Name of the environment variable.
129
+ :param var_value: Value of the environment variable.
130
+ :return: A string containing the appropriate script.
131
+ """
132
+ if platform.system() == "Windows":
133
+ # PowerShell script for Windows
134
+ script = f'[Environment]::SetEnvironmentVariable({double_quote(var_name)}, {double_quote(var_value)}, "User")' # noqa
135
+ else:
136
+ # Bash script for Unix-like systems
137
+ script = f"export {var_name}={double_quote(var_value)}"
138
+ return script
@@ -0,0 +1,29 @@
1
+ import os
2
+
3
+ from fastapi import FastAPI
4
+ from my_app_name.common.db_engine_factory import db_engine
5
+ from my_app_name.common.util.app import (
6
+ create_default_app_lifespan,
7
+ get_default_app_title,
8
+ serve_docs,
9
+ serve_static_dir,
10
+ )
11
+ from my_app_name.config import (
12
+ APP_GATEWAY_FAVICON_PATH,
13
+ APP_GATEWAY_TITLE,
14
+ APP_GATEWAY_VIEW_PATH,
15
+ APP_MODE,
16
+ APP_MODULES,
17
+ APP_VERSION,
18
+ )
19
+
20
+ app_title = get_default_app_title(APP_GATEWAY_TITLE, mode=APP_MODE, modules=APP_MODULES)
21
+ app = FastAPI(
22
+ title=app_title,
23
+ version=APP_VERSION,
24
+ lifespan=create_default_app_lifespan(db_engine),
25
+ docs_url=None,
26
+ )
27
+
28
+ serve_static_dir(app, os.path.join(APP_GATEWAY_VIEW_PATH, "static"))
29
+ serve_docs(app, app_title=app_title, favicon_url=APP_GATEWAY_FAVICON_PATH)