Refactor to middleware, support old routes
This commit is contained in:
27
server/middlewares/shareDomains.ts
Normal file
27
server/middlewares/shareDomains.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Context, Next } from "koa";
|
||||
import { Op } from "sequelize";
|
||||
import { parseDomain } from "@shared/utils/domains";
|
||||
import env from "@server/env";
|
||||
import { Share } from "@server/models";
|
||||
|
||||
export default function shareDomains() {
|
||||
return async function shareDomainsMiddleware(ctx: Context, next: Next) {
|
||||
const isCustomDomain = parseDomain(ctx.host).custom;
|
||||
const isDevelopment = env.ENVIRONMENT === "development";
|
||||
|
||||
if (isDevelopment || (isCustomDomain && env.isCloudHosted)) {
|
||||
const share = await Share.unscoped().findOne({
|
||||
where: {
|
||||
domain: ctx.hostname,
|
||||
published: true,
|
||||
revokedAt: {
|
||||
[Op.is]: null,
|
||||
},
|
||||
},
|
||||
});
|
||||
ctx.state.rootShare = share;
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user