app.ts


Overview

The `app.ts` file is the main entry point of the Proxy API Service. It initializes and configures an Express.js server that serves as a centralized API gateway. Its primary responsibilities include:

This file orchestrates the overall HTTP API server, delegating specific logic to imported classes and route modules.


Detailed Breakdown

Constants and Imports


Logger Initialization

export const logger = new Logger({
  namespace: ['unchained', 'proxy', 'api'],
  level: process.env.LOG_LEVEL,
})

Express App Setup

const app = express()

Middleware Registration

app.use(...middleware.common())

Health Check Endpoint

app.get('/health', async (_, res) => res.json({ status: 'ok' }))

Swagger UI and Static Assets

const options: swaggerUi.SwaggerUiOptions = {
  customCss: '.swagger-ui .topbar { display: none }',
  customSiteTitle: 'ShapeShift Proxy API Docs',
  customfavIcon: '/public/favi-blue.png',
  swaggerUrl: '/swagger.json',
}

app.use('/public', express.static(join(__dirname, '../../../../../../coinstacks/common/api/public/')))
app.use('/swagger.json', express.static(join(__dirname, './swagger.json')))
app.use('/docs', swaggerUi.serve, swaggerUi.setup(undefined, options))

Route Registration

RegisterRoutes(app)

External API Proxy Handlers

const coingecko = new CoinGecko()
app.get('/api/v1/markets/*', coingecko.handler.bind(coingecko))

const zerion = new Zerion()
app.get('/api/v1/zerion/*', zerion.handler.bind(zerion))

const zrx = new Zrx()
app.get('/api/v1/zrx/*', zrx.handler.bind(zrx))

Root Route Redirect

app.get('/', async (_, res) => {
  res.redirect('/docs')
})

Error Handling Middleware

app.use(middleware.errorHandler, middleware.notFoundHandler)

Server Startup

app.listen(PORT, () => logger.info('Server started'))

Classes and Functions

logger: Logger

logger.info('Server started')
logger.error('An unexpected error occurred')

Express Routes and Handlers

All route handlers are defined as inline functions or bound methods from proxy classes.


Important Implementation Details


Interaction with Other Files

`app.ts` acts as the central coordinator, tying together middleware, routes, API proxies, logging, and documentation into a cohesive HTTP service.


Usage Example

Start the server:

PORT=8080 LOG_LEVEL=info node dist/app.js

Check health:

curl http://localhost:8080/health
# Response: { "status": "ok" }

Access API documentation:

Open browser at http://localhost:8080/docs

Proxy a CoinGecko market request:

curl http://localhost:8080/api/v1/markets/coins/list

Mermaid Class Diagram

This diagram shows the structure of the main components instantiated and used in `app.ts` along with their key methods and properties.

classDiagram
    class ExpressApp {
        +use()
        +get()
        +listen()
    }

    class Logger {
        +info(msg: string)
        +error(msg: string)
    }

    class CoinGecko {
        +handler(req, res)
    }

    class Zerion {
        +handler(req, res)
    }

    class Zrx {
        +handler(req, res)
    }

    ExpressApp o-- Logger : uses
    ExpressApp o-- CoinGecko : uses
    ExpressApp o-- Zerion : uses
    ExpressApp o-- Zrx : uses

Summary

The `app.ts` file is the backbone of the Proxy API Service, setting up an Express server that unifies multiple external API proxies, health checks, documentation, and error handling into a single, manageable server. It leverages modular middleware and imported proxy classes to maintain clean separation of concerns while providing a consistent API surface for clients.