server.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import bodyParser from 'body-parser'
  2. import cors from 'cors'
  3. import express from 'express'
  4. import rateLimit from 'express-rate-limit'
  5. import PouchDB from 'pouchdb'
  6. import { WebSocketServer } from 'ws'
  7. import { createHandler } from 'graphql-http/lib/use/express'
  8. import { createServer } from 'http'
  9. import { useServer } from 'graphql-ws/lib/use/ws'
  10. import { ApolloServer } from '@apollo/server'
  11. import { expressMiddleware } from '@apollo/server/express4'
  12. import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer'
  13. /* Import Schema. */
  14. import schema from './src/schema.js'
  15. /* Set (GraphQL) port. */
  16. const PORT = 6000
  17. /* Initialize databases. */
  18. const logsDb = new PouchDB(`http://${process.env.COUCHDB_USER}:${process.env.COUCHDB_PASSWORD}@127.0.0.1:5984/logs`)
  19. // const blocksDb = new PouchDB(`http://${process.env.COUCHDB_USER}:${process.env.COUCHDB_PASSWORD}@127.0.0.1:5984/blocks`)
  20. // const txsDb = new PouchDB(`http://${process.env.COUCHDB_USER}:${process.env.COUCHDB_PASSWORD}@127.0.0.1:5984/txs`)
  21. /* Initialize Express application. */
  22. const app = express()
  23. /* Set rate limits. */
  24. const limiter = rateLimit({
  25. windowMs: 2 * 60 * 1000, // NOTE: Default is 2 minutes.
  26. max: 250, // NOTE: We limit each IP to 250 requests per 2 minute window.
  27. standardHeaders: true, // NOTE: Return rate limit info in the `RateLimit-*` headers.
  28. legacyHeaders: false, // NOTE: Disable the `X-RateLimit-*` headers.
  29. })
  30. /* Apply the rate limiting middleware to all requests. */
  31. app.use(limiter)
  32. app.set('trust proxy', 3) // NOTE: 0 is localhost, 1,2 are Cloudflare
  33. app.get('/ip', (request, response) => response.send(request.ip))
  34. /* Initialize HTTP server. */
  35. const httpServer = createServer(app)
  36. /* Create WebSocket server. */
  37. const wsServer = new WebSocketServer({
  38. // NOTE: This is the `httpServer` we created in a previous step.
  39. server: httpServer,
  40. // NOTE: Serves expressMiddleware at a this path.
  41. path: '/graphql',
  42. })
  43. // Hand in the schema we just created and have the
  44. // WebSocketServer start listening.
  45. const serverCleanup = useServer({ schema }, wsServer)
  46. /* Initialize Apollo Server. */
  47. const server = new ApolloServer({
  48. schema,
  49. plugins: [
  50. // Proper shutdown for the HTTP server.
  51. ApolloServerPluginDrainHttpServer({ httpServer }),
  52. // Proper shutdown for the WebSocket server.
  53. {
  54. async serverWillStart() {
  55. return {
  56. async drainServer() {
  57. await serverCleanup.dispose()
  58. },
  59. }
  60. },
  61. },
  62. ],
  63. })
  64. /* Start Apollo Server. */
  65. await server.start()
  66. /* Initialize Express middleware. */
  67. app.use('/graphql', cors(), bodyParser.json(), expressMiddleware(server))
  68. // Now that our HTTP server is fully set up, we can listen to it.
  69. httpServer.listen(PORT, () => {
  70. console.log(`Server is now running on http://localhost:${PORT}/graphql`);
  71. })