9
9
import gzip
10
10
import json
11
11
import requests
12
- from flask import Flask , request
13
12
import toml
14
13
import threading
14
+ import uvicorn
15
+ from fastapi import FastAPI , HTTPException , Request
15
16
16
17
import kernelci .api .helper
17
18
import kernelci .config
26
27
)
27
28
SETTINGS_PREFIX = 'runtime'
28
29
29
- app = Flask ( __name__ )
30
+ app = FastAPI ( )
30
31
executor = ThreadPoolExecutor (max_workers = 16 )
31
32
32
33
@@ -86,15 +87,20 @@ def _upload_log(log_parser, job_node, storage):
86
87
return _upload_file (storage , job_node , src , 'log.txt.gz' )
87
88
88
89
89
- @app .errorhandler (requests .exceptions .HTTPError )
90
- def handle_http_error (ex ):
91
- detail = ex .response .json ().get ('detail' ) or str (ex )
92
- return detail , ex .response .status_code
93
-
94
-
95
- @app .route ('/' )
96
- def hello ():
97
- return "KernelCI API & Pipeline LAVA callback handler"
90
+ @app .get ('/' )
91
+ async def read_root ():
92
+ page = '''
93
+ <html>
94
+ <head>
95
+ <title>KernelCI Pipeline Callback</title>
96
+ </head>
97
+ <body>
98
+ <h1>KernelCI Pipeline Callback</h1>
99
+ <p>This is a callback endpoint for the KernelCI pipeline.</p>
100
+ </body>
101
+ </html>
102
+ '''
103
+ return page
98
104
99
105
100
106
def async_job_submit (api_helper , node_id , job_callback ):
@@ -146,8 +152,9 @@ def submit_job(api_helper, node_id, job_callback):
146
152
executor .submit (async_job_submit , api_helper , node_id , job_callback )
147
153
148
154
149
- @app .post ('/node/<node_id>' )
150
- def callback (node_id ):
155
+ # POST /node/<node_id>
156
+ @app .post ('/node/{node_id}' )
157
+ async def callback (node_id : str , request : Request ):
151
158
tokens = SETTINGS .get (SETTINGS_PREFIX )
152
159
if not tokens :
153
160
return 'Unauthorized' , 401
@@ -169,7 +176,7 @@ def callback(node_id):
169
176
if not lab_name :
170
177
return 'Unauthorized' , 401
171
178
172
- data = request .get_json ()
179
+ data = await request .json ()
173
180
job_callback = kernelci .runtime .lava .Callback (data )
174
181
api_config_name = job_callback .get_meta ('api_config_name' )
175
182
api_token = os .getenv ('KCI_API_TOKEN' )
@@ -185,4 +192,4 @@ def callback(node_id):
185
192
tokens = SETTINGS .get (SETTINGS_PREFIX )
186
193
if not tokens :
187
194
print ('No tokens configured in toml file' )
188
- app .run (host = '0.0.0.0' , port = 8000 )
195
+ uvicorn .run (app , host = '0.0.0.0' , port = 8000 )
0 commit comments