Skip to content

Dev - Move Hawtio backend logics in app/webpack.config.cjs devServer to @hawtio/backend-middleware #899

Open
@tadayosi

Description

@tadayosi

Now that we have the backend-middleware in hawtio-next #891, we can refactor it more easily. The hawtio backend mocking code tends to be bloated and would be nice to be moved to the backend-middleware package, so that the other downstream hawtio applications don't need to replicate the same mocking logic for their development.

setupMiddlewares: (middlewares, devServer) => {
devServer.app.use(bodyParser.json())
// Redirect / or /hawtio to /hawtio/
if (publicPath && publicPath !== '/') {
devServer.app.get('/', (_, res) => res.redirect(`${publicPath}/`))
devServer.app.get(`${publicPath}$`, (_, res) => res.redirect(`${publicPath}/`))
}
const username = 'developer'
const proxyEnabled = true
// TODO: Currently self-hosting remotes don't work despite no errors thrown
const plugin = [
{
url: 'http://localhost:3000',
scope: 'app',
module: './remote',
pluginEntry: 'registerRemote',
},
]
// Keycloak
const keycloakEnabled = false
const keycloakClientConfig = {
realm: 'hawtio-demo',
clientId: 'hawtio-client',
url: 'http://localhost:18080/',
jaas: false,
pkceMethod: 'S256',
}
// Hawtio backend API mock
let login = true
devServer.app.get(`${publicPath}/user`, (_, res) => {
login ? res.send(`"${username}"`) : res.sendStatus(403)
})
devServer.app.post(`${publicPath}/auth/login`, (req, res) => {
// Test authentication throttling with username 'throttled'
const { username } = req.body
if (username === 'throttled') {
res.append('Retry-After', 10) // 10 secs
res.sendStatus(429)
return
}
login = true
res.send(String(login))
})
devServer.app.get(`${publicPath}/auth/logout`, (_, res) => {
login = false
res.redirect(`${publicPath}/login`)
})
const oidcEnabled = false
const oidcConfig = {
method: 'oidc',
provider: 'https://login.microsoftonline.com/11111111-2222-3333-4444-555555555555/v2.0',
client_id: '66666666-7777-8888-9999-000000000000',
response_mode: 'fragment',
scope: 'openid email profile api://hawtio-server/Jolokia.Access',
redirect_uri: 'http://localhost:3000/hawtio/',
code_challenge_method: 'S256',
prompt: 'login',
}
devServer.app.get(`${publicPath}/auth/config`, (_, res) => {
res.type('application/json')
if (oidcEnabled) {
res.send(JSON.stringify(oidcConfig))
} else {
res.send('{}')
}
})
devServer.app.get(`${publicPath}/auth/config/session-timeout`, (_, res) => {
res.type('application/json')
res.send('{}')
})
devServer.app.get(`${publicPath}/proxy/enabled`, (_, res) => res.send(String(proxyEnabled)))
devServer.app.get(`${publicPath}/plugin`, (_, res) => res.send(JSON.stringify(plugin)))
devServer.app.get(`${publicPath}/keycloak/enabled`, (_, res) => res.send(String(keycloakEnabled)))
devServer.app.get(`${publicPath}/keycloak/client-config`, (_, res) =>
res.send(JSON.stringify(keycloakClientConfig)),
)
devServer.app.get(`${publicPath}/keycloak/validate-subject-matches`, (_, res) => res.send('true'))
// Hawtio backend middleware should be run before other middlewares (thus 'unshift')
// in order to handle GET requests to the proxied Jolokia endpoint.
middlewares.unshift({
name: 'hawtio-backend',
path: `${publicPath}/proxy`,
middleware: hawtioBackend({
// Uncomment it if you want to see debug log for Hawtio backend
logLevel: 'debug',
}),
})
return middlewares
},

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status

      Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions