Skip to content

Commit

Permalink
fix: add default template && add tempKey
Browse files Browse the repository at this point in the history
  • Loading branch information
dfounderliu committed Apr 9, 2020
1 parent 8375eea commit 3fff6c7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
2 changes: 1 addition & 1 deletion serverless.component.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: website
version: 0.0.1
version: 0.0.2
author: anycodes
org: anycodes
description: Deploys Tencent Website.
Expand Down
6 changes: 4 additions & 2 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"license": "Apache",
"dependencies": {
"fs-extra": "^8.1.0",
"tencent-component-toolkit": "^1.0.9",
"type": "^2.0.0"
"tencent-component-toolkit": "^1.1.1",
"type": "^2.0.0",
"string-random": "^0.1.3",
"request": "^2.88.2"
}
}
66 changes: 57 additions & 9 deletions src/serverless.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const {Component} = require('@serverless/core')
const path = require('path')
const fs = require('fs')
const request = require("request")
const stringRandom = require('string-random')
const {Cos, Cdn} = require('tencent-component-toolkit')

const templateDownloadUrl = 'https://serverless-templates-1300862921.cos.ap-beijing.myqcloud.com/website-demo.zip'

class Express extends Component {

getDefaultProtocol(protocols) {
Expand All @@ -11,14 +16,54 @@ class Express extends Component {
return 'http'
}

async downloadDefaultZip() {
const scfUrl = templateDownloadUrl
const loacalPath = '/tmp/' + stringRandom(10)
fs.mkdirSync(loacalPath)
return new Promise(function (resolve, reject) {
request(scfUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
let stream = fs.createWriteStream(path.join(loacalPath, 'demo.zip'));
request(scfUrl).pipe(stream).on("close", function (err) {
resolve(path.join(loacalPath, 'demo.zip'));
});
} else {
if (error) {
reject(error);
} else {
reject(new Error("Download template file failed."));
}
}
});
});

}

async deploy(inputs) {
console.log(`Deploying Tencent Website ...`)

// 获取腾讯云密钥信息
const credentials = this.credentials.tencent
if (!this.credentials.tencent.tmpSecrets) {
throw new Error("Please add SLS_QcsRole in your tencent account.")
}
const credentials = {
SecretId: this.credentials.tencent.tmpSecrets.TmpSecretId,
SecretKey: this.credentials.tencent.tmpSecrets.TmpSecretKey,
Token: this.credentials.tencent.tmpSecrets.Token,
}
const appid = this.credentials.tencent.tmpSecrets.appId

// 默认值
const region = inputs.region || "ap-guangzhou"
const output = {}

// 判断是否需要测试模板
if (!inputs.srcOriginal) {
output.templateUrl = templateDownloadUrl
inputs.srcOriginal = inputs.src
inputs.src = await this.downloadDefaultZip()
inputs.srcOriginal.websitePath = "./src"
}

const sourceDirectory = await this.unzip(inputs.src)

Expand All @@ -28,11 +73,11 @@ class Express extends Component {
// 标准化website inputs
const websiteInputs = {
code: {
src: inputs.src.websitePath ? path.join(sourceDirectory, inputs.src.websitePath) : sourceDirectory,
index: inputs.src.index || 'index.html',
error: inputs.src.error || 'error.html',
src: inputs.srcOriginal.websitePath ? path.join(sourceDirectory, inputs.srcOriginal.websitePath) : sourceDirectory,
index: inputs.srcOriginal.index || 'index.html',
error: inputs.srcOriginal.error || 'error.html',
},
bucket: inputs.bucketName + '-' + credentials.tmpSecrets.appId,
bucket: inputs.bucketName + '-' + appid,
region: inputs.region || 'ap-guangzhou',
protocol: inputs.protocol || 'http',
}
Expand Down Expand Up @@ -67,7 +112,6 @@ class Express extends Component {
cdnInputs.serviceType = 'web'
cdnInputs.fwdHost = cosOriginAdd
cdnInputs.origin = cosOriginAdd
console.log(cdnInputs)
tencentCdnOutput = await cdn.deploy(cdnInputs)
protocol = tencentCdnOutput.https ? 'https' : 'http'
cdnResult.push(protocol + '://' + tencentCdnOutput.host + ' (CNAME: ' + tencentCdnOutput.cname + ')')
Expand All @@ -82,9 +126,13 @@ class Express extends Component {

await this.save()
console.log(`Deployed Tencent Website.`)
return {"website": this.getDefaultProtocol(websiteInputs.protocol) + "://" + websiteUrl, "host": cdnResult}

output.website = this.getDefaultProtocol(websiteInputs.protocol) + "://" + websiteUrl
if (cdnResult.length > 0) {
output.host = cdnResult
}

return output
}

async remove(inputs = {}) {
Expand All @@ -100,9 +148,9 @@ class Express extends Component {
const cos = new Cos(credentials, region)
await cos.remove(this.state.website)

if(this.state.cdn){
if (this.state.cdn) {
const cdn = new Cdn(credentials, region)
for(let i=0;i<this.state.cdn.length;i++){
for (let i = 0; i < this.state.cdn.length; i++) {
await cdn.remove(this.state.cdn[i])
}
}
Expand Down

0 comments on commit 3fff6c7

Please sign in to comment.