This commit is contained in:
+13
-35
@@ -1,16 +1,16 @@
|
||||
import express from 'express'
|
||||
import cors from 'cors'
|
||||
import { ApolloServer } from '@apollo/server'
|
||||
import { expressMiddleware } from '@apollo/server/express4'
|
||||
import { expressMiddleware } from '@as-integrations/express5'
|
||||
import * as Sentry from '@sentry/node'
|
||||
import { publicTypeDefs, publicResolvers } from './schemas/public.js'
|
||||
import { userTypeDefs, userResolvers } from './schemas/user.js'
|
||||
import { teamTypeDefs, teamResolvers } from './schemas/team.js'
|
||||
import { m2mTypeDefs, m2mResolvers } from './schemas/m2m.js'
|
||||
import { publicContext, userContext, teamContext, m2mContext, type AuthContext } from './auth.js'
|
||||
import { resolvers, typeDefs } from './schema.js'
|
||||
|
||||
const PORT = parseInt(process.env.PORT || '8000', 10)
|
||||
const PORT = Number.parseInt(process.env.PORT || '8000', 10)
|
||||
const SENTRY_DSN = process.env.SENTRY_DSN || ''
|
||||
const corsOrigins = (process.env.CORS_ORIGINS || 'https://optovia.ru,https://app.optovia.ru')
|
||||
.split(',')
|
||||
.map(origin => origin.trim())
|
||||
.filter(origin => origin.length > 0)
|
||||
|
||||
if (SENTRY_DSN) {
|
||||
Sentry.init({
|
||||
@@ -22,37 +22,15 @@ if (SENTRY_DSN) {
|
||||
}
|
||||
|
||||
const app = express()
|
||||
app.use(cors({ origin: ['https://optovia.ru'], credentials: true }))
|
||||
app.use(cors({ origin: corsOrigins, credentials: true }))
|
||||
|
||||
const publicServer = new ApolloServer<AuthContext>({ typeDefs: publicTypeDefs, resolvers: publicResolvers, introspection: true })
|
||||
const userServer = new ApolloServer<AuthContext>({ typeDefs: userTypeDefs, resolvers: userResolvers, introspection: true })
|
||||
const teamServer = new ApolloServer<AuthContext>({ typeDefs: teamTypeDefs, resolvers: teamResolvers, introspection: true })
|
||||
const m2mServer = new ApolloServer<AuthContext>({ typeDefs: m2mTypeDefs, resolvers: m2mResolvers, introspection: true })
|
||||
const server = new ApolloServer({ typeDefs, resolvers, introspection: true })
|
||||
await server.start()
|
||||
|
||||
await Promise.all([publicServer.start(), userServer.start(), teamServer.start(), m2mServer.start()])
|
||||
|
||||
app.use('/graphql/public', express.json(), expressMiddleware(publicServer, {
|
||||
context: async () => publicContext(),
|
||||
}) as unknown as express.RequestHandler)
|
||||
|
||||
app.use('/graphql/user', express.json(), expressMiddleware(userServer, {
|
||||
context: async ({ req }) => userContext(req as unknown as import('express').Request),
|
||||
}) as unknown as express.RequestHandler)
|
||||
|
||||
app.use('/graphql/team', express.json(), expressMiddleware(teamServer, {
|
||||
context: async ({ req }) => teamContext(req as unknown as import('express').Request),
|
||||
}) as unknown as express.RequestHandler)
|
||||
|
||||
app.use('/graphql/m2m', express.json(), expressMiddleware(m2mServer, {
|
||||
context: async () => m2mContext(),
|
||||
}) as unknown as express.RequestHandler)
|
||||
|
||||
app.get('/health', (_, res) => { res.json({ status: 'ok' }) })
|
||||
app.use('/graphql', express.json(), expressMiddleware(server))
|
||||
app.get('/health', (_, res) => { res.json({ status: 'ok', service: 'exchange' }) })
|
||||
|
||||
app.listen(PORT, '0.0.0.0', () => {
|
||||
console.log(`Exchange server ready on port ${PORT}`)
|
||||
console.log(` /graphql/public - public`)
|
||||
console.log(` /graphql/user - id token auth`)
|
||||
console.log(` /graphql/team - team access token auth`)
|
||||
console.log(` /graphql/m2m - internal services (no auth)`)
|
||||
console.log(' /graphql - suppliers, products, quotes')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user