chore: init read-star monorepo
This commit is contained in:
2
apps/admin/build/config/index.ts
Normal file
2
apps/admin/build/config/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './proxy'
|
||||
export * from './time'
|
||||
58
apps/admin/build/config/proxy.ts
Normal file
58
apps/admin/build/config/proxy.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import type { ProxyOptions } from 'vite'
|
||||
import { consola } from 'consola'
|
||||
import { bgRed, bgYellow, green, lightBlue } from 'kolorist'
|
||||
import { createServiceConfig } from '../../src/utils/service'
|
||||
|
||||
/**
|
||||
* Set http proxy
|
||||
*
|
||||
* @param env - The current env
|
||||
* @param enable - If enable http proxy
|
||||
*/
|
||||
export function createViteProxy(env: Env.ImportMeta, enable: boolean) {
|
||||
const isEnableHttpProxy = enable && env.VITE_HTTP_PROXY === 'Y'
|
||||
|
||||
if (!isEnableHttpProxy)
|
||||
return undefined
|
||||
|
||||
const isEnableProxyLog = env.VITE_PROXY_LOG === 'Y'
|
||||
|
||||
const { baseURL, proxyPattern, other } = createServiceConfig(env)
|
||||
|
||||
const proxy: Record<string, ProxyOptions> = createProxyItem({ baseURL, proxyPattern }, isEnableProxyLog)
|
||||
|
||||
other.forEach((item) => {
|
||||
Object.assign(proxy, createProxyItem(item, isEnableProxyLog))
|
||||
})
|
||||
|
||||
return proxy
|
||||
}
|
||||
|
||||
function createProxyItem(item: App.Service.ServiceConfigItem, enableLog: boolean) {
|
||||
const proxy: Record<string, ProxyOptions> = {}
|
||||
|
||||
proxy[item.proxyPattern] = {
|
||||
target: item.baseURL,
|
||||
changeOrigin: true,
|
||||
configure: (_proxy, options) => {
|
||||
_proxy.on('proxyReq', (_proxyReq, req, _res) => {
|
||||
if (!enableLog)
|
||||
return
|
||||
|
||||
const requestUrl = `${lightBlue('[proxy url]')}: ${bgYellow(` ${req.method} `)} ${green(`${item.proxyPattern}${req.url}`)}`
|
||||
|
||||
const proxyUrl = `${lightBlue('[real request url]')}: ${green(`${options.target}${req.url}`)}`
|
||||
|
||||
consola.log(`${requestUrl}\n${proxyUrl}`)
|
||||
})
|
||||
_proxy.on('error', (_err, req, _res) => {
|
||||
if (!enableLog)
|
||||
return
|
||||
consola.log(bgRed(`Error: ${req.method} `), green(`${options.target}${req.url}`))
|
||||
})
|
||||
},
|
||||
rewrite: path => path.replace(new RegExp(`^${item.proxyPattern}`), ''),
|
||||
}
|
||||
|
||||
return proxy
|
||||
}
|
||||
12
apps/admin/build/config/time.ts
Normal file
12
apps/admin/build/config/time.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import dayjs from 'dayjs'
|
||||
import timezone from 'dayjs/plugin/timezone'
|
||||
import utc from 'dayjs/plugin/utc'
|
||||
|
||||
export function getBuildTime() {
|
||||
dayjs.extend(utc)
|
||||
dayjs.extend(timezone)
|
||||
|
||||
const buildTime = dayjs.tz(Date.now(), 'Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss')
|
||||
|
||||
return buildTime
|
||||
}
|
||||
Reference in New Issue
Block a user