Configuration Driven API
Suppose you have configuration files like following
!ls ../tests/confs/
!cat ../tests/confs/*.json
from flaskonf.api import FlaskonfAPI
from typing import Dict
app = FlaskonfAPI("AnConfAPI")
Bind the app to the configuration directory
Example directory is for test inputs example, you can just leave it empety for now
app.build_on_config(
confs_dir="../tests/confs/",
examples_dir="../tests/examples/" )
@app.conf_route("/guide/", nobuild=True)
def guide_api(inputs: Dict):
data = inputs['data']
conf = data["conf"]
user = data["user"]
return {"city_data": conf, "user": user}
app.build_flaskonf()
Notice, the configuration data is accessible in the decorated function
The rest is flask usual you are familiared with, if you run like following, you can visit localhost:6060/guide/la/
or localhost:6060/guide/shanghai/
to test your API
app.run("0.0.0.0", port = 6060)
Set nobuild=True
@app.conf_route("/guide2/", nobuild=False)
def build_city_guide(conf_file: str, conf: Dict):
logging.info(f"{conf}")
# doing other things for building API here
# like load huge model into memory with configuration
def guide_api(inputs: Dict):
data = inputs['data']
user = data["user"]
return {"city_data": conf, "user": user}
return guide_api