Skip to content

Commit c05b628

Browse files
bors[bot]p4vook
andauthored
Merge #366
366: Move db connection to fastapi dependency r=MikailBag a=pavel-the-best Co-authored-by: Pavel Kalugin <[email protected]>
2 parents 002e00f + 844e431 commit c05b628

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

src/apiserver/db_connect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def db_connect_url(db_url: str) -> pymongo.database.Database:
2828

2929
def db_connect_via_env() -> pymongo.database.Database:
3030
"""
31-
Connects to MongoDB database using URL in DATABASE_URL
31+
Connects to MongoDB database using URL in MONGODB_CONNECTION_STRING
3232
environment variable. See `db_connect_url` function for url format.
3333
"""
34-
db_url = os.environ["DATABASE_URL"]
34+
db_url = os.environ["MONGODB_CONNECTION_STRING"]
3535
return db_connect_url(db_url)

src/apiserver/routes.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,24 @@ def route_api_version():
8383
It means, that if you tested application with apiVersion == X.Y, your application
8484
should assert that MAJOR = X and MINOR >= Y
8585
"""
86+
8687
return api_models.ApiVersion(major=0, minor=0)
8788

8889
@app.post('/runs', response_model=api_models.Run,
8990
operation_id="submitRun")
90-
def route_submit(params: RunSubmitSimpleParams):
91+
def route_submit(params: RunSubmitSimpleParams, db: pymongo.database.Database = fastapi.Depends(db_connect)):
9192
"""
9293
Submits new run
9394
9495
This operation creates new run, with given source code, and queues it for
9596
judging. Created run will be returned. All fields against `id` will match
9697
fields of request body; `id` will be real id of this run.
9798
"""
98-
db = db_connect()
99+
99100
run_uuid = uuid.uuid4()
100101
user_id = uuid.UUID('12345678123456781234567812345678')
101102
doc_main = db_models.RunMainProj(id=run_uuid, toolchain_name=params.toolchain,
102103
problem_name=params.problem, user_id=user_id, contest_name=params.contest, phase=str(db_models.RunPhase.QUEUED))
103-
104104
doc_source = db_models.RunSourceProj(
105105
source=base64.b64decode(params.code))
106106
doc = {**dict(doc_main), **dict(doc_source)}
@@ -109,30 +109,27 @@ def route_submit(params: RunSubmitSimpleParams):
109109

110110
@app.get('/runs', response_model=typing.List[api_models.Run],
111111
operation_id='listRuns')
112-
def route_list_runs():
112+
def route_list_runs(db: pymongo.database.Database = fastapi.Depends(db_connect)):
113113
"""
114114
Lists runs
115115
116116
This operation returns all created runs
117117
"""
118-
db = db_connect()
119118

120119
runs = db.runs.find(
121120
projection=db_models.RunMainProj.FIELDS)
122121
runs = list(map(api_models.Run.from_db, runs))
123122
return runs
124123

125124
@app.get('/runs/{run_id}', response_model=api_models.Run, operation_id='getRun')
126-
def route_get_run(run_id: uuid.UUID):
125+
def route_get_run(run_id: uuid.UUID, db: pymongo.database.Database = fastapi.Depends(db_connect)):
127126
"""
128127
Loads run by id
129128
"""
130-
db = db_connect()
131129

132130
run = db.runs.find_one(projection=db_models.RunMainProj.FIELDS, filter={
133131
'id': run_id
134132
})
135-
136133
if run is None:
137134
raise fastapi.HTTPException(404, detail='RunNotFound')
138135
return api_models.Run.from_db(run)
@@ -142,11 +139,10 @@ def route_get_run(run_id: uuid.UUID):
142139
'description': "Run source is not available"
143140
}
144141
})
145-
def route_get_run_source(run_id: uuid.UUID):
142+
def route_get_run_source(run_id: uuid.UUID, db: pymongo.database.Database = fastapi.Depends(db_connect)):
146143
"""
147144
Returns run source as base64-encoded JSON string
148145
"""
149-
db = db_connect()
150146

151147
doc = db.runs.find_one(projection=['source'], filter={
152148
'id': run_id
@@ -158,13 +154,12 @@ def route_get_run_source(run_id: uuid.UUID):
158154
return base64.b64encode(doc['source'])
159155

160156
@app.patch('/runs/{run_id}', response_model=api_models.Run, operation_id='patchRun')
161-
def route_run_patch(run_id: uuid.UUID, patch: RunPatch):
157+
def route_run_patch(run_id: uuid.UUID, patch: RunPatch, db: pymongo.database.Database = fastapi.Depends(db_connect)):
162158
"""
163159
Modifies existing run
164160
165161
See `RunPatch` documentation for what can be updated.
166162
"""
167-
db = db_connect()
168163

169164
p = {
170165
'$set': {
@@ -189,7 +184,7 @@ def route_run_patch(run_id: uuid.UUID, patch: RunPatch):
189184

190185
@app.post('/queue', response_model=typing.List[api_models.Run],
191186
operation_id='popRunFromQueue')
192-
def route_pop_from_invoke_queue(limit: int):
187+
def route_pop_from_invoke_queue(limit: int, db: pymongo.database.Database = fastapi.Depends(db_connect)):
193188
"""
194189
Returns runs that should be judged
195190
@@ -200,7 +195,6 @@ def route_pop_from_invoke_queue(limit: int):
200195
be released. It means, that in some rare situations same run can be judged
201196
several times. All judgings except one will be ignored.
202197
"""
203-
db = db_connect()
204198

205199
runs = []
206200
for _ in range(limit):

0 commit comments

Comments
 (0)