Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements #580

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions server/handlers/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ export const create: Handler = async (req: CreateLinkReq, res) => {
domain_id
}),
customurl &&
query.link.find({
address: customurl,
domain_id
}),
!customurl && utils.generateId(domain_id),
query.link.find(
{
address: customurl,
domain_id
},
utils.isDefaultDomain(req.headers.host)
),
!customurl &&
utils.generateId(domain_id, utils.isDefaultDomain(req.headers.host)),
validators.bannedDomain(targetDomain),
validators.bannedHost(targetDomain)
]);
Expand Down Expand Up @@ -273,10 +277,13 @@ export const redirect = (app: ReturnType<typeof next>): Handler => async (

// 2. Get link
const address = req.params.id.replace("+", "");
const link = await query.link.find({
address,
domain_id: domain ? domain.id : null
});
const link = await query.link.find(
{
address,
domain_id: domain ? domain.id : null
},
utils.isDefaultDomain(host)
);

// 3. When no link, if has domain redirect to domain's homepage
// otherwise rediredt to 404
Expand Down
8 changes: 8 additions & 0 deletions server/migrations/20200211220920_constraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ export async function up(knex: Knex): Promise<any> {
REFERENCES users (id)
ON DELETE CASCADE;
`),
knex.raw(`
CREATE INDEX links_address_index
ON links (address);
CREATE INDEX links_domain_id_index
ON links (domain_id);
CREATE INDEX links_user_id_index
ON links (user_id);
`),
knex.raw(`
ALTER TABLE visits
DROP CONSTRAINT visits_link_id_foreign,
Expand Down
10 changes: 8 additions & 2 deletions server/queries/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,14 @@ export const get = async (match: Partial<Link>, params: GetParams) => {
return links;
};

export const find = async (match: Partial<Link>): Promise<Link> => {
if (match.address && match.domain_id) {
export const find = async (
match: Partial<Link>,
isDefaultDomain: boolean = false
): Promise<Link> => {
if (
(match.address && match.domain_id) ||
(match.address && isDefaultDomain)
) {
const key = redis.key.link(match.address, match.domain_id);
const cachedLink = await redis.get(key);
if (cachedLink) return JSON.parse(cachedLink);
Expand Down
12 changes: 10 additions & 2 deletions server/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ export const signToken = (user: UserJoined) =>
env.JWT_SECRET
);

export const generateId = async (domain_id: number = null) => {
export const generateId = async (
domain_id: number = null,
isDefaultDomain: boolean = false
) => {
const address = generate(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
env.LINK_LENGTH
);
const link = await query.link.find({ address, domain_id });
const link = await query.link.find({ address, domain_id }, isDefaultDomain);
if (!link) return address;
return generateId(domain_id);
};
Expand Down Expand Up @@ -168,3 +171,8 @@ export const sanitize = {
export const removeWww = (host = "") => {
return host.replace("www.", "");
};

export const isDefaultDomain = (hostUrl: string = ""): boolean => {
hostUrl = hostUrl.replace("www.", "");
return !!(hostUrl === env.DEFAULT_DOMAIN);
};