Skip to content

Commit

Permalink
Merge pull request #674 from apuyou/apuyou/functionsPort
Browse files Browse the repository at this point in the history
Fix functions server port assignment
  • Loading branch information
RaeesBhatti authored Jan 8, 2020
2 parents 5e28d0c + df70a43 commit 89f90cd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 29 deletions.
6 changes: 2 additions & 4 deletions src/commands/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,13 @@ class DevCommand extends Command {
functionWatcher.on('change', functionBuilder.build)
functionWatcher.on('unlink', functionBuilder.build)
}
const functionsPort = settings.functionsPort
const functionsPort = await getPort({ port: settings.functionsPort || 34567 })
settings.functionsPort = functionsPort

// returns a value but we dont use it
await serveFunctions({
...settings,
port: functionsPort,
functionsDir
})
settings.functionsPort = functionsPort
}

let { url, port } = await startProxy(settings, addonUrls, site.configPath)
Expand Down
28 changes: 3 additions & 25 deletions src/utils/serve-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const express = require('express')
const bodyParser = require('body-parser')
const expressLogging = require('express-logging')
const queryString = require('querystring')
const getPort = require('get-port')
const chokidar = require('chokidar')
const jwtDecode = require('jwt-decode')
const {
Expand All @@ -12,8 +11,6 @@ const {
} = require('./logo')
const { getFunctions } = require('./get-functions')

const defaultPort = 34567

function handleErr(err, response) {
response.statusCode = 500
response.write(`${NETLIFYDEVERR} Function invocation failed: ` + err.toString())
Expand Down Expand Up @@ -190,9 +187,6 @@ function promiseCallback(promise, callback) {
async function serveFunctions(settings) {
const app = express()
const dir = settings.functionsDir
const port = await getPort({
port: assignLoudly(settings.port, defaultPort)
})

app.use(
bodyParser.text({
Expand All @@ -212,33 +206,17 @@ async function serveFunctions(settings) {
})
app.all('*', createHandler(dir))

app.listen(port, function(err) {
app.listen(settings.functionsPort, function(err) {
if (err) {
console.error(`${NETLIFYDEVERR} Unable to start lambda server: `, err) // eslint-disable-line no-console
process.exit(1)
}

// add newline because this often appears alongside the client devserver's output
console.log(`\n${NETLIFYDEVLOG} Lambda server is listening on ${port}`) // eslint-disable-line no-console
console.log(`\n${NETLIFYDEVLOG} Lambda server is listening on ${settings.functionsPort}`) // eslint-disable-line no-console
})

return Promise.resolve({
port
})
return Promise.resolve()
}

module.exports = { serveFunctions }

// if first arg is undefined, use default, but tell user about it in case it is unintentional
function assignLoudly(
optionalValue,
fallbackValue,
tellUser = dV => console.log(`${NETLIFYDEVLOG} No port specified, using defaultPort of `, dV) // eslint-disable-line no-console
) {
if (fallbackValue === undefined) throw new Error('must have a fallbackValue')
if (fallbackValue !== optionalValue && optionalValue === undefined) {
tellUser(fallbackValue)
return fallbackValue
}
return optionalValue
}

0 comments on commit 89f90cd

Please sign in to comment.