include weixin-ilink-gateway source
This commit is contained in:
1 parent
5f8428a318
commit
43a6f7b72b
51 files changed
+5351
-1
No files matched your search
@@ -0,0 +1,87 @@
|
||||
import { Hono } from 'hono'
|
||||
import { logger as honoLogger } from 'hono/logger'
|
||||
import { logger } from '../lib/logger.js'
|
||||
import { HttpStatus } from './lib/http.js'
|
||||
import authRoutes from './routes/auth.js'
|
||||
import configRoutes from './routes/config.js'
|
||||
import consoleRoutes from './routes/console.js'
|
||||
import mediaRoutes from './routes/media.js'
|
||||
import messagesRoutes from './routes/messages.js'
|
||||
import statusRoutes from './routes/status.js'
|
||||
import typingRoutes from './routes/typing.js'
|
||||
import webhookRoutes from './routes/webhook.js'
|
||||
import type { Credentials } from './state.js'
|
||||
import { getCredentials } from './state.js'
|
||||
|
||||
type Env = {
|
||||
Variables: {
|
||||
credentials: Credentials
|
||||
}
|
||||
}
|
||||
|
||||
export const createApp = (apiKey: string): Hono => {
|
||||
const app = new Hono()
|
||||
|
||||
// HTTP request/response logging
|
||||
const logFn = (msg: string) => logger.info('http', msg)
|
||||
app.use('/*', honoLogger(logFn))
|
||||
|
||||
// Bearer token auth (skip for /webhook — uses token-in-URL auth)
|
||||
app.use('/*', async (c, next) => {
|
||||
if (c.req.path.startsWith('/webhook') || c.req.path.startsWith('/console')) {
|
||||
await next()
|
||||
return
|
||||
}
|
||||
const header = c.req.header('Authorization')
|
||||
if (!header) {
|
||||
logger.warn('http', `${c.req.method} ${c.req.path} → 401 Missing Authorization header`)
|
||||
return c.json({ ok: false, message: 'Missing Authorization header' }, HttpStatus.UNAUTHORIZED)
|
||||
}
|
||||
const match = header.match(/^Bearer\s+(.+)$/i)
|
||||
if (!match || match[1] !== apiKey) {
|
||||
logger.warn('http', `${c.req.method} ${c.req.path} → 401 Invalid token`)
|
||||
return c.json({ ok: false, message: 'Invalid token' }, HttpStatus.UNAUTHORIZED)
|
||||
}
|
||||
await next()
|
||||
})
|
||||
|
||||
// Public routes (no login required)
|
||||
app.route('/auth', authRoutes)
|
||||
app.route('/status', statusRoutes)
|
||||
app.route('/webhook', webhookRoutes)
|
||||
app.route('/console', consoleRoutes)
|
||||
|
||||
// Login guard: check credentials and inject into context
|
||||
const authedApp = new Hono<Env>()
|
||||
|
||||
authedApp.use('/*', async (c, next) => {
|
||||
const creds = getCredentials()
|
||||
if (!creds) {
|
||||
return c.json({ ok: false, message: 'Not logged in' }, HttpStatus.UNAUTHORIZED)
|
||||
}
|
||||
c.set('credentials', creds)
|
||||
await next()
|
||||
})
|
||||
|
||||
authedApp.route('/messages', messagesRoutes)
|
||||
authedApp.route('/media', mediaRoutes)
|
||||
authedApp.route('/typing', typingRoutes)
|
||||
authedApp.route('/config', configRoutes)
|
||||
|
||||
app.route('/', authedApp)
|
||||
|
||||
// Global error handler
|
||||
app.onError((err, c) => {
|
||||
logger.error('http', `${c.req.method} ${c.req.path} → 500`, {
|
||||
error: err.message,
|
||||
})
|
||||
return c.json({ ok: false, message: err.message }, HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
})
|
||||
|
||||
// 404
|
||||
app.notFound((c) => {
|
||||
return c.json({ ok: false, message: `Cannot ${c.req.method} ${c.req.path}` }, HttpStatus.NOT_FOUND)
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
Reference in new issue
Block a user