gradido/backend/src/server/createServer.ts
2022-05-04 19:29:14 +02:00

80 lines
2.0 KiB
TypeScript

import 'reflect-metadata'
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'
// DHT
import { startDHT } from '@/federation/index'
// 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')
}
// start DHT hyperswarm when DHT_TOPIC is set in .env
if (CONFIG.DHT_TOPIC) {
console.log('Starting hyperswarm DHT')
await startDHT(con, CONFIG.DHT_TOPIC)
}
// 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