Skip to content

Commit

Permalink
Merge pull request #926 from thomaschaaf/disable-stage-prefix-in-routes
Browse files Browse the repository at this point in the history
Add option to disable prefixing of routes with stage name
  • Loading branch information
dherault committed Mar 24, 2020
2 parents c01202b + 8daf158 commit aefe296
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 10 deletions.
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}`,
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)
})
})

0 comments on commit aefe296

Please sign in to comment.