apiVersion: v1
kind: ConfigMap
metadata:
  name: fe-varnish-config
  namespace: frontend-static
data:
  default.vcl: |
    vcl 4.0;
    backend default {
      .host = "127.0.0.1";
      .port = "8080";
      .connect_timeout = 1200s;
      .first_byte_timeout = 1200s;
      .between_bytes_timeout = 1200s;
    }

    backend nodejs{
      .host = "localhost";
      .port = "3011";
      .connect_timeout = 10s;
      .first_byte_timeout = 10s;
      .between_bytes_timeout = 60s;
      .max_connections = 800;
    }

    sub vcl_recv {
      set req.http.X_ADA_DISCACHE = "true";

      if (req.http.Host == "nodejs.internal") {
        set req.backend_hint = nodejs;
        set req.http.X_ADA_PROFILE = req_top.http.X_ADA_PROFILE;
        set req.http.X_ADA_PRODUCT = req_top.http.X_ADA_PRODUCT;
        set req.http.X_ADA_PROFILETOKEN = req_top.http.X_ADA_PROFILETOKEN;
        return(pass);
      }

      if (req.http.host ~ "clicnew.internal") {
        set req.http.X_ADA_PRODTYPE = "landing";
      }

      set req.http.loc = regsub(req.url, "\?.*$", "");

      if (req.url ~ ".*[?&]+(token)=([a-zA-Z0-9%=]*).*" && (req.http.host ~ "clicqui.adaoncloud.com" || req.http.host ~ "clicnew.localhost" || req.http.host ~ "storynew.localhost")) {
        set req.http.X_ADA_PROFILETOKEN = regsub(req.url,  ".*[?&](token)=([a-zA-Z0-9%=_]*).*", "\2");
      }

      if (req.url ~ "^/([a-zA-Z0-9]*)/([a-zA-Z0-9]*)(\/?|\/?:\?|$)" && (req.http.host ~ "clicqui.adaoncloud.com" || req.http.host == "clicnew.localhost" || req.http.host ~ "storynew.localhost")) {
        set req.http.X_ADA_PRODUCT = regsub(req.http.loc,"^/([a-zA-Z0-9]*)/([a-zA-Z0-9]*)(\/?|\/?:\?|$)","\1");
        set req.http.X_ADA_PROFILE = regsub(req.http.loc,"^/([a-zA-Z0-9]*)/([a-zA-Z0-9]*)(\/?|\/?:\?|$)","\2");
        unset req.http.Cookie;
      } else if (req.url ~ "^/([a-zA-Z0-9]*)(\/?|\/?:\?|$)" && req.http.X_ADA_PROFILETOKEN) {
        set req.http.loc = regsub(req.url, "\?.*$", "");
        set req.http.X_ADA_PRODUCT = regsub(req.http.loc,"^/([a-zA-Z0-9]*)(\/?|\/?:\?|$)","\1");
        unset req.http.Cookie;
      } else {
        set req.http.X-Forwarded-For = req.url;
      }
      unset req.http.loc;

      if (req.http.host ~ "clicnew.internal" || req.http.host ~ "storynew.internal") {
        if (req.url ~ "^/userSettings") {
          set req.http.X_ADA_DISCACHE = "true";
        }
        set req.http.X_ADA_INTERNAL =  "true";
        unset req.http.Cookie;
      }

      if (req.url ~ ".*[?&]+(preview)=([a-zA-Z0-9%=]*).*") {
        set req.http.X_ADA_DISCACHE = "true";
        set req.http.X_ADA_PREVIEW = "true";
      }
    }
    sub vcl_backend_response {
      set beresp.http.X_ADA_TESTONE = bereq.http.X_ADA_TESTONE;

      if (beresp.status == 500 || beresp.status == 404) {
        set beresp.http.pippone = "plutone";
        #return (synth(beresp.status, "reason"));
        return (fail);
      }

      set beresp.do_esi = true; // Do ESI processing
      set beresp.uncacheable = false;

      if (bereq.http.X_ADA_DISCACHE) {
        set beresp.http.X_ADA_DISCACHE = bereq.http.X_ADA_DISCACHE;
      }

      if (bereq.http.X_ADA_DISCACHE == "true") {
        set beresp.ttl = 3s;
        set beresp.uncacheable = true;
        if (bereq.http.X_ADA_PRODUCT) {
            set beresp.http.X_ADA_PRODUCT = bereq.http.X_ADA_PRODUCT;
        }
        if (bereq.http.X_ADA_PROFILE) {
            set beresp.http.X_ADA_PROFILE = bereq.http.X_ADA_PROFILE;
        }
        return (deliver);
      }

      unset beresp.http.cookie;
      unset beresp.http.Set-Cookie;
      set beresp.ttl = 2w;
      if (bereq.http.X_ADA_INTERNAL) {
        set beresp.ttl = 120s;
      }

      if (bereq.http.X_ADA_METHOD) {
        set beresp.http.X_ADA_METHOD = bereq.http.X_ADA_METHOD;
      }
      return (deliver);
    }

    sub vcl_synth {
      set resp.http.Content-Type = "text/html; charset=utf-8";
      synthetic( {"<script type="text/javascript">var esiError=""} + req.url + {"";</script>"} );
      return (deliver);
    }

    sub vcl_backend_error {
      set beresp.http.Content-Type = "text/html; charset=utf-8";
      synthetic( {"<div></div>"} );
      return(deliver);
    }