nniRestServer.ts 1.28 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
3
4
5
6

'use strict';

import * as bodyParser from 'body-parser';
chicm-ms's avatar
chicm-ms committed
7
import * as express from 'express';
goooxu's avatar
goooxu committed
8
import * as path from 'path';
9
10
import * as component from '../common/component';
import { RestServer } from '../common/restServer'
chicm-ms's avatar
chicm-ms committed
11
import { getLogDir } from '../common/utils';
12
13
14
15
16
import { createRestHandler } from './restHandler';

/**
 * NNI Main rest server, provides rest API to support
 * # nnictl CLI tool
goooxu's avatar
goooxu committed
17
 * # NNI WebUI
18
 *
19
20
 */
@component.Singleton
goooxu's avatar
goooxu committed
21
export class NNIRestServer extends RestServer {
22
    private readonly API_ROOT_URL: string = '/api/v1/nni';
chicm-ms's avatar
chicm-ms committed
23
    private readonly LOGS_ROOT_URL: string = '/logs';
24
25
26
27
28
29
30
31
32
33
34
35

    /**
     * constructor to provide NNIRestServer's own rest property, e.g. port
     */
    constructor() {
        super();
    }

    /**
     * NNIRestServer's own router registration
     */
    protected registerRestHandler(): void {
goooxu's avatar
goooxu committed
36
        this.app.use(express.static('static'));
37
38
        this.app.use(bodyParser.json());
        this.app.use(this.API_ROOT_URL, createRestHandler(this));
chicm-ms's avatar
chicm-ms committed
39
        this.app.use(this.LOGS_ROOT_URL, express.static(getLogDir()));
goooxu's avatar
goooxu committed
40
41
42
        this.app.get('*', (req: express.Request, res: express.Response) => {
            res.sendFile(path.resolve('static/index.html'));
        });
43
44
    }
}