mirror of
https://github.com/wassname/talk.git
synced 2026-08-02 13:10:23 +08:00
initial commit
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
import playground from 'graphql-playground-middleware-express';
|
||||
|
||||
export default () => playground({ endpoint: '/api/graphql' });
|
||||
@@ -0,0 +1,15 @@
|
||||
import { graphqlExpress } from 'apollo-server-express';
|
||||
import { GraphQLSchema } from 'graphql';
|
||||
import Context from 'talk-server/graph/context';
|
||||
import { Db } from 'mongodb';
|
||||
|
||||
export interface GraphQLOptions {
|
||||
schema: GraphQLSchema;
|
||||
db: Db;
|
||||
}
|
||||
|
||||
export default (opts: GraphQLOptions) =>
|
||||
graphqlExpress(req => ({
|
||||
schema: opts.schema,
|
||||
context: new Context({ db: opts.db, req }),
|
||||
}));
|
||||
@@ -0,0 +1,43 @@
|
||||
import { RequestHandler, ErrorRequestHandler } from 'express';
|
||||
import logger from '../../logger';
|
||||
import now from 'performance-now';
|
||||
|
||||
export const access: RequestHandler = (req, res, next) => {
|
||||
const startTime = now();
|
||||
const end = res.end;
|
||||
res.end = (chunk: any, encodingOrCb?: string | Function, cb?: Function) => {
|
||||
// Compute the end time.
|
||||
const responseTime = Math.round(now() - startTime);
|
||||
|
||||
// Get some extra goodies from the request.
|
||||
const userAgent = req.get('User-Agent');
|
||||
|
||||
// Reattach the old end, and finish.
|
||||
res.end = end;
|
||||
if (typeof encodingOrCb === 'function') {
|
||||
res.end(chunk, encodingOrCb);
|
||||
} else {
|
||||
res.end(chunk, encodingOrCb, cb);
|
||||
}
|
||||
|
||||
// Log this out.
|
||||
logger.info(
|
||||
{
|
||||
// traceID: req.id,
|
||||
url: req.originalUrl || req.url,
|
||||
method: req.method,
|
||||
statusCode: res.statusCode,
|
||||
userAgent,
|
||||
responseTime,
|
||||
},
|
||||
'http request'
|
||||
);
|
||||
};
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
export const error: ErrorRequestHandler = (err, req, res, next) => {
|
||||
logger.error({ err }, 'http error');
|
||||
next(err);
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
import serveStatic from 'express-static-gzip';
|
||||
import path from 'path';
|
||||
|
||||
export default serveStatic(path.join(__dirname, '..', '..', 'dist'), {});
|
||||
Reference in New Issue
Block a user