轻松创建在fastapi python框架之上构建的openfaas函数

faastapi的Python项目详细描述


faastapi

Easily create OpenFaas functions built on top of FastAPI Python framework

警告:

这个项目正处于早期阶段。它可能会受到很多更改,并且可能会添加/删除许多功能。

概述

FaastAPI构建在FastAPI框架、pydantic库和jinja2库之上。

它允许开发人员快速生成OpenFaas函数。

可以使用几个插件来快速设置rest api。命令行接口可以与包一起安装,以便可以在本地测试函数。

安装

您可以使用pip安装软件包:

pip install faastapi

默认情况下,不安装命令行界面和插件。您可以选择使用附加的cli安装cli:

pip install faastapi[cli]

示例

一个简单的faastapi函数文件(默认情况下,faastapi搜索文件名为faastapi.yml)可以如下:

---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]

考虑到文件hello.py位于faastapi.yml旁边,内容如下:

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 named context. In this case, since sqlite, async-sqlite and mongodb plugins where specified, clients are available in the context.

当用户运行faastapi function new -f faastapi.yml openfaas时,将在openfaas目录中生成openfaas函数。 然后可以使用faas-cli

将此函数部署到openfaas
cd openfaas/
faas-cli up -f function.yml

然后您可以在openfaas portal上尝试该函数(http://<OPENFAAS_URL>):

Screenshot of OpenFaas portal

您还可以在http://<OPENFAAS_URL>/function/hello-faastapi/docs

访问函数的文档

Screenshot of documentation

可用插件列表

安全插件:

  • basic-auth:在应用程序中启用基本身份验证。

Note: Username and password can be configured as in the above example

  • oauth2-password:使用密码和承载启用简单的oauth2。

Note: At this moment, configuration of user database is not possible, thus usage is limited to default values (username: admin, password: secret)

数据库插件:

  • sqlite:默认情况下,将带有sqlite3引擎的sqlalchemySession注入到上下文中。
  • postgres:默认情况下,将带有psycopg2引擎的sqlachemySession注入到上下文中。
  • mysql:默认情况下,将带有mysqlclient引擎的sqlalchemySession注入到上下文中。
  • async-sqlite:默认情况下,将带有aiosqlite引擎的databases.Database实例注入到上下文中。
  • async-postgres:默认情况下,将带有asyncpg引擎的databases.Database实例注入到上下文中。
  • async-mysql:默认情况下,将带有aiomysql引擎的databases.Database实例注入到上下文中。

Note: All those plugins accept a single configuration variable: uri

  • mongodb:向上下文中注入一个pymongo.Database实例。

Note: MongoDB plugin can be configured using the variables: host (default to 127.0.0.1), port (default to 27017) and database (default to test)

  • redis:向上下文中注入一个Redis实例。

Note: Redis plugin can be configured using the variables: host (default to 127.0.0.1), port (default to 6379) and db (default to 0)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java为什么加载个人密钥库需要这么多时间?   当我使用main创建Android应用程序UI时,如何通过java修改它。xml文件?   java Tomcat 6和7:WebappClassLoader:尝试为名称org/apache/openjpa/persistence/osgi/BundleUtils复制类定义   java`parseInt()`和`parseDouble()`throw`NumberFormatExeption`   JavaSpringMongoDB填充引用   在LinuxMint中打开Eclipse时发生java错误;OpenJDK 64位服务器VM警告:忽略选项MaxPermSize=512m;支持在8.0中被删除   使用PKCS7Padding的AES CBC加密在Java和Objective中有不同的结果   java为什么Jackson要用一个以类命名的额外层来包装我的对象?   json在Java中使用parallelStream提取值   JavaSpring存储库自动生成方法:按给定的顶部编号+按字段描述排序选择   java是否有可序列化的标准闭包接口?   .NET与Java在初创公司的web应用程序开发   如何修复java。java中的lang.unsatifiedLinkError   JavaFX+Spring Boot+Hibernate应用程序对多个环境的java支持   自定义视图组中的java更改未呈现