Skip to content

Commit

Permalink
Merge pull request #1086 from apollographql/server-2.0/healthcheck-in…
Browse files Browse the repository at this point in the history
…-variants

Move health check into the server variants
  • Loading branch information
evans authored May 23, 2018
2 parents 69be8c8 + 06a4bcb commit 45d3614
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 26 deletions.
24 changes: 24 additions & 0 deletions packages/apollo-server-express/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface ServerRegistration {
path?: string;
cors?: corsMiddleware.CorsOptions;
bodyParserConfig?: OptionsJson;
onHealthCheck?: (req: express.Request) => Promise<any>;
disableHealthCheck?: boolean;
}

export const registerServer = async ({
Expand All @@ -22,9 +24,31 @@ export const registerServer = async ({
path,
cors,
bodyParserConfig,
disableHealthCheck,
onHealthCheck,
}: ServerRegistration) => {
if (!path) path = '/graphql';

if (!disableHealthCheck) {
//uses same path as engine
app.use('/.well-known/apollo/server-health', (req, res, next) => {
//Response follows https://tools.ietf.org/html/draft-inadarei-api-health-check-01
res.type('application/health+json');

if (onHealthCheck) {
onHealthCheck(req)
.then(() => {
res.json({ status: 'pass' });
})
.catch(() => {
res.status(503).json({ status: 'fail' });
});
} else {
res.json({ status: 'pass' });
}
});
}

// XXX multiple paths?
server.use({
path,
Expand Down
29 changes: 29 additions & 0 deletions packages/apollo-server-hapi/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface ServerRegistration {
server: ApolloServerBase<hapi.Request>;
path?: string;
cors?: boolean;
onHealthCheck?: (req: hapi.Request) => Promise<any>;
disableHealthCheck?: boolean;
}

export interface HapiListenOptions {
Expand All @@ -30,6 +32,8 @@ export const registerServer = async ({
server,
cors,
path,
disableHealthCheck,
onHealthCheck,
}: ServerRegistration) => {
if (!path) path = '/graphql';

Expand Down Expand Up @@ -92,6 +96,31 @@ server.listen({ http: { port: YOUR_PORT_HERE } });
},
});

if (!disableHealthCheck) {
await hapiApp.route({
method: '*',
path: '/.well-known/apollo/server-health',
options: {
cors: typeof cors === 'boolean' ? cors : true,
},
handler: async function(request, h) {
if (onHealthCheck) {
try {
await onHealthCheck(request);
} catch {
const response = h.response({ status: 'fail' });
response.code(503);
response.type('application/health+json');
return response;
}
}
const response = h.response({ status: 'pass' });
response.type('application/health+json');
return response;
},
});
}

await hapiApp.register({
plugin: graphqlHapi,
options: {
Expand Down
48 changes: 22 additions & 26 deletions packages/apollo-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as express from 'express';
import { Application, Request } from 'express';
import { registerServer } from 'apollo-server-express';
import { OptionsJson } from 'body-parser';
import { CorsOptions } from 'cors';

import {
ApolloServerBase,
Expand All @@ -11,47 +13,41 @@ import {

export * from './exports';

export class ApolloServer extends ApolloServerBase<express.Request> {
export class ApolloServer extends ApolloServerBase<Request> {
// here we overwrite the underlying listen to configure
// the fallback / default server implementation
async listen(
opts: ListenOptions & {
onHealthCheck?: (req: express.Request) => Promise<any>;
onHealthCheck?: (req: Request) => Promise<any>;
disableHealthCheck?: boolean;
bodyParserConfig?: OptionsJson;
cors?: CorsOptions;
} = {},
): Promise<ServerInfo> {
//defensive copy
const { onHealthCheck } = opts;
const {
disableHealthCheck,
bodyParserConfig,
onHealthCheck,
cors,
...listenOpts
} = opts;

// we haven't configured a server yet so lets build the default one
// using express
if (!this.getHttp) {
const app = express();

if (!opts.disableHealthCheck) {
//uses same path as engine
app.use('/.well-known/apollo/server-health', (req, res, next) => {
//Response follows https://tools.ietf.org/html/draft-inadarei-api-health-check-01
res.type('application/health+json');

if (onHealthCheck) {
onHealthCheck(req)
.then(() => {
res.json({ status: 'pass' });
})
.catch(() => {
res.status(503).json({ status: 'fail' });
});
} else {
res.json({ status: 'pass' });
}
});
}

await registerServer({ app, path: '/', server: this });
await registerServer({
app,
path: '/',
server: this,
disableHealthCheck,
bodyParserConfig,
onHealthCheck,
cors,
});
}

return super.listen(opts);
return super.listen(listenOpts);
}
}

0 comments on commit 45d3614

Please sign in to comment.