Skip to content

Commit

Permalink
Merge pull request #2029 from botpress/ya-persist-debug
Browse files Browse the repository at this point in the history
fix(debug): added method to persist debug scopes
  • Loading branch information
allardy committed Jun 30, 2019
2 parents 7012e16 + 1e5e57b commit 410b770
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/bp/core/botpress.ts
Expand Up @@ -117,6 +117,7 @@ export class Botpress {
this.config = await this.loadConfiguration()
await this.createDatabase()
await this.initializeGhost()
await this.restoreDebugScope()

// Invalidating the configuration to force it to load it from the ghost if enabled
this.config = await this.loadConfiguration(true)
Expand All @@ -139,6 +140,17 @@ export class Botpress {
await this.hookService.executeHook(new Hooks.AfterServerStart(this.api))
}

async restoreDebugScope() {
if (await this.ghostService.global().fileExists('/', 'debug.json')) {
try {
const { scopes } = await this.ghostService.global().readFileAsObject('/', 'debug.json')
setDebugScopes(scopes.join(','))
} catch (err) {
this.logger.attachError(err).error(`Couldn't load debug scopes. Check the syntax of debug.json`)
}
}
}

async checkJwtSecret() {
// @deprecated > 11: .jwtSecret has been renamed for appSecret. botpress > 11 jwtSecret will not be supported
// @ts-ignore
Expand Down
2 changes: 1 addition & 1 deletion src/bp/core/routers/admin/index.ts
Expand Up @@ -53,7 +53,7 @@ export class AdminRouter extends CustomRouter {
this.licenseRouter = new LicenseRouter(logger, this.licenseService, configProvider)
this.versioningRouter = new VersioningRouter(logger, this.ghostService, this.botService)
this.rolesRouter = new RolesRouter(logger, this.workspaceService)
this.serverRouter = new ServerRouter(logger, monitoringService, alertingService, configProvider)
this.serverRouter = new ServerRouter(logger, monitoringService, alertingService, configProvider, ghostService)
this.languagesRouter = new LanguagesRouter(logger, moduleLoader)
this.loadUser = loadUser(this.authService)

Expand Down
14 changes: 12 additions & 2 deletions src/bp/core/routers/admin/server.ts
@@ -1,6 +1,7 @@
import { Logger } from 'botpress/sdk'
import { spawn } from 'child_process'
import { ConfigProvider } from 'core/config/config-loader'
import { GhostService } from 'core/services'
import { AlertingService } from 'core/services/alerting-service'
import { MonitoringService } from 'core/services/monitoring'
import { Router } from 'express'
Expand All @@ -14,7 +15,8 @@ export class ServerRouter extends CustomRouter {
private logger: Logger,
private monitoringService: MonitoringService,
private alertingService: AlertingService,
private configProvider: ConfigProvider
private configProvider: ConfigProvider,
private ghostService: GhostService
) {
super('Server', logger, Router({ mergeParams: true }))
this.setupRoutes()
Expand Down Expand Up @@ -106,7 +108,15 @@ export class ServerRouter extends CustomRouter {
router.post(
'/debug',
this.asyncMiddleware(async (req, res) => {
setDebugScopes(req.body.debugScope)
const { debugScope, persist } = req.body

if (persist) {
await this.ghostService
.global()
.upsertFile('/', 'debug.json', JSON.stringify({ scopes: debugScope.split(',') }))
}

setDebugScopes(debugScope)
res.sendStatus(200)
})
)
Expand Down
15 changes: 12 additions & 3 deletions src/bp/ui-admin/src/Pages/Server/Debug.tsx
@@ -1,4 +1,4 @@
import { Button, Intent } from '@blueprintjs/core'
import { Button, Checkbox, Intent, Tooltip } from '@blueprintjs/core'
import React from 'react'
import CheckboxTree from 'react-checkbox-tree'
import 'react-checkbox-tree/lib/react-checkbox-tree.css'
Expand All @@ -22,7 +22,8 @@ export default class Debug extends React.Component<Props, State> {
state = {
nodes: undefined,
checked: [],
expanded: ['bp']
expanded: ['bp'],
persist: false
}

async componentDidMount() {
Expand Down Expand Up @@ -54,11 +55,13 @@ export default class Debug extends React.Component<Props, State> {

saveConfiguration = async () => {
const debugScope = this.state.checked && this.state.checked.join(',')
await api.getSecured().post(`/admin/server/debug`, { debugScope })
await api.getSecured().post(`/admin/server/debug`, { debugScope, persist: this.state.persist })

AppToaster.show({ message: 'Debug configuration updated successfully!', intent: Intent.SUCCESS, timeout: 2000 })
}

handlePersistChanged = (e: any) => this.setState({ persist: e.target.checked })

renderTree() {
if (!this.state.nodes) {
return null
Expand Down Expand Up @@ -93,6 +96,11 @@ export default class Debug extends React.Component<Props, State> {
<div>
<Button onClick={this.loadConfiguration} fill={true} icon="refresh" text="Refresh" />
<br />
<br />
<Tooltip content="When checked, the selected debug options will be enabled after each server restart">
<Checkbox checked={this.state.persist} onChange={this.handlePersistChanged} label="Persist" />
</Tooltip>

<Button onClick={this.saveConfiguration} intent={Intent.PRIMARY} fill={true} icon="floppy-disk" text="Save" />
</div>
)
Expand All @@ -117,4 +125,5 @@ interface State {
nodes: any
checked: any
expanded: any
persist: boolean
}

0 comments on commit 410b770

Please sign in to comment.