Skip to content

Commit

Permalink
fix: 🐛 add helth endpoint
Browse files Browse the repository at this point in the history
for health checks

Signed-off-by: Manuel Ruck <[email protected]>
  • Loading branch information
Manuel Ruck committed Nov 1, 2023
1 parent c5cc3d5 commit 9fbbfe1
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 123 deletions.
2 changes: 2 additions & 0 deletions bundestag.io/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"@graphql-tools/schema": "^10.0.0",
"@graphql-tools/utils": "^10.0.8",
"axios": "1.5.1",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"express": "4.18.2",
"graphql": "16.8.1",
"graphql-date": "1.0.3",
Expand Down
78 changes: 59 additions & 19 deletions bundestag.io/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { server } from './services/graphql';
import { startStandaloneServer } from '@apollo/server/standalone';
import { createServer } from './services/graphql';
import {
ProcedureModel,
UserModel,
Expand All @@ -9,6 +8,11 @@ import {
PlenaryMinuteModel,
mongoConnect,
} from '@democracy-deutschland/bundestagio-common';
import express from 'express';
import http from 'http';
import cors from 'cors';
import { expressMiddleware } from '@apollo/server/express4';
import bodyParser from 'body-parser';

// *****************************************************************
// IMPORTANT - you cannot include any models before migrating the DB
Expand All @@ -19,29 +23,65 @@ import CONFIG from './config';
// Allow global Log
import './services/logger';

const app = express();
const httpServer = http.createServer(app);
const server = createServer({ httpServer });

const main = async () => {
// Connect to DB - this keeps the process running
// IMPORTANT - This is done before any Model is registered
await mongoConnect();

startStandaloneServer(server, {
listen: {
port: CONFIG.PORT,
path: CONFIG.GRAPHQL_PATH,
},
context: async ({ req }) => {
return {
req,
// Models
ProcedureModel,
UserModel,
DeputyModel,
NamedPollModel,
ConferenceWeekDetailModel,
PlenaryMinuteModel,
};
},
await server.start();

app.get('/health', (req, res) => {
res.status(200).send('Okay!');
});

app.use(
CONFIG.GRAPHQL_PATH,
cors<cors.CorsRequest>(),
// 50mb is the limit that `startStandaloneServer` uses, but you may configure this to suit your needs
bodyParser.json({ limit: '50mb' }),
// expressMiddleware accepts the same arguments:
// an Apollo Server instance and optional configuration options
expressMiddleware(server, {
// context: async ({ req }) => ({ token: req.headers.token }),
context: async ({ req }) => {
return {
req,
// Models
ProcedureModel,
UserModel,
DeputyModel,
NamedPollModel,
ConferenceWeekDetailModel,
PlenaryMinuteModel,
};
},
}),
);
await new Promise<void>((resolve) => httpServer.listen({ port: CONFIG.PORT }, resolve));
console.log(`🚀 Server ready at http://localhost:${CONFIG.PORT}/`);

// startStandaloneServer(server, {
// listen: {
// port: CONFIG.PORT,
// path: CONFIG.GRAPHQL_PATH,
// },
// context: async ({ req }) => {
// return {
// req,
// // Models
// ProcedureModel,
// UserModel,
// DeputyModel,
// NamedPollModel,
// ConferenceWeekDetailModel,
// PlenaryMinuteModel,
// };
// },
// });
};

// Async Wrapping Function
Expand Down
13 changes: 8 additions & 5 deletions bundestag.io/api/src/services/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { makeExecutableSchema } from '@graphql-tools/schema';
import { typeDefs as typeDefsBase } from '../../generated/graphql';
import resolvers from '../../graphql/resolvers';
import { mergeTypeDefs } from '@graphql-tools/merge';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';

const typeDefsAuth = authDirective('auth').authDirectiveTypeDefs;

Expand All @@ -15,8 +16,10 @@ const schema = makeExecutableSchema({
resolvers,
});

export const server = new ApolloServer<GraphQlContext>({
schema: authDirective('auth').authDirectiveTransformer(schema),
// schema,
introspection: true,
});
export const createServer = ({ httpServer }) =>
new ApolloServer<GraphQlContext>({
schema: authDirective('auth').authDirectiveTransformer(schema),
// schema,
introspection: true,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
Loading

0 comments on commit 9fbbfe1

Please sign in to comment.