-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/refactor server router #290
Changes from all commits
b2457b9
aec6f41
896cb5a
4e8de81
12989de
6952a2e
c17d10a
366f3a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2021 Uber Technologies Inc. | ||
// | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
const buildQueryString = ( | ||
startTime, | ||
endTime, | ||
{ status, workflowId, workflowName } | ||
) => | ||
[ | ||
`CloseTime >= "${startTime.toISOString()}"`, | ||
`CloseTime <= "${endTime.toISOString()}"`, | ||
status && `CloseStatus = "${status}"`, | ||
workflowId && `WorkflowID = "${workflowId}"`, | ||
workflowName && `WorkflowType = "${workflowName}"`, | ||
] | ||
.filter(subQuery => !!subQuery) | ||
.join(' and '); | ||
|
||
module.exports = buildQueryString; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) 2021 Uber Technologies Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
const buildQueryString = require('./build-query-string'); | ||
const listWorkflows = require('./list-workflows'); | ||
const mapHistoryResponse = require('./map-history-response'); | ||
const momentToLong = require('./moment-to-long'); | ||
const replacer = require('./replacer'); | ||
|
||
module.exports = { | ||
buildQueryString, | ||
listWorkflows, | ||
mapHistoryResponse, | ||
momentToLong, | ||
replacer, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2021 Uber Technologies Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
const moment = require('moment'); | ||
const momentToLong = require('./moment-to-long'); | ||
|
||
async function listWorkflows(state, ctx) { | ||
const q = ctx.query || {}, | ||
startTime = moment(q.startTime || NaN), | ||
endTime = moment(q.endTime || NaN); | ||
|
||
ctx.assert(startTime.isValid() && endTime.isValid(), 400); | ||
|
||
ctx.body = await ctx.cadence[state + 'Workflows']({ | ||
StartTimeFilter: { | ||
earliestTime: momentToLong(startTime), | ||
latestTime: momentToLong(endTime), | ||
}, | ||
typeFilter: q.workflowName ? { name: q.workflowName } : undefined, | ||
executionFilter: q.workflowId ? { workflowId: q.workflowId } : undefined, | ||
statusFilter: q.status || undefined, | ||
nextPageToken: q.nextPageToken | ||
? Buffer.from(q.nextPageToken, 'base64') | ||
: undefined, | ||
}); | ||
} | ||
|
||
module.exports = listWorkflows; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2021 Uber Technologies Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
const replacer = require('./replacer'); | ||
|
||
const mapHistoryResponse = history => { | ||
if (Array.isArray(history && history.events)) { | ||
return history.events.map(e => { | ||
const attr = e.eventType | ||
? e.eventType.charAt(0).toLowerCase() + | ||
e.eventType.slice(1) + | ||
'EventAttributes' | ||
: ''; | ||
|
||
const details = e[attr] && JSON.parse(JSON.stringify(e[attr]), replacer); | ||
|
||
return { | ||
timestamp: e.timestamp, | ||
eventType: e.eventType, | ||
eventId: e.eventId, | ||
details, | ||
}; | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = mapHistoryResponse; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2021 Uber Technologies Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
const replacer = (key, value) => { | ||
if (value && value.type && value.type === 'Buffer') { | ||
return Buffer.from(value) | ||
.toString() | ||
.replace(/["]/g, '') | ||
.trim(); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
module.exports = replacer; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright (c) 2017-2021 Uber Technologies Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. file moved from |
||
// Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
const Router = require('koa-router'); | ||
|
||
const { | ||
domainAuthorizationHandler, | ||
domainHandler, | ||
domainListHandler, | ||
featureFlagHandler, | ||
healthHandler, | ||
tasklistHandler, | ||
tasklistPartitionListHandler, | ||
tasklistPollerListHandler, | ||
workflowArchivedListHandler, | ||
workflowExportHandler, | ||
workflowHandler, | ||
workflowHistoryHandler, | ||
workflowListHandler, | ||
workflowQueryHandler, | ||
workflowQueryTypeHandler, | ||
workflowSignalHandler, | ||
workflowTerminateHandler, | ||
} = require('./routes'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
const { listWorkflows } = require('./helpers'); | ||
|
||
const router = new Router(); | ||
|
||
router.get('/api/domains', domainListHandler); | ||
router.get('/api/domains/:domain', domainHandler); | ||
|
||
/** | ||
* Override this route to perform authorization check | ||
* on current user & domain they are accessing. | ||
* | ||
* Example: | ||
* | ||
* router.get('/api/domains/:domain/authorization', () => { | ||
* const { domain } = ctx.params; | ||
* | ||
* ctx.body = { | ||
* // use whatever system authorization checks needed here. | ||
* authorization: db.isUserAuthorizedForDomain(domain), | ||
* }; | ||
* }) | ||
*/ | ||
router.get('/api/domains/:domain/authorization', domainAuthorizationHandler); | ||
|
||
router.get( | ||
'/api/domains/:domain/workflows/open', | ||
listWorkflows.bind(null, 'open') | ||
); | ||
|
||
router.get( | ||
'/api/domains/:domain/workflows/closed', | ||
listWorkflows.bind(null, 'closed') | ||
); | ||
|
||
router.get( | ||
'/api/domains/:domain/workflows/archived', | ||
workflowArchivedListHandler | ||
); | ||
|
||
router.get('/api/domains/:domain/workflows/list', workflowListHandler); | ||
|
||
router.get( | ||
'/api/domains/:domain/workflows/:workflowId/:runId/history', | ||
workflowHistoryHandler | ||
); | ||
|
||
router.get( | ||
'/api/domains/:domain/workflows/:workflowId/:runId/export', | ||
workflowExportHandler | ||
); | ||
|
||
router.get( | ||
'/api/domains/:domain/workflows/:workflowId/:runId/query', | ||
workflowQueryHandler | ||
); | ||
|
||
router.post( | ||
'/api/domains/:domain/workflows/:workflowId/:runId/query/:queryType', | ||
workflowQueryTypeHandler | ||
); | ||
|
||
router.post( | ||
'/api/domains/:domain/workflows/:workflowId/:runId/terminate', | ||
workflowTerminateHandler | ||
); | ||
|
||
router.post( | ||
'/api/domains/:domain/workflows/:workflowId/:runId/signal/:signal', | ||
workflowSignalHandler | ||
); | ||
|
||
router.get( | ||
'/api/domains/:domain/workflows/:workflowId/:runId', | ||
workflowHandler | ||
); | ||
|
||
router.get( | ||
'/api/domains/:domain/task-lists/:taskList/pollers', | ||
tasklistPollerListHandler | ||
); | ||
|
||
router.get( | ||
'/api/domains/:domain/task-lists/:taskList/partitions', | ||
tasklistPartitionListHandler | ||
); | ||
|
||
router.get('/api/feature-flags/:key', featureFlagHandler); | ||
|
||
router.get('/api/domains/:domain/task-lists/:taskListName', tasklistHandler); | ||
|
||
router.get('/health', healthHandler); | ||
|
||
module.exports = router; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) 2021 Uber Technologies Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved |
||
// | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
const domainAuthorizationHandler = async (ctx, next) => { | ||
ctx.body = { | ||
authorization: true, | ||
}; | ||
|
||
next(); | ||
}; | ||
|
||
module.exports = domainAuthorizationHandler; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) 2021 Uber Technologies Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved |
||
// | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
const domainHandler = async ctx => { | ||
ctx.body = await ctx.cadence.describeDomain({ name: ctx.params.domain }); | ||
}; | ||
|
||
module.exports = domainHandler; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.