var express = require('express')
var webpack = require('webpack')
var config = require('./webpack.dev.conf')
var proxyMiddleware = require('http-proxy-middleware')
// var httpProxy = require('http-proxy')

var app = express()
var compiler = webpack(config)
// var proxy = httpProxy.createProxyServer()
// Define HTTP proxies to your custom API backend
// https://github.com/chimurai/http-proxy-middleware
var strulr = 'http://192.168.11.160:31467'
// var strulr = 'http://192.168.10.14:8300'
var strurlMain = 'http://192.168.11.160:31467'
// var strurlMain = 'http://192.168.10.14:8300'
// var strurlMain = 'http://192.168.10.233:8300'
var proxyTable = {
  // 取图片的地址
  '/webapps': {
    target: strulr
    //  target: 'http://192.168.50.220:7000/ldap'
  },
  '/api': {
    target: strurlMain
    //  target: 'http://192.168.50.220:7000/ldap'
  },
  '/resource': {
    target: strurlMain
    //  target: 'http://192.168.50.220:7000/ldap'
  },

  '/images': {
    target: strulr
    //  target: 'http://192.168.50.220:7000/ldap'
  },
  '/newMinio': {
    target: strulr
    //  target: 'http://192.168.50.220:7000/ldap'
  },
  // 查找资源服务数据
  '/rs/search': {
    target: strulr
    //   target: 'http://192.168.50.220:7000/ldap'
  },
  '/phone/rs/vue': {
    target: strulr
    //   target: 'http://192.168.50.220:8300'
  },
  '/rs/vue': {
    target: strulr
    //   target: 'http://192.168.50.220:8300'
  },
  '/rs/user/userLogin': {
    target: strulr
    //   target: 'http://192.168.50.220:8300'
  },
  // 用户登录服务地址
  '/rs/user': {
    target: strulr
    //   target: 'http://192.168.50.220:7000/ldap'
  },
  '/rs/image/file':{
    target: strulr
    // target: 'http://192.168.50.220:9000/telephone'
  },
  '/rs/report/getDefectReport':{
    //target: strulr
     target: strurlMain
  },
  '/rs/report/getDefectReportTS':{
    //target: strulr
     target: strurlMain
  },
  '/rs/path/getParams': {
    target: strulr
    //  target: 'http://192.168.50.220:8300'
  },
  '/rs/sql/singleTable':{
    target: strulr
    //  target: 'http://192.168.50.220:8300'
  },
  // 安检隐患转维修
  '/rs/logic/UploadRectification':{
    target: strulr
    //  target: 'http://192.168.50.220:8300'
  },
  'rs/sql/PhoneChangeMeter':{
    target: strulr
    //  target: 'http://192.168.50.220:8300'
  },
  // '/rs/sql/GetCheckPaperByDefect':{
  //   target: 'http://localhost:8081/'
  //   //  target: 'http://192.168.50.220:8300'
  // },
  // '/rs/logic/SafeExportExcel':{
  //   target: 'http://localhost:8081/'
  //   //  target: 'http://192.168.50.220:8300'
  // },
  '/rs/path/getService':{
    target: strulr
    //  target: 'http://192.168.50.220:8300'
  },
  '/rs/logic/getInitData': {
    target: strulr
    //  target: 'http://192.168.50.220:8300'
  },
  '/rs/logic/callerSend': {
    target: strulr
    //  target: 'http://192.168.50.220:8300'
  },
  '/rs/db': {
    target: strulr
    //  target: 'http://192.168.50.220:7000/ldap'
  },
  // 图片上传存放地址
  '/rs/file': {
    target: strulr
  },
  '/rs/logic/getLogin': {
    target: strulr
    //  target: 'http://192.168.50.220:8300'
  },
  '/rs/sql/tel_singleTable_OrderBy': {
    target: strulr
    //  target: 'http://192.168.50.220:8300'
  },
  '/safecheck': {
    target: strulr
    //  target: 'http://192.168.50.220:8300'
  },
  '/rs/logic/GenCallInAllPlan': {
    target: strulr
    //  target: 'http://127.0.0.1:8081'
  },
  // 安检系统主数据服务
  '/rs': {
     target: strulr
    // target: strulr
    // target: 'http://127.0.0.1:8012/AndroidRest'
    // target: strulr
  },
  '/af-telephone': {
     target: strulr
    // target: strulr
    // target: 'http://127.0.0.1:8012/AndroidRest'
    // target: strulr
  },
  // 安检系统主数据服务
  '/minio': {
    target: strulr
    // target: strulr
    // target: 'http://127.0.0.1:8012/AndroidRest'
    // target: strulr
  }

  // 安卓测试转发地址
  // '/AndroidRest/rs': {
  //   pathRewrite:{
  //     '^/AndroidRest/rs':'/rs'
  //   },
  //   target: 'http://127.0.0.1:9001/safecheck'
  // }
}

var devMiddleware = require('webpack-dev-middleware')(compiler, {
  publicPath: config.output.publicPath,
  stats: {
    colors: true,
    chunks: false
  }
})

var hotMiddleware = require('webpack-hot-middleware')(compiler)
// force page reload when html-webpack-plugin template changes
compiler.plugin('compilation', function (compilation) {
  compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
    hotMiddleware.publish({ action: 'reload' })
    cb()
  })
})

// proxy api requests
Object.keys(proxyTable).forEach(function (context) {
  var options = proxyTable[context]
  if (typeof options === 'string') {
    options = { target: options }
  }
  // 改变源实现跨域
  //options.changeOrigin = true
  /*Object.assign(options, {
    onProxyRes(proxyRes, req, res) {
      // res.header('Access-Control-Allow-Origin', '*')
      // res.header('Access-Control-Allow-Methods', 'PUT, POST, GET,DELETE, OPTIONS')
    }
  })*/
  app.use(proxyMiddleware(context, options))
})

// handle fallback for HTML5 history API
app.use(require('connect-history-api-fallback')())
// app.use(function (req, res, next) {
//   res.header('Access-Control-Allow-Origin', '*')
//   res.header('Access-Control-Allow-Headers', 'X-Requested-With')
//   res.header('Access-Control-Allow-Methods', 'PUT, POST, GET,DELETE, OPTIONS')
//   res.header('X-Powered-By', '3,2,1')
//   res.header('Access-Control-Allow-Credentials', 'true')
//   res.header('Content-Type', 'application/json;charset=utf-8')
//   next()
// })

// serve webpack bundle output
app.use(devMiddleware)

// enable hot-reload and state-preserving
// compilation error display
app.use(hotMiddleware)

// serve pure static assets
app.use('/static', express.static('./static'))

module.exports = app.listen(8789, function (err) {
  if (err) {
    console.log(err)
    return
  }
  console.log('Listening at http://127.0.0.1:8789')
})
