3 Star 1 Fork 0

HuaweiCloudDeveloper / huaweicloud-Serverless-FunctionGraph-EmailConfirm-Python-Sample

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

华为云 FunctionGraph For Email/Phone Confirm项目演示

1. 说明

本项目将演示通过华为云FunctionGraph实现邮件/手机验证场景,让开发者上手体验FunctionGraph。

1.1 环境准备

  • python3.9.2
  • Pycharm 2022+

1.2 必备PyPI包

  • kafka-python==2.0.2
  • redis==4.3.4
  • python-redis-lock==3.7.0
  • jinja2==3.1.2

1.3 基础云服务能力

1.4 核心代码介绍

  • confirm_api.py 为验证码是否正确业务逻辑
  • create_uuid.py 为创建邮件/手机验证任务业务逻辑
  • email_verify.py 发送邮件验证业务逻辑
  • phone_verify.py 发送手机短信验证业务逻辑
  • email_template/templates/email.html 邮件模板
  • util/config_center.py 为配置中心,负责加载环境变量中的配置信息
  • util/kafka_producer_connect.py 为kafka的生产者实现,负责连接kafka发送消息
  • util/redis_connect.py 为对redis的连接,实现对redis进行增查功能
  • util/response_format.py 为响应的格式化,FunctionGraph配置APIG触发器响应格式是必须带有个别字段。
  • util/phone_sender.py 手机信息发送逻辑
  • util/email_sender.py 邮件发送逻辑

2.项目部署

2.1 本地必备程序

  1. window10操作系统
  2. 下载并安装python3.9.2 。( 注意安装后需要确认是否设置了python环境变量)
  3. 下载并安装Pycharm 2022+
  4. 下载并安装node.js 16.17.0 。( 注意安装后需要确认是否设置了node.js环境变量)

2.2 基础环境搭建

  1. 创建虚拟私有云 VPC (快速入门)
  2. 购买分布式缓存服务 DCS (快速入门)
  3. 购买分布式消息服务Kafka版 (快速入门)
  4. 购买API网关专享版(快速入门)
  5. 开通消息&短信服务(快速入门)
  6. 购买NAT网关,并创建SNAT规则(快速入门)

2.3 部署

  1. 拉取代码
  2. 创建函数
    步骤一 进入FunctionGraph页面
    create_function01
    步骤二 创建函数
    create_function02

  3. 创建委托
    步骤一 进入函数页面-设置-权限。
    create_agency01
    步骤二 创建委托。
    create_agency02
    步骤三 可以根据函数所需云服务定制权限限制的权限,Tenant Administrator为除了iam用户权限所有云服务权限。函数主要用到服务有DMS和VPC的访问权限。
    create_agency03
    步骤四 选择已创建的委托,并点击保存。
    create_agency04

  4. 上传函数
    步骤一
    upload_function01
    步骤二
    upload_function02
    步骤三 因文件命名差异,上传函数需要修改常规设置
    upload_function03
    注意:函数可能存在超时情况,建议执行超时时间可以默认的3秒往上增加数秒,防止超时导致函数执行失败。
  5. 设置VPC
    步骤一 建议选择与其他服务实例(Kafka, redis, SNAT规则)相同的VPC及子网,并点击保存
    set_vpc
  6. 设置环境变量
  • confirm_api.py
    confirm_api_env_value
  • create_uuid.py
    create_uuid_env_value
  • email_verify.py
    email_verify_env_value
  • phone_verify.py
    phone_verify_env_value
    注意:环境变量填写完,请点击保存。

    • KAFKA_CLUSTERS(图为:Kafka实例页面)
      kafka_cluster
      注1:KAFKA_CLUSTERS的值格式应为["ip:port", "ip:port"], 例["110.110.111.11:9092", "110.110.111.12:9092"]

    * REDIS_HOST、REDIS_PORT、REDIS_PASSWORD(图为:分布式缓存服务 Redis版实例页面) ![redis](./image4md/redis.PNG) 注1:REDIS_PASSWORD为创建服务实例时设置密码 注2:这里的Redis服务是分布式缓存服务 Redis版
    * PHONE_URL、PHONE_APP_KEY、PHONE_APP_SECRET(图为:消息&短信服务实例页面) ![phone_value01](./image4md/phone_value01.PNG) PHONE_SENDER ![phone_value02](./image4md/phone_value02.PNG) PHONE_TEMPLATE_ID ![phone_value03](./image4md/phone_value03.PNG)
    * 开启邮箱SMTP服务(以163邮箱为例),[163邮箱登录入口](https://email.163.com) ![163_SMTP](./image4md/163_SMTP.PNG) ![163_SMTP](./image4md/163_SMTP02.PNG) EMAIL_PASSWORD ![163_SMTP](./image4md/163_SMTP03.PNG) EMAIL_HOST、EMAIL_PORT ![163_SMTP](./image4md/163_SMTP04.PNG) EMAIL_USERE、MAIL_SENDER_ADDRESS为账户的邮箱地址,例:********@163.com
  1. 绑定触发器
    confirm_api及create_uuid绑定APIG专享版触发器
    步骤一 创建触发器
    create_trigger01
    步骤二 选择APIG专享版
    create_trigger02
    步骤三 创建成功
    create_trigger03

    email_verify及phone_verify绑定分布式消息服务(Kafka)
    步骤一 创建触发器
    create_trigger01
    步骤二 选择分布式消息服务(Kafka)
    create_trigger02_kafka
    步骤三 创建成功
    create_trigger03_kafka
    注意:主题默认为email_topic和phone_topic

  2. 上传依赖/制作依赖
    步骤一 制作依赖(本步骤可以选择跳过,已有制作好的依赖包直接上传,email_confirm\dependency.zip)

    环境准备 制作函数依赖包推荐在EulerOS环境中进行,原因函数执行的操作系统为EulerOS,使用其他系统打包可能会因为底层依赖库的原因,运行出问题,比如找不到动态链接库。

    编写requirements.txt文件。

    vi /tmp/requirements.txt

    将所有需要的包写入其中,例:

    kafka-python==2.0.2
    redis==4.3.4
    requests==2.28.1
    jinja2==3.1.2

    安装第三方依赖包

    pip install /tmp/requirements.txt --target=/tmp/dependency -i https://repo.huaweicloud.com/repository/pypi/simple

    注意:可能会出现个别包版本不存在的情况,可以尝试更改-i参数后面的镜像源地址

    切换到依赖包的安装目录

    cd /tmp/dependency

    打包所有依赖到zip文件(zip找不到,下载并安装zip)

    zip -rq dependency.zip *

    下载dependency.zip到本地

    步骤二 上传依赖
    create_dependency01
    create_dependency02
    函数页面下方,代码依赖包 create_dependency03
    create_dependency04

  3. API绑定应用
    步骤一 进入APIG界面,点击进入专享版控制台
    API网关
    apig_test01
    步骤二 创建应用
    apig_test02
    步骤三 绑定API
    apig_test03
    步骤四 选定已创建API
    apig_test04

2.4 测试

  1. 本地windows系统部署测试
    步骤一 修改 项目根路径/login_page/login_demo/.env.development文件
    local_test01

    VUE_APP_KEY值为API绑定应用的App_Key
    VUE_APP_SECRET值为API绑定应用的App_Secret
    local_test05
    VUE_APP_DOMAIN(APIG默认分配一个未备案的子域名,如果已有备案域名可以和APIG分组进行绑定)
    local_test02
    注意:因子域名未经过备案,所以DNS无法解析该域名,需要修改本地hosts文件绑定域名和公网IP,具体步骤见2.5常见问题3.windows。

    步骤二 打开终端,切换路径至login_page/login_demo(以下操作Pycharm为例) local_test03

    cd .\login_page\login_demo\  

    步骤三 下载vue.js第三方依赖

    npm install  

    注意:报错找不到命令npm,可能原因为环境变量还未设置或尝试重启Pycharm。
    步骤四 启动前端服务

    npm run serve  

    步骤五 Chrome浏览器打开http://localhost:8080
    local_test06
    local_test07

  2. ECS EulerOS操作系统部署测试
    步骤一 购买ECS服务器并绑定弹性公网IP
    ECS_test01
    ECS_test02
    ECS_test03
    ECS_test04
    下载并安装MobaXterm
    ECS_test05
    连接弹性服务器
    ECS_test06
    ECS_test07
    ECS_test08
    ECS_test09
    步骤二 修改 项目根路径/login_page/login_demo/.env.production文件
    ECS_test14

    VUE_APP_KEY值为API绑定应用的App_Key
    VUE_APP_SECRET值为API绑定应用的App_Secret
    local_test05
    VUE_APP_DOMAIN(APIG默认分配一个未备案的子域名,如果已有备案域名可以和APIG分组进行绑定)
    local_test02
    注意:因子域名未经过备案,所以DNS无法解析该域名,需要修改本地hosts文件绑定域名和公网IP,具体步骤见2.5常见问题3.linux。

    步骤三 构建前端代码上传至ECS服务器
    通过Pycharm打开项目,并打开终端和执行切换目录命令

    cd .\login_page\login_demo\  

    local_test03
    下载vue.js第三方依赖(已下载可以跳过)

    npm install

    ECS_test10
    构建前端代码

    npm run build

    ECS_test11
    ECS_test12
    ECS_test13
    步骤四 命令行安装Nginx

    yum install nginx  

    步骤五 修改Nginx配置文件并启动nginx

    vim /etc/nginx/nginx.conf

    nginx_conf
    注意:root 如图所示填前端构建dist文件夹路径。如果涉及跨域需要修改location部分, proxy_pass为后端服务地址,可使用自动生成的子域名。具体配置可参考项目下nginx.conf文件。
    注意:因子域名未经过备案,所以DNS无法解析该域名,需要修改本地hosts文件绑定域名和公网IP,具体步骤见2.5常见问题3.linux。
    步骤五 访问服务
    打开Chrome浏览器,访问服务器公网IP地址

  3. OBS + DNS部署测试

    前提条件

    1. 拥有已备案通过的公网域名,详见域名注册

    步骤一 APIG实例分组绑定域名
    选择函数所在分组
    APIG_Domain_01
    单击分组信息及绑定独立域名
    APIG_Domain_02
    输入子域名
    APIG_Domain_03
    步骤二 APIG实例分组添加跨域预校验OPTIONS方法
    单击创建API
    create_options_01
    选择配置项
    create_options_02
    create_options_03

    Access-Control-Allow-Origin表示允许跨域的源
    Access-Control-Allow-Credentials表示是否允许带上cookies
    Access-Control-Allow-Methods允许访问的方法
    Access-Control-Allow-Headers允许请求带上的头信息

    步骤三 修改前端代码生产环境配置
    修改配置文件,项目路径/login_page/login_demo/.env.production
    pro_config

    VUE_APP_BASE_URL为http://test.example.com(APIG绑定域名)
    VUE_APP_DOMAIN为test.example.com(APIG绑定域名)

    步骤四 构建前端代码
    通过Pycharm打开项目,并打开终端和执行切换目录命令

    cd .\login_page\login_demo\

    local_test03
    下载vue.js第三方依赖(已下载可以跳过)

    npm install

    ECS_test10
    构建前端代码

    npm run build

    ECS_test11
    ECS_test12
    步骤五 通过OBS Browser+ 上传前端代码
    upload_front_code
    注意:建议按照图中文件路径上传文件
    步骤六 配置静态页面托管
    obs_config_01
    步骤七 配置自定义域名
    obs_config_02
    步骤八 DNS添加记录集
    进入华为云DNS控制台,单击域名注册
    DNS_01
    选择已备案域名,单击解析
    DNS_02
    添加前端静态页面记录集(CNAME类型)
    DNS_front
    值为OBS托管静态页面域名,如下图:
    OBS_domain
    添加APIG记录集(A类型)
    DNS_APIG
    值为APIG入口IP地址,如下图:
    APIG_IP
    步骤九 测试访问
    打开浏览器并访问前端域名

2.5 常见问题

  1. 实例需要打开对应的端口安全组入方向规则,让各服务之间可以互相访问。

  2. 导致各数据库或消息队列实例连接超时原因,一般为安全组入方向规则没有打开或者打开错误。

  3. 未备案域名,服务器配置IP映射域名。

  • windows

修改 C:\WINDOWS\system32\drivers\etc\hosts 文件。

打开cmd,测试域名是否能访问。

ping 域名

  • linux

修改 /etc/hosts 文件。

vim /etc/hosts

ping 域名  

注意:域名是网址的组成部分,例如http://localhost.com/test 中localhost.com为域名

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

华为云-开发者联盟Serverles-FunctionGraph-EmailConfirm-Python示例代码 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/HuaweiCloudDeveloper/huaweicloud-serverless-function-graph-email-confirm-python-sample.git
git@gitee.com:HuaweiCloudDeveloper/huaweicloud-serverless-function-graph-email-confirm-python-sample.git
HuaweiCloudDeveloper
huaweicloud-serverless-function-graph-email-confirm-python-sample
huaweicloud-Serverless-FunctionGraph-EmailConfirm-Python-Sample
master-dev

搜索帮助