forked from mikus-loli/Meting-API
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp.js
More file actions
69 lines (58 loc) · 2.09 KB
/
Copy pathapp.js
File metadata and controls
69 lines (58 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import api from './src/service/api.js'
import { handler } from './src/template.js'
import { renderHomepage } from './src/homepage.js'
import { adminPageHandler } from './src/admin/page.js'
import adminRoutes from './src/admin/api.js'
import store from './src/admin/store.js'
import cookieMonitor from './src/admin/cookie-monitor.js'
import { Hono } from 'hono'
import { logger } from 'hono/logger'
import { cors } from 'hono/cors'
import config from './src/config.js'
import { get_runtime, get_url } from './src/util.js'
import { isQishuiRequestAbort, qishuiAudioResponse } from './src/providers/qishui/audio.js'
import { apiTokenMiddleware } from './src/middleware/auth.js'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const { version: APP_VERSION } = require('./package.json')
const app = new Hono()
app.use('*', cors())
app.use('*', logger())
adminRoutes(app)
const getAdminPath = () => {
const storedPath = store.getAdminPath()
return storedPath || config.ADMIN_PATH
}
app.use('*', async (c, next) => {
const adminPath = getAdminPath()
const path = c.req.path
if (path === '/' + adminPath || path.startsWith('/' + adminPath + '/')) {
return adminPageHandler(c)
}
await next()
})
app.get('/api', apiTokenMiddleware, api)
app.get('/audio/qishui', async (c) => {
try {
return await qishuiAudioResponse(c)
} catch (error) {
if (isQishuiRequestAbort(error, c.req.raw?.signal)) {
// 浏览器切歌或重新请求 Range 时会主动关闭旧连接,不应伪装成播放失败。
return new Response(null, { status: 499 })
}
console.error('[QishuiAudio]', error?.message || error)
return c.json({ error: error?.message || 'qishui audio failed' }, 502)
}
})
app.get('/test', apiTokenMiddleware, handler)
app.get('/', (c) => {
return c.html(renderHomepage({
baseUrl: get_url(c),
runtime: get_runtime(),
port: config.PORT,
overseas: Boolean(config.OVERSEAS),
version: APP_VERSION,
}))
})
cookieMonitor.start()
export default app