get client from user-agent

This commit is contained in:
Hongbo Wu
2024-05-07 14:53:29 +08:00
parent 23eae7871a
commit 31233d8348
2 changed files with 22 additions and 2 deletions

View File

@ -21,6 +21,7 @@ import { articleRouter } from './routers/article_router'
import { authRouter } from './routers/auth/auth_router'
import { mobileAuthRouter } from './routers/auth/mobile/mobile_auth_router'
import { digestRouter } from './routers/digest_router'
import { explainRouter } from './routers/explain_router'
import { integrationRouter } from './routers/integration_router'
import { localDebugRouter } from './routers/local_debug_router'
import { notificationRouter } from './routers/notification_router'
@ -42,9 +43,9 @@ import { userRouter } from './routers/user_router'
import { sentryConfig } from './sentry'
import { analytics } from './utils/analytics'
import { corsConfig } from './utils/corsConfig'
import { getClientFromUserAgent } from './utils/helpers'
import { buildLogger, buildLoggerTransport, logger } from './utils/logger'
import { apiLimiter, authLimiter } from './utils/rate_limit'
import { explainRouter } from './routers/explain_router'
const PORT = process.env.PORT || 4000
@ -70,11 +71,18 @@ export const createApp = (): Express => {
// set client info in the request context
app.use(httpContext.middleware)
app.use('/api/', (req, res, next) => {
// get client info from header
const client = req.header('X-OmnivoreClient')
if (client) {
httpContext.set('client', client)
}
// TODO: get client info from user agent
// get client info from user agent
const userAgent = req.header('User-Agent')
if (userAgent) {
const client = getClientFromUserAgent(userAgent)
httpContext.set('client', client)
}
next()
})

View File

@ -411,3 +411,15 @@ export const setRecentlySavedItemInRedis = async (
})
}
}
export const getClientFromUserAgent = (userAgent: string): string => {
// for plugins, currently only obsidian and logseq are supported
const plugins = userAgent.match(/(obsidian|logseq)/i)
if (plugins) return plugins[0].toLowerCase()
// web browser
const browsers = userAgent.match(/(chrome|safari|firefox|edge|opera)/i)
if (browsers) return 'web'
return 'other'
}