Skip to content
This repository has been archived by the owner on May 31, 2019. It is now read-only.

Commit

Permalink
Move to use provided config
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki committed Aug 21, 2018
1 parent 1347732 commit 67396ec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
6 changes: 3 additions & 3 deletions server/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ if (process.env.PLAYGROUND_VARIABLES_FILE) {
}
}

const topLevelGraphqlPath = '/graphql'
const graphqlEndpoint = '/graphql'
const port = process.env.HTTP_PORT || '8000'

const config = {
server: {
apiPath: topLevelGraphqlPath,
port
},
graphQLConfig: {
graphqlEndpoint,
tracing: true
},
playgroundConfig: {
endpoint: topLevelGraphqlPath, // if you want GraphiQL enabled
endpoint: graphqlEndpoint, // if you want GraphiQL enabled
query: playgroundQueryFileContent,
variables: playgroundVariableFileContent,
subscriptionEndpoint: process.env.PLAYGROUND_SUBS_ENDPOINT || `ws://${hostname()}:${port}/subscriptions`
Expand Down
16 changes: 8 additions & 8 deletions server/security/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ const { log } = require('../lib/util/logger')
const Keycloak = require('keycloak-connect')
var session = require('express-session')

const config = require('../config')

/**
* Create keycloak middleware if needed.
*
* @param {*} expressRouter
* @param {*} keycloakConfig keycloak server specific configuration
* @param {*} expressRouter express router that should be used to attach auth
* @param {string} apiPath location of the protected api
*/
exports.applyAuthMiddleware = (expressRouter, apiPath) => {
if (config.keycloakConfig) {
exports.applyAuthMiddleware = (keycloakConfig, expressRouter, apiPath) => {
if (keycloakConfig) {
log.info('Initializing Keycloak authentication')
const memoryStore = new session.MemoryStore()
expressRouter.use(session({
secret: config.secret || 'secret',
secret: keycloakConfig.secret || 'secret',
resave: false,
saveUninitialized: true,
store: memoryStore
}))

var keycloak = new Keycloak({
store: memoryStore
}, config.keycloakConfig)
}, keycloakConfig)

// Install general keycloak middleware
expressRouter.use(keycloak.middleware())

// Protect the main route for all graphql services
// (disable unauthenticated access)
// Disable unauthenticated access
expressRouter.use(apiPath, keycloak.protect())
} else {
log.info('Keycloak authentication is not configured')
Expand Down
21 changes: 12 additions & 9 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ const {getMetrics, responseLoggingMetric} = require('./metrics')
const {applyAuthMiddleware} = require('./security')
const schemaParser = require('./lib/schemaParser')
const schemaListenerCreator = require('./lib/schemaListeners/schemaListenerCreator')
const { server } = require('./config')

function newExpressApp () {
function newExpressApp (keycloakConfig, graphqlEndpoint) {
let app = express()

app.use(responseLoggingMetric)

app.use('*', cors())
app.use(expressPino)
applyAuthMiddleware(app, server.apiPath)
applyAuthMiddleware(keycloakConfig, app, graphqlEndpoint)
return app
}

function newApolloServer (app, schema, httpServer, tracing, playgroundConfig) {
function newApolloServer (app, schema, httpServer, tracing, playgroundConfig, graphqlEndpoint) {
let apolloServer = new ApolloServer({

schema,
Expand All @@ -43,21 +42,25 @@ function newApolloServer (app, schema, httpServer, tracing, playgroundConfig) {
]
}
})
apolloServer.applyMiddleware({ app, disableHealthCheck: true, path: server.apiPath })
apolloServer.applyMiddleware({ app, disableHealthCheck: true, path: graphqlEndpoint })
apolloServer.installSubscriptionHandlers(httpServer)

return apolloServer
}

module.exports = async ({graphQLConfig, playgroundConfig, schemaListenerConfig}, models, pubsub) => {
module.exports = async ({graphQLConfig,
playgroundConfig,
schemaListenerConfig,
keycloakConfig}, models, pubsub) => {
const { tracing } = graphQLConfig
let { schema, dataSources } = await buildSchema(models, pubsub)
await connectDataSources(dataSources)

let server = http.createServer()

let app = newExpressApp()
let apolloServer = newApolloServer(app, schema, server, tracing, playgroundConfig)
let graphqlEndpoint = graphQLConfig.graphqlEndpoint
let app = newExpressApp(keycloakConfig, graphqlEndpoint)
let apolloServer = newApolloServer(app, schema, server, tracing, playgroundConfig, graphqlEndpoint)
server.on('request', app)

app.get('/healthz', async (req, res) => {
Expand Down Expand Up @@ -97,7 +100,7 @@ module.exports = async ({graphQLConfig, playgroundConfig, schemaListenerConfig},
server.removeListener('request', app)
// reinitialize the server objects
schema = newSchema.schema
app = newExpressApp()
app = newExpressApp(keycloakConfig, graphqlEndpoint)
apolloServer = newApolloServer(app, schema, server, tracing, playgroundConfig)
server.on('request', app)

Expand Down

0 comments on commit 67396ec

Please sign in to comment.