Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move health check into the server variants #1086

Merged
merged 2 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}