gradido/backend/src/server/createServer.ts

72 lines
1.8 KiB
TypeScript

import 'reflect-metadata'
import 'module-alias/register'
import { ApolloServer } from 'apollo-server-express'
import express, { Express } from 'express'
// database
import connection from '../typeorm/connection'
import { checkDBVersion } from '../typeorm/DBVersion'
// server
import cors from './cors'
import serverContext from './context'
import plugins from './plugins'
// config
import CONFIG from '../config'
// graphql
import schema from '../graphql/schema'
// webhooks
import { elopageWebhook } from '../webhook/elopage'
import { Connection } from '@dbTools/typeorm'
// TODO implement
// import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity";
type ServerDef = { apollo: ApolloServer; app: Express; con: Connection }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const createServer = async (context: any = serverContext): Promise<ServerDef> => {
// open mysql connection
const con = await connection()
if (!con || !con.isConnected) {
throw new Error(`Fatal: Couldn't open connection to database`)
}
// check for correct database version
const dbVersion = await checkDBVersion(CONFIG.DB_VERSION)
if (!dbVersion) {
throw new Error('Fatal: Database Version incorrect')
}
// Express Server
const app = express()
// cors
app.use(cors)
// bodyparser json
app.use(express.json())
// bodyparser urlencoded for elopage
app.use(express.urlencoded({ extended: true }))
// Elopage Webhook
app.post('/hook/elopage/' + CONFIG.WEBHOOK_ELOPAGE_SECRET, elopageWebhook)
// Apollo Server
const apollo = new ApolloServer({
schema: await schema(),
playground: CONFIG.GRAPHIQL,
introspection: CONFIG.GRAPHIQL,
context,
plugins,
})
apollo.applyMiddleware({ app, path: '/' })
return { apollo, app, con }
}
export default createServer