escrcpy/electron/copilot/index.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

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