Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
  • Loading branch information
rkamysz committed Jul 25, 2023
1 parent 5a19e1b commit 5713c11
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/api/api.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Command } from 'commander';

export const apiCommand = new Command();

apiCommand
.version('1.0', '-v, --version')
.option('-p, --port <port>')
.parse(process.argv);
16 changes: 16 additions & 0 deletions src/api/api.dependencies.ts
Original file line number Diff line number Diff line change
@@ -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<Result>;
}
5 changes: 4 additions & 1 deletion src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { ApiConfig } from './api.types';

export class Api<WebFramework = unknown> {
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');
Expand Down
6 changes: 5 additions & 1 deletion src/api/api.types.ts
Original file line number Diff line number Diff line change
@@ -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<DatabaseConfig = UnknownObject> = {
port: number;
Expand Down
4 changes: 3 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
@@ -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';
21 changes: 16 additions & 5 deletions src/api/start-api.ts
Original file line number Diff line number Diff line change
@@ -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 <WebFramework = unknown>(
api: Api<WebFramework>,
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<ApiCommandOptions>();
const config = buildApiConfig(vars, dependencies.databaseConfigBuilder, options);

api.setup(config);

routes.forEach(route => {
Route.mount(api, route);
});
Expand Down
11 changes: 7 additions & 4 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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),
});

Expand Down

0 comments on commit 5713c11

Please sign in to comment.