29
29
logging_config .config_log (
30
30
logging_level = "INFO" , log_serializer = False , log_name = "log.log"
31
31
)
32
-
33
-
34
- @asynccontextmanager
35
- async def lifespan (app : FastAPI ):
36
- logger .info ("starting up" )
37
- # Create the tables in the database
38
- await async_db .create_tables ()
39
-
40
- create_users = True
41
- if create_users :
42
- await create_a_bunch_of_users (single_entry = 2000 , many_entries = 20000 )
43
- yield
44
- logger .info ("shutting down" )
45
-
46
-
47
- # Create an instance of the FastAPI class
48
- app = FastAPI (
49
- title = "FastAPI Example" , # The title of the API
50
- description = "This is an example of a FastAPI application using the DevSetGo Toolkit." , # A brief description of the API
51
- version = "0.1.0" , # The version of the API
52
- docs_url = "/docs" , # The URL where the API documentation will be served
53
- redoc_url = "/redoc" , # The URL where the ReDoc documentation will be served
54
- openapi_url = "/openapi.json" , # The URL where the OpenAPI schema will be served
55
- debug = True , # Enable debug mode
56
- middleware = [], # A list of middleware to include in the application
57
- routes = [], # A list of routes to include in the application
58
- lifespan = lifespan , # this is the replacement for the startup and shutdown events
59
- )
60
-
61
-
62
- @app .get ("/" )
63
- async def root ():
64
- """
65
- Root endpoint of API
66
- Returns:
67
- Redrects to openapi document
68
- """
69
- # redirect to openapi docs
70
- logger .info ("Redirecting to OpenAPI docs" )
71
- response = RedirectResponse (url = "/docs" )
72
- return response
73
-
74
-
75
- config_health = {
76
- "enable_status_endpoint" : True ,
77
- "enable_uptime_endpoint" : True ,
78
- "enable_heapdump_endpoint" : True ,
79
- }
80
- app .include_router (
81
- system_health_endpoints .create_health_router (config = config_health ),
82
- prefix = "/api/health" ,
83
- tags = ["system-health" ],
84
- )
85
-
86
32
# Create a DBConfig instance
87
33
config = {
88
34
# "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
@@ -95,6 +41,7 @@ async def root():
95
41
"pool_recycle" : 3600 ,
96
42
# "pool_timeout": 30,
97
43
}
44
+
98
45
# create database configuration
99
46
db_config = database_config .DBConfig (config )
100
47
# Create an AsyncDatabase instance
@@ -144,6 +91,64 @@ class Address(base_schema.SchemaBaseSQLite, async_db.Base):
144
91
"User" , back_populates = "addresses"
145
92
) # Relationship to the User class
146
93
94
+ @asynccontextmanager
95
+ async def lifespan (app : FastAPI ):
96
+ logger .info ("starting up" )
97
+ # Create the tables in the database
98
+ await async_db .create_tables ()
99
+
100
+ create_users = True
101
+ if create_users :
102
+ await create_a_bunch_of_users (single_entry = 2000 , many_entries = 20000 )
103
+ yield
104
+ logger .info ("shutting down" )
105
+ await async_db .disconnect ()
106
+ logger .info ("database disconnected" )
107
+ print ("That's all folks!" )
108
+
109
+
110
+
111
+ # Create an instance of the FastAPI class
112
+ app = FastAPI (
113
+ title = "FastAPI Example" , # The title of the API
114
+ description = "This is an example of a FastAPI application using the DevSetGo Toolkit." , # A brief description of the API
115
+ version = "0.1.0" , # The version of the API
116
+ docs_url = "/docs" , # The URL where the API documentation will be served
117
+ redoc_url = "/redoc" , # The URL where the ReDoc documentation will be served
118
+ openapi_url = "/openapi.json" , # The URL where the OpenAPI schema will be served
119
+ debug = True , # Enable debug mode
120
+ middleware = [], # A list of middleware to include in the application
121
+ routes = [], # A list of routes to include in the application
122
+ lifespan = lifespan , # this is the replacement for the startup and shutdown events
123
+ )
124
+
125
+
126
+ @app .get ("/" )
127
+ async def root ():
128
+ """
129
+ Root endpoint of API
130
+ Returns:
131
+ Redrects to openapi document
132
+ """
133
+ # redirect to openapi docs
134
+ logger .info ("Redirecting to OpenAPI docs" )
135
+ response = RedirectResponse (url = "/docs" )
136
+ return response
137
+
138
+
139
+ config_health = {
140
+ "enable_status_endpoint" : True ,
141
+ "enable_uptime_endpoint" : True ,
142
+ "enable_heapdump_endpoint" : True ,
143
+ }
144
+ app .include_router (
145
+ system_health_endpoints .create_health_router (config = config_health ),
146
+ prefix = "/api/health" ,
147
+ tags = ["system-health" ],
148
+ )
149
+
150
+
151
+
147
152
148
153
async def create_a_bunch_of_users (single_entry = 0 , many_entries = 0 ):
149
154
logger .info (f"single_entry: { single_entry } " )
0 commit comments