기반 서버를 사용하지 않는 + 파이썬, 말초 COS에 배포 http://suo.im 단축 URL API를 사용하여
용도
설치 서버를 사용하지 않는 프레임 워크
npm install -g serverless
프로젝트 다운로드
https://gitee.com/yu-se/short-link.git 다운로드 소스 패키지 자식 복제 또는 실행에서
git clone [email protected]:yu-se/short-link.git
배포 리소스
실행
serverless --debug
기록의 서버를 성공적으로 배포 주소로 자료를 배포 한 후수정 요청 주소
주소 변경 요청 웹 / 어드레스 서버없는 index.html을도 1의 선택된 부분에 대한 수정.
자원의 재배치
다시 실행
serverless --debug
자원의 배치를액세스
링크를 myWebsite
수정 KEY (선택 사항)
로그인 http://suo.im/ , 사용자 배경> 짧은 URL> API 인터페이스는, 키를 index.py / 코드를 대체 키를 복사
개발
API를 사용 Http://suo.im, 파이썬 사용 요청 요청 API를, 그래서 요청의 환경 SCF를 가지고
import requests
from urllib import parse
api = 'http://suo.im/api.htm'
key = '5e51fa20b1b63c4fd98eaa4a@6599fbb9164b4bc7eb440746c0c1a1fc'
def main_handler(event, context):
print(event)
url = event['queryString']['url']
print(url)
if url == None:
return {'code':400,'info':'url is none'}
res = requests.get(api+'?key='+key+'&url='+parse.quote(url))
print(res.text)
return {'code':200,'url':res.text}
URL은 테이프 및 URL 매개 변수 절단을 방지하기 위해, 코딩를 urlencode를 사용
클라우드 지역 디버깅 기능의 경우,이 방법을 사용하면, 큰 형님 anycodes을 언급 코드 APIGW 트리거를 디버깅 증가, 로컬로 실행 될 수있는 클라우드의 함수이다.
if __name__ == "__main__":
event = {}
print(main_handler(event,None))
필요에 따라 이벤트가 클라우드 기능 세부 정보 페이지에서 APIGW 이벤트 템플릿 인 경우, 다음 기능 코드를 클릭> 이벤트 템플릿> 새 템플릿> API 광고 템플릿 다음은 복사, 수정
프런트 엔드 VUE + 요소 + Axios의, CDN 방법을 사용하여 도입
요청 URL의 프론트 엔드 &
를 urlencode 코딩 후 문자로 대체 %26
동일한 URL 매개 변수를 방지하기 위해, 잘립니다.
서버를 사용하지 않는 두 가지 구성 요소가 사용 @serverless/tencent-scf
하고@serverless/tencent-website
@serverless/tencent-scf
배포 백엔드 인터페이스를 사용 apigw
@serverless/tencent-website
선단부에 업로드 왜냐하면
다음 구성은 serverless.yml입니다
shortLink:
component: '@serverless/tencent-scf'
inputs:
name: shortLink
enableRoleAuth: ture
codeUri: ./code
handler: index.main_handler
runtime: Python3.6
region: ap-guangzhou
description: My Serverless Function
memorySize: 64
timeout: 20
events:
- apigw:
name: serverless
parameters:
protocols:
- http
serviceName: serverless
description: the serverless service
environment: release
endpoints:
- path: /
method: GET
description: Serverless REST API
enableCORS: TRUE
serviceTimeout: 10
myWebsite:
component: '@serverless/tencent-website'
inputs:
code:
src: ./web
index: index.html
error: index.html
region: ap-guangzhou
bucketName: short-link
프로젝트 주소
https://gitee.com/yu-se/short-link