Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03bbbae
commit 24f0af0
Showing
7 changed files
with
175 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,48 @@ | ||
# aligi | ||
|
||
为无服务函数计算作为阿里云 API 后端运行提供转析。 | ||
|
||
## 为什么会有这个库? | ||
|
||
使用 Flask,Django 等支持标准 WSGI 协议的 Web 框架创建无服务函数时,使用HTTP触发器才能让它们正常运行。 | ||
|
||
但如果想使用无服务函数作为阿里云 API 网关的后端时,则无法直接使用这些函数,只能通过网络调用,这显然是不够高效、并且费钱的。 | ||
|
||
## 如何安装 | ||
|
||
在项目根目录下执行 | ||
|
||
``` | ||
pip install -t . aligi | ||
``` | ||
|
||
## 如何使用 | ||
|
||
以下为一个最小的 Flask 样例 | ||
|
||
```python | ||
import json | ||
|
||
from flask import Flask | ||
from aligi.wsgi import WSGI | ||
from aligi.types import FCContext | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route('/') | ||
def hello_world(): | ||
return 'Hello, World!' | ||
|
||
|
||
def handler(event: str, context: FCContext): | ||
"""阿里云无服务函数的入口""" | ||
wsgi = WSGI(event, context) | ||
wsgi.mount(app) | ||
return wsgi.get_response() | ||
``` | ||
|
||
其中`app`可以是任意一个标准 WSGI Application。 | ||
|
||
在 Django 项目中,它一般在项目的`wsgi.py`里。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
VERSION = (0, 0, 0) | ||
VERSION = (0, 0, 1) | ||
|
||
__version__ = '.'.join(map(str, VERSION)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import json | ||
|
||
from flask import Flask | ||
from aligi.wsgi import WSGI | ||
from aligi.types import FCContext | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route('/') | ||
def hello_world(): | ||
return 'Hello, World!' | ||
|
||
|
||
def handler(event: str, context: FCContext): | ||
"""阿里云无服务函数的入口""" | ||
wsgi = WSGI(event, context) | ||
wsgi.mount(app) | ||
return wsgi.get_response() | ||
|
||
|
||
if __name__ == "__main__": | ||
print(handler(json.dumps({ | ||
"path": "/", | ||
"httpMethod": "GET", | ||
"headers": { | ||
"Content-Length": 0, | ||
"Content-Type": "text/plain" | ||
}, | ||
"queryParameters": {}, | ||
"pathParameters": {}, | ||
"body": "", | ||
"isBase64Encoded": False | ||
}), None)) |