diff --git a/src/api/api.command.ts b/src/api/api.command.ts new file mode 100644 index 0000000..3a6c292 --- /dev/null +++ b/src/api/api.command.ts @@ -0,0 +1,8 @@ +import { Command } from 'commander'; + +export const apiCommand = new Command(); + +apiCommand + .version('1.0', '-v, --version') + .option('-p, --port ') + .parse(process.argv); diff --git a/src/api/api.dependencies.ts b/src/api/api.dependencies.ts new file mode 100644 index 0000000..6f7ce27 --- /dev/null +++ b/src/api/api.dependencies.ts @@ -0,0 +1,16 @@ +import { Result } from '@alien-worlds/api-core'; +import { DatabaseConfigBuilder, Dependencies } from '../common'; +import { Api } from './api'; +import { ApiConfig } from './api.types'; + +/** + * An abstract class representing a Api dependencies. + * @class ApiDependencies + */ +export abstract class ApiDependencies extends Dependencies { + public api: Api; + + public databaseConfigBuilder: DatabaseConfigBuilder; + + public abstract initialize(config: ApiConfig): Promise; +} diff --git a/src/api/api.ts b/src/api/api.ts index 0aede4b..cf2db65 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -3,8 +3,11 @@ import { ApiConfig } from './api.types'; export class Api { protected app: WebFramework; + protected config: ApiConfig; - constructor(protected config: ApiConfig) {} + public setup(config: ApiConfig) { + this.config = config; + } public async start() { throw new Error('Method "start" not implemented'); diff --git a/src/api/api.types.ts b/src/api/api.types.ts index 1605540..9edfd9b 100644 --- a/src/api/api.types.ts +++ b/src/api/api.types.ts @@ -1,4 +1,8 @@ -import { UnknownObject } from "@alien-worlds/api-core"; +import { UnknownObject } from '@alien-worlds/api-core'; + +export type ApiCommandOptions = { + port: number; +}; export type ApiConfig = { port: number; diff --git a/src/api/index.ts b/src/api/index.ts index ce894e8..70bae94 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,4 +1,6 @@ -export * from './api'; +export * from './api.command'; +export * from './api.dependencies'; export * from './api.types'; +export * from './api'; export * from './start-api'; export * as ApiEndpoints from './endpoints'; diff --git a/src/api/start-api.ts b/src/api/start-api.ts index 3b7bd99..8de318d 100644 --- a/src/api/start-api.ts +++ b/src/api/start-api.ts @@ -1,12 +1,23 @@ import 'reflect-metadata'; -import { Route } from '@alien-worlds/api-core'; -import { Api } from './api'; +import { ConfigVars, Route } from '@alien-worlds/api-core'; +import { apiCommand } from './api.command'; +import { ApiCommandOptions } from './api.types'; +import { buildApiConfig } from '../config'; +import { ApiDependencies } from './api.dependencies'; -export const startApi = async ( - api: Api, - routes: Route[] = [] +export const startApi = async ( + dependencies: ApiDependencies, + routes: Route[] = [], + ...args: string[] ) => { + const { api } = dependencies; + const vars = new ConfigVars(); + const options = apiCommand.parse(args).opts(); + const config = buildApiConfig(vars, dependencies.databaseConfigBuilder, options); + + api.setup(config); + routes.forEach(route => { Route.mount(api, route); }); diff --git a/src/config/index.ts b/src/config/index.ts index 0a84a6c..3f01753 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,5 +1,6 @@ +import { Api } from './../api/api'; import { FilterCommandOptions, FilterConfig } from '../filter'; -import { ApiConfig } from '../api'; +import { ApiCommandOptions, ApiConfig } from '../api'; import { BlockchainConfig, BootstrapCommandOptions, BootstrapConfig } from '../bootstrap'; import { ReaderCommandOptions, ReaderConfig } from '../reader'; import { ProcessorCommandOptions, ProcessorConfig } from '../processor'; @@ -96,14 +97,16 @@ export const buildUnprocessedBlockQueueConfig = ( sizeCheckInterval: vars.getNumberEnv('UNPROCESSED_BLOCK_QUEUE_SIZE_CHECK_INTERVAL') || 2000, batchSize: vars.getNumberEnv('UNPROCESSED_BLOCK_QUEUE_BATCH_SIZE') || 100, - fastLaneBatchSize: vars.getNumberEnv('UNPROCESSED_BLOCK_QUEUE_FAST_LANE_BATCH_SIZE') || 1, + fastLaneBatchSize: + vars.getNumberEnv('UNPROCESSED_BLOCK_QUEUE_FAST_LANE_BATCH_SIZE') || 1, }); export const buildApiConfig = ( vars: ConfigVars, - databaseConfigBuilder: (vars: ConfigVars, ...args: unknown[]) => UnknownObject + databaseConfigBuilder: (vars: ConfigVars, ...args: unknown[]) => UnknownObject, + options?: ApiCommandOptions ): ApiConfig => ({ - port: vars.getNumberEnv('API_PORT') || 8080, + port: vars.getNumberEnv('API_PORT') || options.port || 8080, database: databaseConfigBuilder(vars), });