How to dynamically set upstream by request host (tenant)
edson-nascimento opened this issue · 4 comments
edson-nascimento commented
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the issue has not already been raised
Issue
I'm developing a multi tenant api where:
Each tenant will have a different upstream url that must be dynamically set after identification.
I do the tenant identification by domain hostname.
async function getProxyUrlByTenant(request: FastifyRequest): Promise<string> {
const tenant = await getTenantByHost(request.hostname);
if (!tenant) throw new Error('Unauthorized');
return tenant.apiUrl;
}
// Tenant routes
export async function proxyRoutes(server: FastifyInstance) {
server.get('/status', async () => {
return {
status: 'active',
at: new Date().toISOString(),
};
});
// Proxy routes
server.register(proxy, {
upstream: await getProxyUrlByTenant(/* how get request here ? */),
prefix: '/proxy',
});
}
KaKi87 commented
How did you implement it ? Thanks