轻松创建在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第三方库


热门话题
当使用RequestDispatcher时,servlets Java最终没有被调用   java生成对具有可变参数数的方法的调用   java如何使用select子句中的参数化列映射iBATIS的查询?   java无法创建类型为org的插件。阿帕奇。登录中。log4j。果心阿佩德。元素RollingFile的RollingFileAppender   java当子实体和父实体之间存在OneTONE JPA关系时,是否可以将其与父实体一起持久化?   Android上的java Facebook集成fbconnect断开链接   获取方法调用方的java机制   从列表创建Oracle阵列时出现java问题   添加和检索元素的ArrayList的java ArrayList   在java中从字符串中删除无效的XML字符   java如何构建完整生成的maven模块   java如何准确地改变播放所有分辨率midi文件的速度?   shell javac:无效标志:/src/中位_度。ubuntu中的java   java使用从其他类的方法检索到的信息