All files / src/http index.ts

100% Statements 19/19
75% Branches 3/4
100% Functions 4/4
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 415x 5x     5x 5x 5x   5x 6x         6x 6x 6x   6x 6x 6x       3x 3x 3x         3x         3x 3x      
import Debug from "debug";
import express from "express";
import {Server} from "http";
import {RhizomeNode} from "../node";
import {HttpApi} from "./api";
import {HttpHtml} from "./html";
const debug = Debug('rz:http-api');
 
export class HttpServer {
  app = express();
  httpHtml: HttpHtml;
  httpApi: HttpApi;
  server?: Server;
 
  constructor(readonly rhizomeNode: RhizomeNode) {
    this.httpHtml = new HttpHtml(this.rhizomeNode);
    this.httpApi = new HttpApi(this.rhizomeNode);
 
    this.app.use(express.json());
    this.app.use('/html', this.httpHtml.router);
    this.app.use('/api', this.httpApi.router);
  }
 
  start() {
    const {httpAddr, httpPort} = this.rhizomeNode.config;
    this.httpHtml.start();
    this.server = this.app.listen({
      port: httpPort,
      host: httpAddr,
      exclusive: true
    }, () => {
      debug(`[${this.rhizomeNode.config.peerId}]`, `HTTP API bound to ${httpAddr}:${httpPort}`);
    });
  }
 
  async stop() {
    this.server?.close();
    this.httpHtml.stop();
  }
}