escrcpy/electron/copilot/index.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-11-17 18:42:50 +08:00
import { relative } from 'node:path'
import { Hono } from 'hono'
import { serve } from '@hono/node-server'
import { serveStatic } from '@hono/node-server/serve-static'
2023-11-22 09:56:18 +08:00
import createWebSocketServer from './wss/index'
2023-11-17 18:42:50 +08:00
export default async (mainWindow) => {
const app = new Hono()
app.notFound((c) => {
2023-11-21 16:37:02 +08:00
return c.text('Escrcpy copilot 404', 404)
2023-11-17 18:42:50 +08:00
})
const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL
if (VITE_DEV_SERVER_URL) {
2023-11-22 09:56:18 +08:00
app.get('/', (ctx) => {
console.log('ctx', ctx.get('wss'))
return ctx.redirect(`${VITE_DEV_SERVER_URL}copilot/index.html`)
})
2023-11-17 18:42:50 +08:00
}
else {
app.use(
'/*',
serveStatic({
root: relative('./', process.env.DIST),
rewriteRequestPath: (path) => {
2023-11-21 16:37:02 +08:00
return path.replace(/^\//, '/copilot')
2023-11-17 18:42:50 +08:00
},
}),
)
app.use(
'/assets/*',
serveStatic({
root: relative('./', `${process.env.DIST}/assets/`),
rewriteRequestPath: (path) => {
console.log('path', path)
return path.replace(/^\/assets/, '/')
},
}),
)
}
2023-11-22 09:56:18 +08:00
createWebSocketServer()
2023-11-17 18:42:50 +08:00
serve({ fetch: app.fetch, port: 1996 })
}