github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/index.ts (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import { config, init as initConfig, shutDown as shutDownConfig } from './lib/config';
    16  import express from 'express';
    17  import bodyParser from 'body-parser';
    18  import membersRouter from './routers/members';
    19  import assetDefinitionsRouter from './routers/asset-definitions';
    20  import assetInstancesRouter from './routers/asset-instances';
    21  import paymentDefinitionsRouter from './routers/payment-definitions';
    22  import paymentInstancesRouter from './routers/payment-instances';
    23  import settingsRouter from './routers/settings';
    24  import batchesRouter from './routers/batches';
    25  import { errorHandler, requestLogger, responseLogger } from './lib/request-handlers';
    26  import * as database from './clients/database';
    27  import * as settings from './lib/settings';
    28  import * as utils from './lib/utils';
    29  import * as ipfs from './clients/ipfs';
    30  import * as app2app from './clients/app2app';
    31  import * as docExchange from './clients/doc-exchange';
    32  import * as eventStreams from './clients/event-streams';
    33  import { ensureEventStreamAndSubscriptions } from './lib/event-stream';
    34  import { assetTradeHandler } from './handlers/asset-trade';
    35  import { clientEventHandler } from './handlers/client-events';
    36  import { assetInstancesPinning } from './handlers/asset-instances-pinning';
    37  
    38  const log = utils.getLogger('index.ts');
    39  
    40  export const start = () => {
    41    return initConfig(() => { app2app.reset(); docExchange.reset() })
    42      .then(() => settings.init())
    43      .then(() => database.init())
    44      .then(() => ipfs.init())
    45      .then(() => app2app.init())
    46      .then(() => docExchange.init())
    47      .then(() => ensureEventStreamAndSubscriptions())
    48      .then(() => assetInstancesPinning.init())
    49      .then(() => {
    50        eventStreams.init();
    51        const app = express();
    52  
    53        app.use(bodyParser.urlencoded({ extended: true }));
    54        app.use(bodyParser.json());
    55        app.use(requestLogger);
    56  
    57        app.use('/api/v1/members', membersRouter);
    58        app.use('/api/v1/assets/definitions', assetDefinitionsRouter);
    59        app.use('/api/v1/assets', assetInstancesRouter);
    60        app.use('/api/v1/payments/definitions', paymentDefinitionsRouter);
    61        app.use('/api/v1/payments/instances', paymentInstancesRouter);
    62        app.use('/api/v1/settings', settingsRouter);
    63        app.use('/api/v1/batches', batchesRouter);
    64  
    65        app.use(responseLogger);
    66        app.use(errorHandler);
    67  
    68        app2app.addListener(assetTradeHandler);
    69        database.addListener(clientEventHandler);
    70  
    71        const server = app.listen(config.port, () => {
    72          log.info(`Asset trail listening on port ${config.port} - log level "${utils.constants.LOG_LEVEL}"`);
    73        }).on('error', (err) => {
    74          log.error(err);
    75        });
    76  
    77        const shutDown = () => {
    78          server.close(err => {
    79            if (err) {
    80              log.error(`Error closing server. ${err}`);
    81            } else {
    82              log.info(`Stopped server.`)
    83            }
    84          });
    85          eventStreams.shutDown();
    86          database.shutDown();
    87          shutDownConfig();
    88        };
    89  
    90        return { app, shutDown };
    91  
    92      });
    93  }