Easily create OpenFaas functions built on top of FastAPI Python framework
Project description
FaastAPI
Easily create OpenFaas functions built on top of FastAPI Python framework
Warning:
This project is in its early phase. It might be subject to a lot of changes and many features might be added/removed.
Overview
FaastAPI
is built on top of FastAPI framework, pydantic library, and [jinja2] library.
It allows developers to quickly generate OpenFaas functions.
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.
Installation
You can install package using pip:
pip install faastapi
By default, the command line interface and plugins are not installed. You can choose to install the cli using the cli
extra:
pip install faastapi[cli]
Example
A simple Faastapi function file (by default Faastapi search files named faastapi.yml
) can be as follow:
---
apiversion: v1
meta:
name: hello-faastapi
version: 0.3.2
image: gcharbon/hello-fastapi
tags:
- test
- demo
description: A dummy function to demonstrate Faastapi
build:
plugins:
basic-auth:
username: admin
password: password
mongodb:
host: 127.0.0.1
port: 27017
database: demo
sqlite:
uri: sqlite:///example.db
async-sqlite:
instrumentation:
run:
script: hello.py
function: say_hello
method: post
input:
name:
type: str
default: World
output:
message: str
date: datetime
drivers: Dict[str,str]
Considering the file hello.py
is present next to faastapi.yml
with the following content:
import datetime
def say_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 named
context
. In this case, sincesqlite
,async-sqlite
andmongodb
plugins where specified, clients are available in the context.
When the user run faastapi function new -f faastapi.yml openfaas
, an OpenFaas function will be generated in the openfaas
directory.
This function can then be deployed to OpenFaas with faas-cli
:
cd openfaas/
faas-cli up -f function.yml
List of available plugins
Security plugins:
basic-auth
: Enables a basic authentication in your application.
Note: Username and password can be configured as in the above example
oauth2-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 an sqlalchemySession
withsqlite3
engine into the context by default.postgres
: Injects an sqlachemySession
withpsycopg2
engine into the context by default.mysql
: Injects an sqlalchemySession
withmysqlclient
engine into the context by default.async-sqlite
: Injects adatabases.Database
instance withaiosqlite
engine into the context by default.async-postgres
: Injects adatabases.Database
instance withasyncpg
engine into the context by default.async-mysql
: Injects adatabases.Database
instance withaiomysql
engine into the context by default.
Note: All those plugins accept a single configuration variable:
uri
mongodb
: Injects apymongo.Database
instance 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 aRedis
instance into the context.
Note: Redis plugin can be configured using the variables:
host
(default to127.0.0.1
),port
(default to6379
) anddb
(default to0
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.