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,7 +27,19 @@ export type ProxyMiddleware = (
logger: Logger,
) => boolean | Promise<boolean>
const stats = (() => {
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 metrics = {
requests: 0,
errors: 0,
@ -73,22 +85,13 @@ const stats = (() => {
metrics.ips.set(ip, (metrics.ips.get(ip) || 0) + 1)
},
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({})
proxy.on('error', (err, req, res, target) => {