Skip to content

Commit

Permalink
Merge pull request #1 from devsapp/add-other-examples
Browse files Browse the repository at this point in the history
Add other examples
  • Loading branch information
rsonghuster authored Aug 30, 2021
2 parents b771bec + 4f4ecab commit e634a86
Show file tree
Hide file tree
Showing 27 changed files with 374 additions and 8 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
python -m pip install --upgrade pip
pip install setuptools wheel twine
pip install requests
- name: fc-mysql-python
env:
publish_token: ${{ secrets.alibaba_registry_publish_token }}
Expand All @@ -29,4 +30,34 @@ jobs:
wget https://serverless-registry.oss-cn-hangzhou.aliyuncs.com/publish-file/python3/hub-publish.py
ls
python hub-publish.py
cd ..
- name: fc-mongodb-python
env:
publish_token: ${{ secrets.alibaba_registry_publish_token }}
run: |
cd mongodb
wget https://serverless-registry.oss-cn-hangzhou.aliyuncs.com/publish-file/python3/hub-publish.py
ls
python hub-publish.py
cd ..
- name: fc-sql-server-python
env:
publish_token: ${{ secrets.alibaba_registry_publish_token }}
run: |
cd sql_server
wget https://serverless-registry.oss-cn-hangzhou.aliyuncs.com/publish-file/python3/hub-publish.py
ls
python hub-publish.py
cd ..
- name: fc-redis-python
env:
publish_token: ${{ secrets.alibaba_registry_publish_token }}
run: |
cd redis
wget https://serverless-registry.oss-cn-hangzhou.aliyuncs.com/publish-file/python3/hub-publish.py
ls
python hub-publish.py
cd ..
20 changes: 20 additions & 0 deletions mongodb/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Type: Application
Name: start-fc-mongodb-python
Version: 0.0.2
Provider:
- 阿里云
Description: 快速部署一个 Python 访问 mongodb 的函数到阿里云函数计算。
HomePage: https://github.com/devsapp/start-fc-db-python
Tags:
- 函数计算
- 你好世界
- 新手入门
Category: 新手入门
Service:
函数计算:
Authorities:
- 创建函数
- 删除函数
- 创建服务
- 删除服务
Runtime: Python
12 changes: 12 additions & 0 deletions mongodb/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 阿里云函数计算 Python 访问 mongodb 数据库

只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 mongodb 数据库

- 初始化项目:`s init start-fc-mongodb-python -d start-fc-mongodb-python`
- 进入项目:`cd start-fc-mongodb-python`
- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的
- 构建项目:`s build --use-docker`
- 部署项目:`s deploy`
- 触发项目:`s invoke`

即可实现`Python 访问 mongodb`案例的初始化、部署整个流程。
16 changes: 16 additions & 0 deletions mongodb/src/code/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
import os
from pymongo import MongoClient


def handler(event, context):
dbName = os.environ['MONGO_DATABASE']
url = os.environ['MONGO_URL']

client = MongoClient(url)

col = client[dbName]['fc_col']
col.insert_one(dict(DEMO="FC", MSG="Hello FunctionCompute For MongoDB"))
doc = col.find_one(dict(DEMO="FC"))
print('find documents:' + str(doc))
return str(doc)
1 change: 1 addition & 0 deletions mongodb/src/code/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pymongo
12 changes: 12 additions & 0 deletions mongodb/src/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 阿里云函数计算 Python 访问 mongodb 数据库

只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 mongodb 数据库

- 初始化项目:`s init start-fc-mongodb-python -d start-fc-mongodb-python`
- 进入项目:`cd start-fc-mongodb-python`
- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的
- 构建项目:`s build --use-docker`
- 部署项目:`s deploy`
- 触发项目:`s invoke`

即可实现`Python 访问 mongodb`案例的初始化、部署整个流程。
25 changes: 25 additions & 0 deletions mongodb/src/s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
edition: 1.0.0 # 命令行YAML规范版本,遵循语义化版本(Semantic Versioning)规范
name: fcDeployApp # 项目名称
access: "{{ access }}" # 秘钥别名

services:
fc-db-mongodb-python: # 服务名称
component: devsapp/fc # 组件名称
props: # 组件的属性值
region: cn-hangzhou
service:
name: fc-db-demo
description: 'demo for fc visit db'
internetAccess: true
function:
name: mongodb
description: visit mongodb
runtime: python3
codeUri: ./code
handler: index.handler
memorySize: 256
timeout: 60
environmentVariables:
MONGO_DATABASE: <your mongo database>
MONGO_URL: <your mongo url>
# 函数计算FC组件文档参考地址:https://github.com/devsapp/fc
1 change: 1 addition & 0 deletions mongodb/version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- 新版本支持
2 changes: 1 addition & 1 deletion mysql/publish.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Type: Application
Name: start-fc-mysql-python
Version: 0.0.1
Version: 0.0.2
Provider:
- 阿里云
Description: 快速部署一个 Python 访问 mysql 的函数到阿里云函数计算。
Expand Down
2 changes: 1 addition & 1 deletion mysql/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- 初始化项目:`s init start-fc-mysql-python -d start-fc-mysql-python`
- 进入项目:`cd start-fc-mysql-python`
- 将 s.yaml 中的环境变量修改成您自己的值
- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的
- 构建项目:`s build --use-docker`
- 部署项目:`s deploy`
- 触发项目:`s invoke`
Expand Down
7 changes: 2 additions & 5 deletions mysql/src/code/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging
import pymysql
import os
import sys

logger = logging.getLogger()

Expand All @@ -21,15 +20,13 @@ def getConnection():
logger.error(e)
logger.error(
"ERROR: Unexpected error: Could not connect to MySql instance.")
sys.exit()
raise Exception(str(e))


def handler(event, context):
conn = getConnection()
try:
conn = getConnection()
with conn.cursor() as cursor:
sql = "REPLACE INTO users (id, name) VALUES(%s, %s)"
cursor.execute(sql, ('2', 'wan'))
cursor.execute("SELECT * FROM users")
result = cursor.fetchone()
logger.info(result)
Expand Down
2 changes: 1 addition & 1 deletion mysql/src/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- 初始化项目:`s init start-fc-mysql-python -d start-fc-mysql-python`
- 进入项目:`cd start-fc-mysql-python`
- 将 s.yaml 中的环境变量修改成您自己的值
- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的
- 构建项目:`s build --use-docker`
- 部署项目:`s deploy`
- 触发项目:`s invoke`
Expand Down
39 changes: 39 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,45 @@

即可实现`Python 访问 mysql`案例的初始化、部署整个流程。

### redis

只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 redis 数据库

- 初始化项目:`s init start-fc-redis-python -d start-fc-redis-python`
- 进入项目:`cd start-fc-redis-python`
- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的
- 构建项目:`s build --use-docker`
- 部署项目:`s deploy`
- 触发项目:`s invoke`

即可实现`Python 访问 redis`案例的初始化、部署整个流程。

### mongodb

只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 mongodb 数据库

- 初始化项目:`s init start-fc-mongodb-python -d start-fc-mongodb-python`
- 进入项目:`cd start-fc-mongodb-python`
- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的
- 构建项目:`s build --use-docker`
- 部署项目:`s deploy`
- 触发项目:`s invoke`

即可实现`Python 访问 mongodb`案例的初始化、部署整个流程。

### sql-server

只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 mysql 数据库

- 初始化项目:`s init start-fc-sql-server-python -d start-fc-sql-server-python`
- 进入项目:`cd start-fc-sql-server-python`
- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的
- 构建项目:`s build --use-docker`
- 部署项目:`s deploy`
- 触发项目:`s invoke`

即可实现`Python 访问 sql server`案例的初始化、部署整个流程。

## 参考

> - Serverless Devs 项目:https://www.github.com/serverless-devs/serverless-devs
Expand Down
20 changes: 20 additions & 0 deletions redis/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Type: Application
Name: start-fc-redis-python
Version: 0.0.2
Provider:
- 阿里云
Description: 快速部署一个 Python 访问 redis 的函数到阿里云函数计算。
HomePage: https://github.com/devsapp/start-fc-db-python
Tags:
- 函数计算
- 你好世界
- 新手入门
Category: 新手入门
Service:
函数计算:
Authorities:
- 创建函数
- 删除函数
- 创建服务
- 删除服务
Runtime: Python
12 changes: 12 additions & 0 deletions redis/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 阿里云函数计算 Python 访问 redis 数据库

只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 redis 数据库

- 初始化项目:`s init start-fc-redis-python -d start-fc-redis-python`
- 进入项目:`cd start-fc-redis-python`
- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的
- 构建项目:`s build --use-docker`
- 部署项目:`s deploy`
- 触发项目:`s invoke`

即可实现`Python 访问 redis`案例的初始化、部署整个流程。
29 changes: 29 additions & 0 deletions redis/src/code/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
import os
import redis

conn_pool = None


def initializer(context):
global conn_pool
conn_pool = redis.ConnectionPool(
host=os.environ['REDIS_HOST'], password=os.environ['REDIS_PASSWORD'],
port=os.environ['REDIS_PORT'], db=1, decode_responses=True)


def handler(event, context):
global conn_pool
r = redis.Redis(connection_pool=conn_pool)

counter = r.get('counter')

if counter is None:
counter = 0
else:
counter = int(counter)

print('counter: ' + str(counter))

r.set('counter', str(counter + 1))
return counter
1 change: 1 addition & 0 deletions redis/src/code/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
redis
12 changes: 12 additions & 0 deletions redis/src/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 阿里云函数计算 Python 访问 redis 数据库

只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 redis 数据库

- 初始化项目:`s init start-fc-redis-python -d start-fc-redis-python`
- 进入项目:`cd start-fc-redis-python`
- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的
- 构建项目:`s build --use-docker`
- 部署项目:`s deploy`
- 触发项目:`s invoke`

即可实现`Python 访问 redis`案例的初始化、部署整个流程。
28 changes: 28 additions & 0 deletions redis/src/s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
edition: 1.0.0 # 命令行YAML规范版本,遵循语义化版本(Semantic Versioning)规范
name: fcDeployApp # 项目名称
access: "{{ access }}" # 秘钥别名

services:
fc-db-redis-python: # 服务名称
component: devsapp/fc # 组件名称
props: # 组件的属性值
region: cn-hangzhou
service:
name: fc-db-demo
description: 'demo for fc visit db'
internetAccess: true
function:
name: redis
description: visit redis
runtime: python3
codeUri: ./code
handler: index.handler
memorySize: 256
timeout: 30
initializationTimeout: 60
initializer: index.initializer
environmentVariables:
REDIS_HOST: <your redis host>
REDIS_PASSWORD: <your redis pwd>
REDIS_PORT: <your redis port>
# 函数计算FC组件文档参考地址:https://github.com/devsapp/fc
1 change: 1 addition & 0 deletions redis/version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- 新版本支持
20 changes: 20 additions & 0 deletions sql_server/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Type: Application
Name: start-fc-sql-server-python
Version: 0.0.2
Provider:
- 阿里云
Description: 快速部署一个 Python 访问 sql-server 的函数到阿里云函数计算。
HomePage: https://github.com/devsapp/start-fc-db-python
Tags:
- 函数计算
- 你好世界
- 新手入门
Category: 新手入门
Service:
函数计算:
Authorities:
- 创建函数
- 删除函数
- 创建服务
- 删除服务
Runtime: Python
12 changes: 12 additions & 0 deletions sql_server/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 阿里云函数计算 Python 访问 sql server 数据库

只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 mysql 数据库

- 初始化项目:`s init start-fc-sql-server-python -d start-fc-sql-server-python`
- 进入项目:`cd start-fc-sql-server-python`
- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的
- 构建项目:`s build --use-docker`
- 部署项目:`s deploy`
- 触发项目:`s invoke`

即可实现`Python 访问 sql server`案例的初始化、部署整个流程。
Loading

0 comments on commit e634a86

Please sign in to comment.