47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
|
|
import express from "express";
|
|
import cors from "cors";
|
|
import bodyParser from 'body-parser';
|
|
import mainCtrl from './robot/main.controller';
|
|
import {initData} from "./common/GConfig";
|
|
import {
|
|
registerGracefulShutdown,
|
|
registService,
|
|
unRegistService
|
|
} from './utils/system.util'
|
|
import { error } from './common/Debug'
|
|
import { Config } from './cfg/Config'
|
|
import { RedisClient } from './redis/RedisClient'
|
|
|
|
require('./common/Extend');
|
|
const app = express()
|
|
|
|
const port = Number(process.env.PORT || 2500);
|
|
|
|
let config: Config = require('../config/config.json');
|
|
const isProd = process.env.NODE_ENV === 'production'
|
|
global.isProd = isProd
|
|
initData();
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
app.use(bodyParser.json({}));
|
|
|
|
app.use('/robot', mainCtrl);
|
|
let gracefullyShutdown = function (exit: boolean = true, err?: Error) {
|
|
unRegistService(port).then(()=>{})
|
|
.catch(err => {
|
|
error('error unregistservice')
|
|
})
|
|
}
|
|
|
|
let opts = {url: config.redis}
|
|
new RedisClient(opts)
|
|
|
|
app.listen(port, async function () {
|
|
if (isProd) {
|
|
await registerGracefulShutdown((err) => gracefullyShutdown(true, err));
|
|
await registService(port)
|
|
}
|
|
console.log(`App is listening on port ${port}!`);
|
|
});
|