Skip to content

Commit

Permalink
feat(config): added a way to configure an external url
Browse files Browse the repository at this point in the history
  • Loading branch information
allardy committed Nov 20, 2018
1 parent 748bedf commit be33fa8
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/bp/core/config/botpress.config.ts
Expand Up @@ -41,6 +41,13 @@ export type BotpressConfig = {
enabled?: boolean
origin?: string
}
/**
* Represents the complete base URL exposed externally by your bot. This is useful if you configure the bot
* locally and use NGINX as a reverse proxy to handle HTTPS. It should include the protocol and no trailing slash.
* If unset, it will be constructed from the real host/port
* @example https://botpress.io
*/
externalUrl?: string
}
database: DatabaseConfig
ghost: {
Expand Down
4 changes: 3 additions & 1 deletion src/bp/core/server.ts
Expand Up @@ -149,6 +149,8 @@ export default class HTTPServer {

process.HOST = config.host
process.PORT = await portFinder.getPortPromise({ port: config.port })
process.EXTERNAL_URL = process.env.EXTERNAL_URL || config.externalUrl || `http://${process.HOST}:${process.PORT}`

if (process.PORT !== config.port) {
this.logger.warn(`Configured port ${config.port} is already in use. Using next port available: ${process.PORT}`)
}
Expand Down Expand Up @@ -198,7 +200,7 @@ export default class HTTPServer {

async getAxiosConfigForBot(botId: string): Promise<AxiosBotConfig> {
return {
baseURL: `http://${process.HOST || 'localhost'}:${process.PORT}/api/v1/bots/${botId}`
baseURL: `${process.EXTERNAL_URL}/api/v1/bots/${botId}`
}
}
}
2 changes: 1 addition & 1 deletion src/bp/core/services/action/action-service.ts
Expand Up @@ -130,7 +130,7 @@ export class ScopedActionService {
user: incomingEvent.state.user,
state: incomingEvent.state.context.data || {},
args: actionArgs,
process: _.pick(process, 'HOST', 'PORT', 'env')
process: _.pick(process, 'HOST', 'PORT', 'EXTERNAL_URL', 'env')
},
require: {
external: true,
Expand Down
2 changes: 1 addition & 1 deletion src/bp/core/services/cms/cms-service.ts
Expand Up @@ -414,7 +414,7 @@ export class CMSService implements IDisposeOnExit {

async renderElement(contentTypeId, payload, channel) {
const contentType = await this.getContentType(contentTypeId)
const additionnalData = { BOT_URL: 'http://localhost:3000' }
const additionnalData = { BOT_URL: process.EXTERNAL_URL }

return await contentType.renderElement({ ...additionnalData, ...payload }, channel)
}
Expand Down
2 changes: 1 addition & 1 deletion src/bp/core/services/hook/hook-service.ts
Expand Up @@ -173,7 +173,7 @@ export class HookService {
console: 'inherit',
sandbox: {
...hookScript.hook.args,
process: _.pick(process, 'HOST', 'PORT', 'env')
process: _.pick(process, 'HOST', 'PORT', 'EXTERNAL_URL', 'env')
},
timeout: hookScript.hook.timeout,
require: {
Expand Down
Expand Up @@ -13,5 +13,5 @@ const params = {
options: JSON.stringify(chatOptions)
}

// Bot will be available at http://$HOST:$PORT/s/$BOT_NAME
bp.http.createShortLink(botId, `http://${process.HOST}:${process.PORT}/lite/${botId}/`, params)
// Bot will be available at $EXTERNAL_URL/s/$BOT_NAME
bp.http.createShortLink(botId, `${process.EXTERNAL_URL}/lite/${botId}/`, params)
1 change: 1 addition & 0 deletions src/typings/global.d.ts
Expand Up @@ -16,6 +16,7 @@ declare namespace NodeJS {
JWT_SECRET: string
HOST: string
PORT: number
EXTERNAL_URL: string
PROJECT_LOCATION: string
LOADED_MODULES: { [module: string]: string }
pkg: any
Expand Down

0 comments on commit be33fa8

Please sign in to comment.