mirror of
https://github.com/wassname/talk.git
synced 2026-08-20 12:50:41 +08:00
added tenant + graph support
This commit is contained in:
@@ -1,16 +1,59 @@
|
||||
import express, { Express } from 'express';
|
||||
import express, { Express, Router } from 'express';
|
||||
import { Db } from 'mongodb';
|
||||
|
||||
import { Config } from 'talk-server/config';
|
||||
import schema from 'talk-server/graph/tenant/schema';
|
||||
import { create } from 'talk-server/services/mongodb';
|
||||
|
||||
import serveStatic from './middleware/serveStatic';
|
||||
import graphql from './middleware/graphql';
|
||||
import graphiql from './middleware/graphiql';
|
||||
|
||||
import playground from './middleware/playground';
|
||||
import {
|
||||
access as accessLogger,
|
||||
error as errorLogger,
|
||||
} from './middleware/logging';
|
||||
import tenantGraphMiddleware from 'talk-server/graph/tenant/middleware';
|
||||
|
||||
import schema from 'talk-server/graph/schema';
|
||||
import { create } from 'talk-server/services/mongodb';
|
||||
async function createTenantRouter(config: Config, db: Db): Promise<Router> {
|
||||
const router = express.Router({ mergeParams: true });
|
||||
|
||||
if (config.get('env') === 'development') {
|
||||
// GraphiQL
|
||||
router.get(
|
||||
'/graphiql',
|
||||
playground(req => ({
|
||||
endpoint: `/api/tenant/${req.params.tenantID}/graphql`,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
// Tenant API
|
||||
router.use('/graphql', express.json(), tenantGraphMiddleware(db));
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
async function createAPIRouter(config: Config, db: Db): Promise<Router> {
|
||||
// Create a router.
|
||||
const router = express.Router({ mergeParams: true });
|
||||
|
||||
// Configure the tenant routes.
|
||||
router.use('/tenant/:tenantID', await createTenantRouter(config, db));
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
async function createRouter(config: Config): Promise<Router> {
|
||||
// Setup MongoDB.
|
||||
const db = await create(config);
|
||||
|
||||
// Create a router.
|
||||
const router = express.Router({ mergeParams: true });
|
||||
|
||||
router.use('/api', await createAPIRouter(config, db));
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* createApp will create a Talk Express app that can be used to handle requests.
|
||||
@@ -27,16 +70,8 @@ export async function createApp(
|
||||
// Static Files
|
||||
app.use(serveStatic);
|
||||
|
||||
if (config.get('env') === 'development') {
|
||||
// GraphiQL
|
||||
app.get('/graphiql', graphiql());
|
||||
}
|
||||
|
||||
// Setup MongoDB.
|
||||
const db = await create(config);
|
||||
|
||||
// API
|
||||
app.use('/api/graphql', express.json(), graphql({ schema, db }));
|
||||
// Mount the router.
|
||||
app.use(await createRouter(config));
|
||||
|
||||
// Error Handling
|
||||
app.use(errorLogger);
|
||||
|
||||
Reference in New Issue
Block a user