Package: faastapi
FaastAPIEasily create OpenFaas functions built on top of FastAPI Python frameworkWarning:This project is in its early phase. It might be subject to a lot of changes and many features might be added/removed.OverviewFaastAPIis built on top ofFastAPIframework,pydanticlibrary, andjinja2library.It allows developers to quickly generateOpenFaasfunctions.Several plugins can be used to quicly set up a rest API. A command line interface can be installed with the package so that functions can be tested locally.InstallationYou can install package using pip:pipinstallfaastapiBy default, the command line interface and plugins are not installed. You can choose to install the cli using thecliextra:pipinstallfaastapi[cli]ExampleA simple Faastapi function file (by default Faastapi search files namedfaastapi.yml) can be as follow:---apiversion:v1meta:name:hello-faastapiversion:0.3.4image:gcharbon/hello-fastapitags:-test-demodescription:A dummy function to demonstrate Faastapibuild:plugins:basic-auth:username:adminpassword:passwordmongodb:host:127.0.0.1port:27017database:demosqlite:uri:sqlite:///example.dbasync-sqlite:instrumentation:run:script:hello.pyfunction:say_hellomethod:postinput:name:type:strdefault:Worldoutput:message:strdate:datetimedrivers:Dict[str,str]Considering the filehello.pyis present next tofaastapi.ymlwith the following content:importdatetimedefsay_hello(context,name):return{"message":f"Hello{name}","date":datetime.datetime.now(),"drivers":{"mongodb":str(context.mongodb),"sqlite":str(context.sql),"async-sqlite":str(context.async_sql),},}Note: As you can see function must always accept an argument namedcontext. In this case, sincesqlite,async-sqliteandmongodbplugins where specified, clients are available in the context.When the user runfaastapi function new -f faastapi.yml openfaas, an OpenFaas function will be generated in theopenfaasdirectory.
This function can then be deployed to OpenFaas withfaas-cli:cdopenfaas/
faas-cliup-ffunction.ymlYou can then try the function on OpenFaas portal (http://<OPENFAAS_URL>):You can also access the documentation of the function athttp://<OPENFAAS_URL>/function/hello-faastapi/docs:List of available pluginsSecurity plugins:basic-auth: Enables a basic authentication in your application.Note: Username and password can be configured as in the above exampleoauth2-password: Enable simple oauth2 with password and bearer.Note: At this moment, configuration of user database is not possible, thus usage is limited to default values (username:admin, password:secret)Databases plugins:sqlite: Injects ansqlalchemySessionwithsqlite3engine into the context by default.postgres: Injects an sqlachemySessionwithpsycopg2engine into the context by default.mysql: Injects an sqlalchemySessionwithmysqlclientengine into the context by default.async-sqlite: Injects adatabases.Databaseinstance withaiosqliteengine into the context by default.async-postgres: Injects adatabases.Databaseinstance withasyncpgengine into the context by default.async-mysql: Injects adatabases.Databaseinstance withaiomysqlengine into the context by default.Note: All those plugins accept a single configuration variable:urimongodb: Injects apymongo.Databaseinstance into the context.Note: MongoDB plugin can be configured using the variables:host(default to127.0.0.1),port(default to27017) anddatabase(default totest)redis:  Injects aRedisinstance into the context.Note: Redis plugin can be configured using the variables:host(default to127.0.0.1),port(default to6379) anddb(default to0)