<commit_before><%

require 'base64'

hostname = ENV['VELOCITA_HOSTNAME']
tls_enabled = ENV['VELOCITA_TLS_ENABLED'] == 'true'
public_port = tls_enabled ? ENV['VELOCITA_HTTPS_PORT'] : ENV['VELOCITA_HTTP_PORT']
public_scheme = tls_enabled ? 'https' : 'http'
full_host = "#{public_scheme}://#{hostname}:#{public_port}"

caches = {}
mirrors = {}

ENV.keys.map { |key| key.to_s }
    .select { |key| key.match(/^MIRROR_/) }
    .map { |key| key[/^MIRROR_(.+?)_/, 1] }
    .uniq
    .each do |name|
        url = ENV["MIRROR_#{name}_URL"].dup
        unless url.end_with? "/"
            url << "/"
        end

        cache_name = ENV["MIRROR_#{name}_CACHE"] || name.downcase
        unless caches.key?(cache_name)
            caches[cache_name] = {
                :size   => ENV["MIRROR_#{name}_CACHE_SIZE"] || '100m',
                :expiry => ENV["MIRROR_#{name}_CACHE_EXPIRY"] || '3650d',
            }
        end

        mirrors[name.downcase] = {
            :type       => ENV["MIRROR_#{name}_TYPE"] || 'simple',
            :url        => url,
            :cache      => cache_name,
            :auth_type  => ENV["MIRROR_#{name}_AUTH_TYPE"] || 'basic',
            :auth_user  => ENV["MIRROR_#{name}_AUTH_USERNAME"] || '',
            :auth_pass  => ENV["MIRROR_#{name}_AUTH_PASSWORD"] || '',
            :auth_token => ENV["MIRROR_#{name}_AUTH_TOKEN"] || '',
        }
    end

-%>
# Caching configuration
proxy_cache_convert_head off;
proxy_cache_key $request_method$scheme$proxy_host$request_uri;
proxy_cache_lock on;
proxy_cache_revalidate on;
proxy_cache_use_stale error timeout http_403 http_404 http_429 http_500 http_502 http_503 http_504;
proxy_cache_valid 200 302 1d;
proxy_cache_valid 404 30s;

# Proxy configuration
proxy_connect_timeout 10s;
proxy_http_version 1.1;
proxy_max_temp_file_size 2m;
proxy_read_timeout 10s;
proxy_send_timeout 10s;
proxy_set_header User-Agent "Velocita Composer Cache";
proxy_ssl_protocols TLSv1.3 TLSv1.2;

# Remove and ignore headers from proxy server responses
proxy_hide_header Cache-Control;
proxy_hide_header Expires;
proxy_hide_header Strict-Transport-Security;
proxy_ignore_headers Cache-Control Expires;

# Caching paths
<%- caches.each do |name, cache| -%>
proxy_cache_path /var/cache/velocita/<%= name %> levels=2:2 keys_zone=<%= name %>:5m inactive=<%= cache[:expiry] %> max_size=<%= cache[:size] %>;
<%- end -%>

# Proxy redirects
<%- mirrors.each do |name, mirror| -%>
proxy_redirect <%= mirror[:url] %> <%= full_host %>/mirror/<%= name %>/;
<%- end -%>

# Allow keepalive to upstream
proxy_set_header Connection "";
proxy_socket_keepalive on;
<%- if tls_enabled -%>

# TLS configuration
ssl_ciphers HIGH;
ssl_protocols TLSv1.3;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
<%- end -%>

# Enable compression
gzip on;
gzip_comp_level 9;
gzip_types application/json;
<%- if tls_enabled -%>

server {
    listen 80 default_server;
    server_name <%= hostname %>;

    return 301 <%= full_host %>$request_uri;
}
<%- end -%>

server {
<%- if tls_enabled -%>
    listen 443 ssl http2 default_server;

    ssl_certificate /etc/nginx/server.crt;
    ssl_certificate_key /etc/nginx/server.key;
<%- else -%>
    listen 80 default_server;
<%- end -%>

    server_name <%= hostname %>;
    root /var/www/html;
  <%- mirrors.each do |name, mirror| -%>

    location /mirror/<%= name %>/ {
        proxy_pass <%= mirror[:url] %>;<commit_msg>Only apply gzip for responses nearing MTU size (~1500 bytes)<commit_after><%

require 'base64'

hostname = ENV['VELOCITA_HOSTNAME']
tls_enabled = ENV['VELOCITA_TLS_ENABLED'] == 'true'
public_port = tls_enabled ? ENV['VELOCITA_HTTPS_PORT'] : ENV['VELOCITA_HTTP_PORT']
public_scheme = tls_enabled ? 'https' : 'http'
full_host = "#{public_scheme}://#{hostname}:#{public_port}"

caches = {}
mirrors = {}

ENV.keys.map { |key| key.to_s }
    .select { |key| key.match(/^MIRROR_/) }
    .map { |key| key[/^MIRROR_(.+?)_/, 1] }
    .uniq
    .each do |name|
        url = ENV["MIRROR_#{name}_URL"].dup
        unless url.end_with? "/"
            url << "/"
        end

        cache_name = ENV["MIRROR_#{name}_CACHE"] || name.downcase
        unless caches.key?(cache_name)
            caches[cache_name] = {
                :size   => ENV["MIRROR_#{name}_CACHE_SIZE"] || '100m',
                :expiry => ENV["MIRROR_#{name}_CACHE_EXPIRY"] || '3650d',
            }
        end

        mirrors[name.downcase] = {
            :type       => ENV["MIRROR_#{name}_TYPE"] || 'simple',
            :url        => url,
            :cache      => cache_name,
            :auth_type  => ENV["MIRROR_#{name}_AUTH_TYPE"] || 'basic',
            :auth_user  => ENV["MIRROR_#{name}_AUTH_USERNAME"] || '',
            :auth_pass  => ENV["MIRROR_#{name}_AUTH_PASSWORD"] || '',
            :auth_token => ENV["MIRROR_#{name}_AUTH_TOKEN"] || '',
        }
    end

-%>
# Caching configuration
proxy_cache_convert_head off;
proxy_cache_key $request_method$scheme$proxy_host$request_uri;
proxy_cache_lock on;
proxy_cache_revalidate on;
proxy_cache_use_stale error timeout http_403 http_404 http_429 http_500 http_502 http_503 http_504;
proxy_cache_valid 200 302 1d;
proxy_cache_valid 404 30s;

# Proxy configuration
proxy_connect_timeout 10s;
proxy_http_version 1.1;
proxy_max_temp_file_size 2m;
proxy_read_timeout 10s;
proxy_send_timeout 10s;
proxy_set_header User-Agent "Velocita Composer Cache";
proxy_ssl_protocols TLSv1.3 TLSv1.2;

# Remove and ignore headers from proxy server responses
proxy_hide_header Cache-Control;
proxy_hide_header Expires;
proxy_hide_header Strict-Transport-Security;
proxy_ignore_headers Cache-Control Expires;

# Caching paths
<%- caches.each do |name, cache| -%>
proxy_cache_path /var/cache/velocita/<%= name %> levels=2:2 keys_zone=<%= name %>:5m inactive=<%= cache[:expiry] %> max_size=<%= cache[:size] %>;
<%- end -%>

# Proxy redirects
<%- mirrors.each do |name, mirror| -%>
proxy_redirect <%= mirror[:url] %> <%= full_host %>/mirror/<%= name %>/;
<%- end -%>

# Allow keepalive to upstream
proxy_set_header Connection "";
proxy_socket_keepalive on;
<%- if tls_enabled -%>

# TLS configuration
ssl_ciphers HIGH;
ssl_protocols TLSv1.3;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
<%- end -%>

# Enable compression
gzip on;
gzip_comp_level 9;
gzip_min_length 1300;
gzip_types application/json;
<%- if tls_enabled -%>

server {
    listen 80 default_server;
    server_name <%= hostname %>;

    return 301 <%= full_host %>$request_uri;
}
<%- end -%>

server {
<%- if tls_enabled -%>
    listen 443 ssl http2 default_server;

    ssl_certificate /etc/nginx/server.crt;
    ssl_certificate_key /etc/nginx/server.key;
<%- else -%>
    listen 80 default_server;
<%- end -%>

    server_name <%= hostname %>;
    root /var/www/html;
  <%- mirrors.each do |name, mirror| -%>

    location /mirror/<%= name %>/ {
        proxy_pass <%= mirror[:url] %>;
{'fraction_non_alphanumeric': 0.15031254542811456, 'fraction_numerical': 0.023549934583515047, 'mean_word_length': 3.5502645502645502, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '946515', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2524, 'n_words': 485}