Skip to content

Commit f4d24c4

Browse files
committed
Use FastAPI for h5grove support server
1 parent 1353295 commit f4d24c4

File tree

5 files changed

+375
-33
lines changed

5 files changed

+375
-33
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ dist*/
99

1010
/coverage/
1111

12+
/support/h5grove/__pycache__/
13+
1214
.env.local
1315
.env.*.local
1416

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"test": "vitest",
3333
"support:setup": "poetry -C support/sample install && poetry -C support/h5grove install",
3434
"support:sample": "poetry -C support/sample run python create_h5_sample.py",
35-
"support:h5grove": "poetry -C support/h5grove run python tornado_app.py --basedir ../sample/dist",
35+
"support:h5grove": "poetry -C support/h5grove run python fastapi_app.py --basedir ../sample/dist",
3636
"cypress": "cypress open --e2e",
3737
"cypress:run": "cypress run --e2e",
3838
"version": "pnpm -r sync-version && git add .",
Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
#!/usr/bin/env python
22
# coding: utf-8
3-
"""Tornado-based server sample code"""
3+
"""FastAPI-based server sample code"""
4+
45
import argparse
56
import os
6-
import tornado.web
7-
import tornado.ioloop
7+
8+
import uvicorn
9+
from fastapi import FastAPI
10+
from fastapi.middleware.cors import CORSMiddleware
811

912
# Disable libhdf5 file locking since h5grove is only reading files
1013
# This needs to be done before any import of h5py, so before h5grove import
1114
os.environ["HDF5_USE_FILE_LOCKING"] = "FALSE"
1215

13-
from h5grove.tornado_utils import get_handlers # noqa
16+
from h5grove.fastapi_utils import router, settings # noqa
1417

1518

1619
def parser_fn():
@@ -29,13 +32,20 @@ def parser_fn():
2932
return parser
3033

3134

35+
app = FastAPI()
36+
app.include_router(router)
37+
app.add_middleware(
38+
CORSMiddleware,
39+
allow_origins="*",
40+
allow_credentials=True,
41+
allow_methods=["*"],
42+
allow_headers=["*"],
43+
)
44+
3245
if __name__ == "__main__":
3346
parser = parser_fn()
3447
options = parser.parse_args()
3548

36-
base_dir = os.path.abspath(options.basedir)
49+
settings.base_dir = options.basedir
3750

38-
app = tornado.web.Application(get_handlers(base_dir, allow_origin="*"), debug=True)
39-
app.listen(options.port, options.ip)
40-
print(f"App is listening on {options.ip}:{options.port} serving from {base_dir}...")
41-
tornado.ioloop.IOLoop.current().start()
51+
uvicorn.run("fastapi_app:app", host=options.ip, port=options.port, log_level="info")

0 commit comments

Comments
 (0)