var express = require('express')
var proxy = require('http-proxy-middleware')
var compression = require('compression')
const tool = require('hsright-route-register')
var app = express()
app.use(compression())
// let mesApi = 'http://118.126.103.194:8080' // 正式环境的后端服务器
let api = '47.110.145.204:59168' // 代理服务器地址
if (typeof process.env.PROXY_URL !== 'undefined' && process.env.PROXY_URL !== '') {
  api = process.env.PROXY_URL
} else {
  console.error('Env API is lost')
}
const mesApi = `http://${api}/BMS/api/`
const authApi = `http://${api}/HSRight/api/`

let registerAppMenu_result = null
let register_time = ''
const apiObj = {
  registerApi: authApi, // 菜单注册的api
  authAPI: authApi,
  mesAPI: mesApi
}

tool.registerAppMenu(authApi).then((res) => {
  registerAppMenu_result = res.data
  if (res.data) {
    console.log('---成功回调---', res.data.message)
  } else {
    console.log('res.data未返回数据')
  }
}).catch((e) => {
  registerAppMenu_result = e
  console.log('--路由注册报错啦---', e)
})
app.get('/serverinfo', function(req, res) {
  res.send({
    mesApi,
    authApi,
    registerAppMenu_result
  })
})
app.use('/', express.static('dist'))
app.use(
  '/mesAPI',
  proxy({
    target: mesApi,
    pathRewrite: {
      '^/mesAPI': ''
    },
    logLevel: 'debug'
  })
)
app.use(
  '/authAPI',
  proxy({
    target: authApi,
    pathRewrite: {
      '^/authAPI': ''
    },
    logLevel: 'debug'
  })
)
var port = process.env.PORT || 8231// 重要的一点
app.listen(port)
console.log('Running on http://localhost:' + port)
