Skip to content

Instantly share code, notes, and snippets.

@kptdobe
Last active May 13, 2019 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kptdobe/bd0f5bb10b88783a3cefd917beb98a68 to your computer and use it in GitHub Desktop.
Save kptdobe/bd0f5bb10b88783a3cefd917beb98a68 to your computer and use it in GitHub Desktop.
How to select content repo based on subdomain (format: https://<owner>-<repo>.yourhost.com)
# Gets the content owner for the current strain and sets the X-Owner header
sub hlx_owner {
  set req.http.X-Trace = req.http.X-Trace + "; hlx_owner";

  set req.http.X-Owner = table.lookup(strain_owners, req.http.X-Strain);
  if (!req.http.X-Owner) {
    set req.http.X-Owner = table.lookup(strain_owners, "default");
  }
  
  
  # CUSTOM CODE BEGINS
  declare local var.o STRING;
  
  # compute a potential owner in the subdoamin (if subdomain contains a dash)
  # first, try 2x -- pattern (include branch)
  set var.o = if(req.http.host ~ "(.*)--(.*)--(.*)\..*\..*$", re.group.3, "");

  if (var.o == "") {
    # second, try -- pattern
    set var.o = if(req.http.host ~ "(.*)--(.*)\..*\..*$", re.group.2, "");
  }
  
  if (var.o == "") {
    # third try - pattern
    set var.o = if(req.http.host ~ "(.*)-(.*)\..*\..*$", re.group.2, "");
  }
  
  if (var.o != "") {
    set req.http.X-Owner = var.o;
  }
  # CUSTOM CODE ENDS
}

# Gets the content repo
sub hlx_repo {
  set req.http.X-Trace = req.http.X-Trace + "; hlx_repo";
  set req.http.X-Repo = table.lookup(strain_repos, req.http.X-Strain);
  if (!req.http.X-Repo) {
    set req.http.X-Repo = table.lookup(strain_repos, "default");
  }
  
  # CUSTOM CODE BEGINS
  declare local var.o STRING;
  declare local var.r STRING;
  
  # compute a potential owner in the subdoamin (if subdomain contains a dash)
  # first, try 2x -- pattern (include branch)
  set var.o = if(req.http.host ~ "(.*)--(.*)--(.*)\..*\..*$", re.group.3, "");
  set var.r = re.group.2;
  
  if (var.o == "") {
    # first, try -- pattern
    set var.o = if(req.http.host ~ "(.*)--(.*)\..*\..*$", re.group.2, "");
    set var.r = re.group.1;
  }
  
  if (var.o == "") {
    # second try - pattern
    set var.o = if(req.http.host ~ "(.*)-(.*)\..*\..*$", re.group.2, "");
    set var.r = re.group.1;
  }
  
  if (var.o != "") {
    # extract repo
    set req.http.X-Repo = var.r;
  }
  # CUSTOM CODE ENDS
}


# Gets the content ref
sub hlx_ref {
  set req.http.X-Trace = req.http.X-Trace + "; hlx_ref";
  set req.http.X-Ref = table.lookup(strain_refs, req.http.X-Strain);
  # fall back to default strain
  if (!req.http.X-Ref) {
    set req.http.X-Ref = table.lookup(strain_refs, "default");
  }
  # if default isn't set, use 'master'
  if (!req.http.X-Ref) {
    set req.http.X-Ref = "master";
  }
  
  # CUSTOM CODE BEGINS
  declare local var.ref STRING;

  # compute a potential owner in the subdoamin (if subdomain contains a dash)
  # first, try 2x -- pattern (include branch)
  set var.ref = if(req.http.host ~ "(.*)--(.*)--(.*)\..*\..*$", re.group.1, "");

  if (var.ref != "") {
    # extract repo
    set req.http.X-Ref= var.ref;
  }
  # CUSTOM CODE ENDS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment