Skip to content

Commit 86db625

Browse files
committed
adding the disconnect function to the example
1 parent e1da6c6 commit 86db625

File tree

2 files changed

+60
-55
lines changed

2 files changed

+60
-55
lines changed

coverage.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" ?>
2-
<coverage version="7.6.1" timestamp="1724623192815" lines-valid="716" lines-covered="193" line-rate="0.2696" branches-covered="0" branches-valid="0" branch-rate="0" complexity="0">
2+
<coverage version="7.6.1" timestamp="1724623709392" lines-valid="716" lines-covered="193" line-rate="0.2696" branches-covered="0" branches-valid="0" branch-rate="0" complexity="0">
33
<!-- Generated by coverage.py: https://coverage.readthedocs.io/en/7.6.1 -->
44
<!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd -->
55
<sources>

examples/fastapi_example.py

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -29,60 +29,6 @@
2929
logging_config.config_log(
3030
logging_level="INFO", log_serializer=False, log_name="log.log"
3131
)
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-
8632
# Create a DBConfig instance
8733
config = {
8834
# "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
@@ -95,6 +41,7 @@ async def root():
9541
"pool_recycle": 3600,
9642
# "pool_timeout": 30,
9743
}
44+
9845
# create database configuration
9946
db_config = database_config.DBConfig(config)
10047
# Create an AsyncDatabase instance
@@ -144,6 +91,64 @@ class Address(base_schema.SchemaBaseSQLite, async_db.Base):
14491
"User", back_populates="addresses"
14592
) # Relationship to the User class
14693

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+
147152

148153
async def create_a_bunch_of_users(single_entry=0, many_entries=0):
149154
logger.info(f"single_entry: {single_entry}")

0 commit comments

Comments
 (0)