11#!/usr/bin/env python
22# coding: utf-8
3- """Tornado-based server sample code"""
3+ """FastAPI-based server sample code"""
4+
45import argparse
56import 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
1114os .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
1619def 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+
3245if __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