1+ import { Organizations } from "@acm-uiuc/js-shared" ;
12import {
23 DynamoDBClient ,
34 QueryCommand ,
@@ -14,6 +15,38 @@ const DYNAMODB_TABLE = "infra-core-api-linkry";
1415const FALLBACK_URL = process . env . FALLBACK_URL || "https://acm.illinois.edu/404" ;
1516const DEFAULT_URL = process . env . DEFAULT_URL || "https://www.acm.illinois.edu" ;
1617const CACHE_TTL = "30" ; // seconds to hold response in PoP
18+ const BASE_DOMAINS = [ ".acm.illinois.edu" , ".aws.qa.acmuiuc.org" , ".acm.gg" ] ;
19+
20+ const entries = Object . entries ( Organizations ) ;
21+ const shortToOrgCodeMapper : Record < string , string > = { } ;
22+ for ( const item of entries ) {
23+ shortToOrgCodeMapper [ item [ 1 ] . shortcode ] = item [ 0 ] ;
24+ }
25+
26+ function getSlugToQuery ( path : string , host : string ) : string {
27+ let cleanedHost = host . toLowerCase ( ) ;
28+
29+ for ( const domain of BASE_DOMAINS ) {
30+ if ( cleanedHost . endsWith ( domain ) ) {
31+ cleanedHost = cleanedHost . substring (
32+ 0 ,
33+ cleanedHost . length - domain . length ,
34+ ) ;
35+ break ;
36+ }
37+ }
38+
39+ const hostParts = cleanedHost . split ( "." ) ;
40+
41+ if ( hostParts . length > 1 && host !== "acm" ) {
42+ const short = hostParts [ 0 ] ;
43+ if ( shortToOrgCodeMapper [ short ] ) {
44+ return `${ shortToOrgCodeMapper [ short ] } #${ path } ` ;
45+ }
46+ }
47+
48+ return path ;
49+ }
1750
1851/**
1952 * Determine which DynamoDB replica to use based on Lambda execution region
@@ -50,8 +83,9 @@ export const handler = async (
5083) : Promise < CloudFrontRequestResult > => {
5184 const request = event . Records [ 0 ] . cf . request ;
5285 const path = request . uri . replace ( / ^ \/ + / , "" ) ;
53-
54- console . log ( `Processing path: ${ path } ` ) ;
86+ const host = request . headers . host ?. [ 0 ] ?. value || "" ;
87+ const slugToQuery = getSlugToQuery ( path , host ) ;
88+ console . log ( `Host: ${ host } , Path: ${ path } , Querying Slug: ${ slugToQuery } ` ) ;
5589
5690 if ( ! path ) {
5791 return {
@@ -73,7 +107,7 @@ export const handler = async (
73107 KeyConditionExpression :
74108 "slug = :slug AND begins_with(access, :owner_prefix)" ,
75109 ExpressionAttributeValues : {
76- ":slug" : { S : path } ,
110+ ":slug" : { S : slugToQuery } ,
77111 ":owner_prefix" : { S : "OWNER#" } ,
78112 } ,
79113 ProjectionExpression : "redirect" ,
0 commit comments