Skip to content

Commit 54d4bfa

Browse files
author
amazon-meaisiah
authored
Merge pull request awslabs#370 from awslabs/meaisiah-assets-fix
Fix asset uploader, tweak backend and dev setup
2 parents ac41897 + ef84208 commit 54d4bfa

File tree

6 files changed

+64
-76
lines changed

6 files changed

+64
-76
lines changed

cloudformation/template.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,8 @@ Resources:
13921392
CallbackURL: !If [ LocalDevelopmentMode,
13931393
[
13941394
'http://localhost:3000/index.html?action=login',
1395-
!Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName, '/index.html?action=login' ]]
1395+
!Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName, '/index.html?action=login' ]],
1396+
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=login' ]]
13961397
],
13971398
[
13981399
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=login' ]]
@@ -1402,12 +1403,10 @@ Resources:
14021403
[
14031404
'http://localhost:3000/index.html?action=logout',
14041405
!Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName, '/index.html?action=logout' ]],
1405-
'http://localhost:3000/index.html?action=logout&reason=inactive',
1406-
!Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName, '/index.html?action=logout&reason=inactive' ]]
1406+
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=logout' ]],
14071407
],
14081408
[
14091409
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=logout' ]],
1410-
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=logout&reason=inactive' ]]
14111410
]
14121411
]
14131412
AllowedOAuthFlowsUserPoolClient: true

lambdas/backend/routes/apikey.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,22 @@
33
const customersController = require('dev-portal-common/customers-controller')
44
const util = require('../util')
55

6-
exports.get = (req, res) => {
6+
exports.get = async (req, res) => {
77
const cognitoIdentityId = util.getCognitoIdentityId(req)
88
console.log(`GET /apikey for Cognito ID: ${cognitoIdentityId}`)
99

10-
function errFunc (data) {
11-
console.log(`error: ${data}`)
12-
res.status(500).json(data)
13-
}
10+
const data = await new Promise((resolve, reject) => {
11+
customersController.getApiKeyForCustomer(cognitoIdentityId, reject, resolve)
12+
})
1413

15-
customersController.getApiKeyForCustomer(cognitoIdentityId, errFunc, (data) => {
16-
if (data.items.length === 0) {
17-
res.status(404).json({ error: 'No API Key for customer' })
18-
} else {
19-
const item = data.items[0]
20-
const key = {
21-
id: item.id,
22-
value: item.value
23-
}
24-
res.status(200).json(key)
14+
if (data.items.length === 0) {
15+
res.status(404).json({ error: 'No API Key for customer' })
16+
} else {
17+
const item = data.items[0]
18+
const key = {
19+
id: item.id,
20+
value: item.value
2521
}
26-
})
22+
res.status(200).json(key)
23+
}
2724
}

lambdas/backend/routes/catalog.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
const util = require('../util')
44

5-
exports.get = (req, res) => {
5+
exports.get = async (req, res) => {
66
console.log(`GET /catalog for Cognito ID: ${util.getCognitoIdentityId(req)}`)
7-
util.catalog()
8-
.then(catalog => res.status(200).json(catalog))
9-
.catch(error => res.status(error.statusCode).json(error))
7+
const catalog = await util.catalog()
8+
res.status(200).json(catalog)
109
}

lambdas/backend/routes/feedback.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,25 @@ const util = require('../util')
55

66
const feedbackEnabled = !!process.env.FeedbackSnsTopicArn
77

8-
exports.get = (req, res) => {
8+
exports.get = async (req, res) => {
99
console.log(`GET /feedback for Cognito ID: ${util.getCognitoIdentityId(req)}`)
1010

1111
if (!feedbackEnabled) {
1212
res.status(401).json('Customer feedback not enabled')
1313
} else {
14-
feedbackController.fetchFeedback()
15-
.then(feedback => {
16-
res.status(200).json(feedback)
17-
})
18-
.catch(err => {
19-
console.log(`error: ${err}`)
20-
res.status(500).json(err)
21-
})
14+
const feedback = await feedbackController.fetchFeedback()
15+
res.status(200).json(feedback)
2216
}
2317
}
2418

25-
exports.post = (req, res) => {
19+
exports.post = async (req, res) => {
2620
const cognitoIdentityId = util.getCognitoIdentityId(req)
2721
console.log(`POST /feedback for Cognito ID: ${cognitoIdentityId}`)
2822

2923
if (!feedbackEnabled) {
3024
res.status(401).json('Customer feedback not enabled')
3125
} else {
32-
feedbackController.submitFeedback(cognitoIdentityId, req.body.message)
33-
.then(() => res.status(200).json('success'))
34-
.catch((err) => res.status(500).json(err))
26+
await feedbackController.submitFeedback(cognitoIdentityId, req.body.message)
27+
res.status(200).json('success')
3528
}
3629
}

lambdas/backend/routes/subscriptions/usage.js

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,37 @@
33
const customersController = require('dev-portal-common/customers-controller')
44
const util = require('../../util')
55

6-
exports.get = (req, res) => {
6+
exports.get = async (req, res) => {
77
const cognitoIdentityId = util.getCognitoIdentityId(req)
8-
console.log(`GET /usage for Cognito ID: ${cognitoIdentityId}`)
8+
console.log(`GET /subscriptions/:usagePlanId/usage for Cognito ID: ${cognitoIdentityId}`)
99
const usagePlanId = req.params.usagePlanId
1010

11-
function errFunc (data) {
12-
console.log(`error: ${data}`)
13-
res.status(500).json(data)
14-
}
15-
16-
util.catalog()
17-
.then(catalog => util.getUsagePlanFromCatalog(usagePlanId, catalog))
18-
.then(usagePlan => {
19-
const isUsagePlanInCatalog = Boolean(usagePlan)
11+
const catalog = await util.catalog()
12+
const usagePlan = util.getUsagePlanFromCatalog(usagePlanId, catalog)
2013

21-
// could error here if customer is not subscribed to usage plan, or save an extra request by just showing 0 usage
22-
if (!isUsagePlanInCatalog) {
23-
res.status(404).json({ error: 'Invalid Usage Plan ID' })
24-
} else {
25-
customersController.getApiKeyForCustomer(cognitoIdentityId, errFunc, (data) => {
26-
const keyId = data.items[0].id
14+
// could error here if customer is not subscribed to usage plan, or save an extra request by just showing 0 usage
15+
if (!usagePlan) {
16+
res.status(404).json({ error: 'Invalid Usage Plan ID' })
17+
} else {
18+
const data = await new Promise((resolve, reject) => {
19+
customersController.getApiKeyForCustomer(cognitoIdentityId, reject, resolve)
20+
})
21+
const keyId = data.items[0].id
2722

28-
const params = {
29-
endDate: req.query.end,
30-
startDate: req.query.start,
31-
usagePlanId,
32-
keyId,
33-
limit: 1000
34-
}
23+
const params = {
24+
endDate: req.query.end,
25+
startDate: req.query.start,
26+
usagePlanId,
27+
keyId,
28+
limit: 1000
29+
}
3530

36-
util.apigateway.getUsage(params, (err, usageData) => {
37-
if (err) {
38-
console.log(`get usage err ${JSON.stringify(err)}`)
39-
errFunc(err)
40-
} else {
41-
console.log(`get usage data ${JSON.stringify(usageData)}`)
42-
res.status(200).json(usageData)
43-
}
44-
})
45-
})
46-
}
31+
const usageData = await new Promise((resolve, reject) => {
32+
util.apigateway.getUsage(params, (err, data) => {
33+
if (err) reject(err)
34+
else resolve(data)
35+
})
4736
})
37+
res.status(200).json(usageData)
38+
}
4839
}

lambdas/static-asset-uploader/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const path = require('path')
44
const AWS = require('aws-sdk')
55
const notifyCFN = require('dev-portal-common/notify-cfn')
6+
const { inspectStringify } = require('dev-portal-common/inspect-stringify')
67
const fs = require('fs')
78
const klaw = require('klaw')
89
// const crypto = require('crypto')
@@ -67,9 +68,9 @@ async function cleanS3Bucket (bucketName) {
6768
Bucket: bucketName
6869
}).promise()
6970

70-
console.log(`result: ${JSON.stringify(result, null, 4)}`)
71+
console.log(`result: ${inspectStringify(result)}`)
7172
const keys = result.Contents.map((obj) => {
72-
console.log(`obj: ${JSON.stringify(obj)}`)
73+
console.log(`obj: ${inspectStringify(obj)}`)
7374
return { Key: obj.Key }
7475
})
7576

@@ -81,7 +82,7 @@ async function cleanS3Bucket (bucketName) {
8182
Objects: keys
8283
}
8384
})
84-
console.log(`deleteObjects result: ${JSON.stringify(result, null, 4)}`)
85+
console.log(`deleteObjects result: ${inspectStringify(result)}`)
8586
}
8687
}
8788

@@ -193,7 +194,7 @@ class State {
193194

194195
try {
195196
const readResults = await new Promise((resolve, reject) => {
196-
fs.readFile(filePath, 'utf-8', (err, data) => {
197+
fs.readFile(filePath, null, (err, data) => {
197198
if (err) reject(err)
198199
else resolve(data)
199200
})
@@ -210,6 +211,13 @@ class State {
210211
params.ACL = 'public-read'
211212
}
212213

214+
// body just pollutes logs and takes up space
215+
console.log('uploading to s3', {
216+
Bucket: params.Bucket,
217+
Key: params.Key,
218+
BodyLength: params.Body.byteLength,
219+
ContentType: params.ContentType
220+
})
213221
uploadPromises.push(s3.upload(params, options).promise())
214222
} catch (error) {
215223
console.log('Failed to upload:', error)
@@ -262,6 +270,7 @@ class State {
262270
console.log('pushing b/c Create', data.path)
263271
} else if (this.event.ResourceProperties.RebuildMode === 'overwrite-content') {
264272
// always write everything on an overwrite
273+
console.log('pushing b/c RebuildMode=overwrite-content', data.path)
265274
} else if (!/build\/custom-content/.test(data.path)) {
266275
// only write non custom-content files on everything else
267276
console.log('pushing b/c not custom', data.path)

0 commit comments

Comments
 (0)