custom示例代码:
    51ab7acc-929c-40fb-8247-d50a3a60304f.zip

    项目目录

    1. -rwxrwxrwx@ 1 apple staff 123B 4 21 18:40 Funfile # 必须,依赖包
    2. -rwxrwxrwx@ 1 apple staff 27B 4 21 14:59 bootstrap # 必须,启动脚本
    3. -rw------- 1 apple staff 231B 4 21 18:34 index.py # 主函数
    4. -rwxrwxrwx 1 apple staff 1.1K 4 21 18:25 template.packaged.yml
    5. -rw------- 1 apple staff 863B 4 21 18:34 template.yml # fc配置

    发布

    1. fun install
    2. fun package -b tianyunperfect
    3. fun deploy

    Funfile

    1. RUNTIME custom
    2. RUN PYTHONUSERBASE=/code/.fun/python # 这一行不一定需要,我的mac是必须要加的
    3. RUN fun-install pip install fastapi
    4. RUN fun-install pip install uvicorn

    bootstrap

    1. #!/bin/bash
    2. python index.py

    index.py

    1. from fastapi import FastAPI
    2. app = FastAPI()
    3. @app.get('/random/') # 点get就支持get请求
    4. def read_root():
    5. return {"hello":'world'}
    6. if __name__ == '__main__':
    7. import uvicorn
    8. uvicorn.run(app,host='0.0.0.0',port=9000)

    template.yml

    1. ROSTemplateFormatVersion: '2015-09-01'
    2. Transform: 'Aliyun::Serverless-2018-04-03'
    3. Resources:
    4. FastAPI-example: # service name
    5. Type: 'Aliyun::Serverless::Service'
    6. Properties:
    7. Description: This is FC service
    8. FastAPI-example: # function name
    9. Type: 'Aliyun::Serverless::Function'
    10. Properties:
    11. Handler: index.handler
    12. Runtime: custom
    13. CodeUri: ./
    14. MemorySize: 128
    15. Timeout: 120
    16. Events:
    17. httpTrigger:
    18. Type: HTTP
    19. Properties:
    20. AuthType: ANONYMOUS
    21. Methods: ['GET', 'POST', 'PUT']