fix: move stats initialization into proxyservice

This commit is contained in:
Ben Allfree 2024-11-28 00:41:45 -08:00
parent 00f3a4dc0a
commit fec6d4fa59

View File

@ -27,6 +27,18 @@ export type ProxyMiddleware = (
logger: Logger, logger: Logger,
) => boolean | Promise<boolean> ) => boolean | Promise<boolean>
export type ProxyServiceConfig = SingletonBaseConfig & {
coreInternalUrl: string
}
export const proxyService = mkSingleton(
async (
config: ProxyServiceConfig,
): Promise<{ use: ReturnType<typeof express>['use'] }> => {
const _proxyLogger = LoggerService().create('ProxyService')
const { dbg, error, info, trace, warn } = _proxyLogger
const { coreInternalUrl } = config
const stats = (() => { const stats = (() => {
const metrics = { const metrics = {
requests: 0, requests: 0,
@ -73,23 +85,14 @@ const stats = (() => {
metrics.ips.set(ip, (metrics.ips.get(ip) || 0) + 1) metrics.ips.set(ip, (metrics.ips.get(ip) || 0) + 1)
}, },
addCountry: (country: string) => { addCountry: (country: string) => {
metrics.countries.set(country, (metrics.countries.get(country) || 0) + 1) metrics.countries.set(
country,
(metrics.countries.get(country) || 0) + 1,
)
}, },
} }
})() })()
export type ProxyServiceConfig = SingletonBaseConfig & {
coreInternalUrl: string
}
export const proxyService = mkSingleton(
async (
config: ProxyServiceConfig,
): Promise<{ use: ReturnType<typeof express>['use'] }> => {
const _proxyLogger = LoggerService().create('ProxyService')
const { dbg, error, info, trace, warn } = _proxyLogger
const { coreInternalUrl } = config
const proxy = httpProxy.createProxyServer({}) const proxy = httpProxy.createProxyServer({})
proxy.on('error', (err, req, res, target) => { proxy.on('error', (err, req, res, target) => {
warn(`Proxy error ${err} on ${req.url} (${req.headers.host})`) warn(`Proxy error ${err} on ${req.url} (${req.headers.host})`)