import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [vue()],
  server: {
    port: 3000,
    proxy: {
      '/api': {
        target: 'http://localhost:8000',
        changeOrigin: true
      },
      '/proxy-image': {
        target: 'https://cdn-mineru.openxlab.org.cn',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/proxy-image/, ''),
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyReq, req, res) => {
            // 添加必要的请求头
            proxyReq.setHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36')
            proxyReq.setHeader('Accept', 'image/webp,image/apng,image/*,*/*;q=0.8')
            proxyReq.setHeader('Accept-Language', 'zh-CN,zh;q=0.9,en;q=0.8')
          })
          proxy.on('proxyRes', (proxyRes, req, res) => {
            // 添加CORS头
            proxyRes.headers['Access-Control-Allow-Origin'] = '*'
            proxyRes.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'
            proxyRes.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
            proxyRes.headers['Cache-Control'] = 'public, max-age=31536000'
          })
        }
      }
    }
  }
})
