-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chain-follower): add WIP Dgraph client and service
Also implements envalid for process.env handling
- Loading branch information
Showing
52 changed files
with
515 additions
and
32 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { dummyLogger } from 'ts-log' | ||
import dgraph from 'dgraph-js' | ||
|
||
export interface DgraphClient { | ||
setSchema: (schema: string) => Promise<void> | ||
} | ||
|
||
export const createDgraphClient = (address: string, logger = dummyLogger): DgraphClient => { | ||
const clientStub = new dgraph.DgraphClientStub(address) | ||
const dgraphClient = new dgraph.DgraphClient(clientStub) | ||
|
||
return { | ||
async setSchema (schema) { | ||
const op = new dgraph.Operation() | ||
op.setSchema(schema) | ||
await dgraphClient.alter(op) | ||
logger.info('Dgraph schema set') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { LogLevelString } from 'bunyan' | ||
import { cleanEnv, EnvError, host, makeValidator, port, Spec } from 'envalid' | ||
|
||
// Todo: Hoist to util package, dedupe | ||
// custom validator same as str from envalid but with typed return type | ||
const str = <T extends string = string> (spec?: Spec<T>) => makeValidator((input: string) => { | ||
if (typeof input === 'string') return input as T | ||
throw new EnvError(`Not a string: "${input}"`) | ||
})(spec) | ||
|
||
export const getConfig = () => cleanEnv(process.env, { | ||
dgraphAddress: host(), | ||
loggerMinSeverity: str<LogLevelString>({ choices: ['trace', 'debug', 'info', 'warn', 'error', 'fatal'] }), | ||
ogmiosHost: host({ default: undefined }), | ||
ogmiosPort: port({ default: undefined }) | ||
}) | ||
|
||
export type Config = ReturnType<typeof getConfig> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './ChainFollower' | ||
export * from './DgraphClient' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { createDgraphClient } from './DgraphClient' | ||
import { getConfig } from './config' | ||
import { Logger } from 'ts-log' | ||
import { createLogger } from 'bunyan' | ||
import onDeath from 'death' | ||
import { ChainFollower } from './ChainFollower' | ||
import { loadSchema} from './util' | ||
|
||
(async () => { | ||
const config = getConfig() | ||
|
||
const logger: Logger = createLogger({ | ||
name: 'chain-follower', | ||
level: config.loggerMinSeverity | ||
}) | ||
|
||
try { | ||
const dgraphClient = createDgraphClient(config.dgraphAddress) | ||
await dgraphClient.setSchema(await loadSchema()) | ||
|
||
const chainFollower = new ChainFollower(logger) | ||
|
||
onDeath(async (signal) => { | ||
logger.error({ signal }, 'About to exit the process') | ||
await chainFollower.shutdown() | ||
process.exit(1) | ||
}) | ||
|
||
await chainFollower.initialize({ host: config.ogmiosHost, port: config.ogmiosPort }) | ||
await chainFollower.start(['origin']) | ||
} catch (error) { | ||
logger.error(error) | ||
process.exit(1) | ||
} | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { readFile } from 'fs-extra' | ||
|
||
export const loadSchema = async (): Promise<string> => readFile('./schema', 'utf-8') |
Oops, something went wrong.