1/*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
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 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
16const {readCSVFile, promiseRun} = require('./util/index');
17const path = require('path');
18const express = require('express');
19const bodyParser = require('body-parser');
20
21const app = express();
22const PORT = process.env.PORT || 4000;
23let executeStatus = -1;
24let message = '';
25
26app.use(express.static(path.join(__dirname, 'public')));
27
28app.use(bodyParser.json());
29
30app.get('/', (req, res) => {
31    res.sendFile(__dirname + path.join('/pages/index.html'));
32});
33
34app.get('/api/getCsv', (req, res) => {
35    readCSVFile().then((resolve, reject) => {
36        res.json(resolve);
37    })
38});
39
40app.get('/api/getExecuteStatus', (req, res) => {
41    res.json({execResult: executeStatus, mes: message});
42    if (executeStatus === 0 || executeStatus === 1) {
43        executeStatus = -1;
44    }
45});
46
47app.post('/api/runCase', (req, res) => {
48    const {list, pr} = req.body;
49    executeStatus = 2;
50    promiseRun(list, pr)
51        .then((result) => {
52            if (result.code === 0) {
53                executeStatus = 0;
54            } else if (result.code === 1) {
55                executeStatus = 1;
56                message = result.errorMes;
57            }
58        })
59    res.json({execResult: executeStatus, mes: message});
60});
61
62app.listen(PORT, () => {
63    console.log(`Server is running on port ${PORT}`);
64});