0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

fix(core): load dist files when needed (#123)

This commit is contained in:
Gao Sun 2021-09-28 12:45:04 +08:00 committed by GitHub
parent 61ac65aeb5
commit 1c3338f657
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,4 @@
import fs from 'fs';
import fs from 'fs/promises';
import { MiddlewareType } from 'koa';
import proxy from 'koa-proxies';
@ -8,7 +8,6 @@ import serveStatic from 'koa-static';
import { isProduction, mountedApps } from '@/env/consts';
const PATH_TO_UI_DIST = '../ui/build/public';
const uiDistFiles = fs.readdirSync(PATH_TO_UI_DIST);
export default function koaUIProxy<
StateT,
@ -17,12 +16,13 @@ export default function koaUIProxy<
>(): MiddlewareType<StateT, ContextT, ResponseBodyT> {
type Middleware = MiddlewareType<StateT, ContextT, ResponseBodyT>;
const developmentProxy: Middleware = proxy('*', {
target: 'http://localhost:5000',
changeOrigin: true,
logs: true,
});
const staticProxy: Middleware = serveStatic(PATH_TO_UI_DIST);
const uiProxy: Middleware = isProduction
? serveStatic(PATH_TO_UI_DIST)
: proxy('*', {
target: 'http://localhost:5000',
changeOrigin: true,
logs: true,
});
return async (ctx, next) => {
// Route has been handled by one of mounted apps
@ -31,13 +31,14 @@ export default function koaUIProxy<
}
if (!isProduction) {
return developmentProxy(ctx, next);
return uiProxy(ctx, next);
}
const uiDistFiles = await fs.readdir(PATH_TO_UI_DIST);
if (!uiDistFiles.some((file) => ctx.request.path.startsWith(`/${file}`))) {
ctx.request.path = '/';
}
return staticProxy(ctx, next);
return uiProxy(ctx, next);
};
}