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

Add option to disable prefixing of routes with stage name #926

Merged
merged 6 commits into from Mar 24, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -102,6 +102,7 @@ All CLI options are optional:
--httpPort Http port to listen on. Default: 3000
--httpsProtocol -H To enable HTTPS, specify directory (relative to your cwd, typically your project dir) for both cert.pem and key.pem files
--lambdaPort Lambda http port to listen on. Default: 3002
--noPrependStageInUrl Don't prepend http routes with the stage.
--noAuth Turns off all authorizers
--noTimeout -t Disables the timeout feature.
--printOutput Turns on logging of your lambda outputs in the terminal.
Expand Down
3 changes: 3 additions & 0 deletions src/config/commandOptions.js
Expand Up @@ -43,6 +43,9 @@ export default {
lambdaPort: {
usage: 'Lambda http port to listen on. Default: 3002',
},
noPrependStageInUrl: {
usage: "Don't prepend http routes with the stage.",
},
noAuth: {
usage: 'Turns off all authorizers',
},
Expand Down
1 change: 1 addition & 0 deletions src/config/defaultOptions.js
Expand Up @@ -13,6 +13,7 @@ export default {
httpPort: 3000,
httpsProtocol: '',
lambdaPort: 3002,
noPrependStageInUrl: false,
noAuth: false,
noTimeout: false,
printOutput: false,
Expand Down
11 changes: 5 additions & 6 deletions src/events/http/HttpServer.js
Expand Up @@ -250,13 +250,13 @@ export default class HttpServer {
// path must start with '/'
let hapiPath = path.startsWith('/') ? path : `/${path}`

const _path = hapiPath

// prepend stage to path
const stage = this.#options.stage || this.#serverless.service.provider.stage

// prepend stage to path
hapiPath = `/${stage}${hapiPath}`
if (!this.#options.noPrependStageInUrl) {
// prepend stage to path
hapiPath = `/${stage}${hapiPath}`
}

// but must not end with '/'
if (hapiPath !== '/' && hapiPath.endsWith('/')) {
Expand All @@ -276,9 +276,8 @@ export default class HttpServer {

this.#terminalInfo.push({
method,
path: _path,
path: hapiPath,
server,
stage,
})

// If the endpoint has an authorization function, create an authStrategy for the route
Expand Down
8 changes: 4 additions & 4 deletions src/serverlessLog.js
Expand Up @@ -36,12 +36,12 @@ export function setLog(serverlessLogRef) {
// logs based on:
// https://github.com/serverless/serverless/blob/master/lib/classes/CLI.js

function logRoute(method, server, stage, path, maxLength) {
function logRoute(method, server, path, maxLength) {
const methodColor = colorMethodMapping.get(method) ?? peachpuff
const methodFormatted = method.padEnd(maxLength, ' ')

return `${methodColor(methodFormatted)} ${yellow.dim('|')} ${grey.dim(
`${server}/${stage}`,
Copy link
Contributor

Choose a reason for hiding this comment

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

I would say to maybe leave this in, and just not print it if the option isn't specified? That way people who are cool with the stage change still see the way it was formatted before.

Copy link

Choose a reason for hiding this comment

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

Ran into problems after updating serverless-offline to the current version. Basically offline adds the stage name 'development' or 'production' to the Lambda URLs when previous versions did not. Used to be http://0.0.0.0:3001/query, now it's http://0.0.0.0:3001/development/query. Seems like this PR is almost ready to go. Any chance it could be merged in the next few days?

server,
)}${lime(path)}`
}

Expand All @@ -61,8 +61,8 @@ export function logRoutes(routeInfo) {
console.log(
boxen(
routeInfo
.map(({ method, path, server, stage }) =>
logRoute(method, server, stage, path, maxLength),
.map(({ method, path, server }) =>
logRoute(method, server, path, maxLength),
)
.join('\n'),
boxenOptions,
Expand Down
29 changes: 29 additions & 0 deletions tests/integration/uncategorized/uncategorized.test.js
Expand Up @@ -35,3 +35,32 @@ describe('uncategorized tests', () => {
expect(json).toEqual({ foo: 'bar' })
})
})

describe('noPrependStageInUrl tests', () => {
// init
beforeAll(() =>
setup({
servicePath: resolve(__dirname),
args: ['--noPrependStageInUrl'],
}),
)

// cleanup
afterAll(() => teardown())

test('noPrependStageInUrl 1', async () => {
const url = joinUrl(TEST_BASE_URL, '/uncategorized-1')
const response = await fetch(url)
const json = await response.json()

expect(json).toEqual({ foo: 'bar' })
})

test('noPrependStageInUrl 2', async () => {
const url = joinUrl(TEST_BASE_URL, '/dev/uncategorized-1')
const response = await fetch(url)
const json = await response.json()

expect(json.statusCode).toEqual(404)
})
})