watcher-ts/packages/watcher/src/server.ts
Ashwin Phatak 4e0ef7c852
Implement typescript strict mode and ESLint in ipld-eth-client and cache packages (#37)
* Set up typescript build.

* Setup eslint in cache package.

* Automatic lint fixes.

* Fix typescript return types.

* Fix typescript argument type warnings.

* Set up typescript build and eslint.

* Automatic lint fixes.

* Fix typescript explicit any warnings.

* Add argument types.

* Fix return type warnings.

* Fix typescript errors.

* Implement declaration in types directory.

Co-authored-by: nikugogoi <95nikass@gmail.com>
2021-06-04 12:04:12 +05:30

67 lines
1.5 KiB
TypeScript

import assert from 'assert';
import express, { Application, Request, Response } from 'express';
import { graphqlHTTP } from 'express-graphql';
import fs from 'fs-extra';
import path from 'path';
import toml from 'toml';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers'
import debug from 'debug';
import { createSchema } from './gql';
const log = debug('vulcanize:server');
export const createServer = async () => {
const argv = yargs(hideBin(process.argv))
.option('f', {
alias: 'config-file',
demandOption: true,
describe: 'configuration file path (toml)',
type: 'string'
})
.argv
const configFile = argv['configFile'];
const configFilePath = path.resolve(configFile);
const fileExists = await fs.pathExists(configFilePath);
if (!fileExists) {
throw new Error(`Config file not found: ${configFilePath}`);
}
var config = toml.parse(await fs.readFile(configFilePath, 'utf8'));
log("config", JSON.stringify(config, null, 2));
assert(config.server, 'Missing server config');
const { host, port } = config.server;
const app: Application = express();
const schema = await createSchema(config);
app.use(
'/graphql',
graphqlHTTP({
schema,
graphiql: true,
}),
);
app.get('/', (req: Request, res: Response) => {
res.send('ERC20 Watcher');
});
app.listen(port, host, () => {
log(`Server is listening on host ${host} port ${port}`);
});
return app;
};
createServer().then(() => {
log('Starting server...');
}).catch(err => {
log(err);
});