Skip to content
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

Merged
merged 8 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
422 changes: 0 additions & 422 deletions server/router.js

This file was deleted.

37 changes: 37 additions & 0 deletions server/router/helpers/build-query-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2021 Uber Technologies Inc.
Copy link
Contributor Author

@just-at-uber just-at-uber Apr 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helpers in server/router.js to server/router/handlers

//
//
// 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;
34 changes: 34 additions & 0 deletions server/router/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2021 Uber Technologies Inc.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved server/helpers into server/router/handlers

//
//
// 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,
};
46 changes: 46 additions & 0 deletions server/router/helpers/list-workflows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2021 Uber Technologies Inc.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helpers in server/router.js toserver/router/helpers

//
//
// 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;
45 changes: 45 additions & 0 deletions server/router/helpers/map-history-response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2021 Uber Technologies Inc.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helpers in server/router.js toserver/router/helpers

//
//
// 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;
33 changes: 33 additions & 0 deletions server/router/helpers/replacer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2021 Uber Technologies Inc.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helpers in server/router.js toserver/router/helpers

//
//
// 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;
136 changes: 136 additions & 0 deletions server/router/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright (c) 2017-2021 Uber Technologies Inc.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file moved from server/router.js to server/router/index.js

// 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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved server/router.js route handlers into separate files in server/router/routes


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;
30 changes: 30 additions & 0 deletions server/router/routes/domain-authorization-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2021 Uber Technologies Inc.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved server/router.js route handlers into separate files in server/router/routes

//
//
// 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;
26 changes: 26 additions & 0 deletions server/router/routes/domain-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2021 Uber Technologies Inc.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved server/router.js route handlers into separate files in server/router/routes

//
//
// 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;
Loading