UNPKG

deth

Version:

Ethereum node focused on Developer Experience

37 lines (36 loc) 1.43 kB
import express from 'express'; import bodyParser from 'body-parser'; import { asyncHandler } from '@restless/restless'; import { sanitizeRPC, executeRPC, respondRPC, sanitizeRPCEnvelope } from './rpc/middlewares'; import { errorHandler, NotFoundHttpError } from './errorHandler'; import { rpcCommandsDescription } from './rpc/schema'; import { rpcExecutorFromCtx } from './rpc/rpcExecutor'; import { makeDefaultCtx } from './ctx'; import { loadConfig } from '../config/loader'; import { RealFileSystem } from '../fs/RealFileSystem'; export function getApp(ctx) { const rpcExecutor = rpcExecutorFromCtx(ctx); const app = express(); app.use(bodyParser.json({ type: '*/*' })); app.post('/', asyncHandler(sanitizeRPCEnvelope(), sanitizeRPC(rpcCommandsDescription), executeRPC(rpcExecutor), respondRPC(rpcCommandsDescription))); app.use('/health', (req, res) => { res.status(200).json({ status: 'OK', }); }); app.use('*', (_req, _res) => { throw new NotFoundHttpError(); }); app.use(errorHandler); return app; } export async function runNode(port, configPath) { const fs = new RealFileSystem(); if (configPath) { console.log(`Using ${configPath}...`); } const ctx = await makeDefaultCtx(configPath && loadConfig(fs, configPath)); ctx.logger.logNodeInfo(ctx.walletManager); const app = getApp(ctx); return app.listen(port); }