nniRestServer.ts 1.39 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
import { createRestHandler } from './restHandler';
13
import { getAPIRootUrl } from '../common/experimentStartupInfo';
14
15
16
17

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

    /**
     * constructor to provide NNIRestServer's own rest property, e.g. port
     */
    constructor() {
        super();
31
        this.API_ROOT_URL = getAPIRootUrl();
32
33
34
35
36
37
    }

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