#---------------------------------------------------------------------
# Managed by puppet
#---------------------------------------------------------------------

global
<%- if @enable_haproxy_nbproc -%>
    nbproc 4
<%- end -%>
    pidfile           <%= @rundir %>/<%= @name %>.pid
<%- @log_address.each do |log| -%>
    log               <%= log %> local0 info alert
<%- end -%>
<%- if @log_send_hostname -%>
    log-send-hostname <%= @fqdn %>
<%- end -%>
    log-tag           haproxy_<%= @name %>
    stats socket      <%= @rundir %>/<%= @name %>_socket level admin
    user              haproxy
    group             haproxy
    chroot            /var/lib/haproxy
    daemon

defaults
    mode tcp
    log global
    option httplog
    timeout client 300s   # Client and server timeout must match the longest
    timeout server 300s   # time we may wait for a response from the server.
    timeout queue  30s   # Don't queue requests too long if saturated.
    timeout connect 4s   # There's no reason to change this one.
    timeout http-request 5s # A complete request may never take that long.
    maxconn 20000
    retries 2
    option redispatch
    option dontlognull
    option http-server-close
    log-format %ci:%cp\ [%t]\ %ID\ %ft\ %b/%s\ %Tq/%Tw/%Tc/%Tr/%Tt\ %ST\ %B\ %CC\ %CS\ %tsc\ %ac/%fc/%bc/%sc/%rc\ %sq/%bq\ %hr\ %hs\ %U\ %{+Q}r

frontend http_<%= @name %>
    #Add a request id
    unique-id-format %{+X}o\ %ci:%cp_%fi:%fp_%ts_%rt
    unique-id-header X-Some-Header-3 if !is_request_id
    http-response set-header X-Some-Header-3 %ID

    capture request header X-Some-Header-3 len 60
    capture cookie e-p-v1 len 32
    capture request header Host len 60
    capture request header Range len 30
    capture request header User-Agent len 150

    # http front end since some webdav and image requests are made over http, send as is to L1
    mode http
    reqidel ^Cookie:.*
    bind <%= @bind['ip'].map{ |x| x + ":" + @bind['http_port'].to_s }.join(",") %>

    # haproxy health status
    monitor-uri /health-check
    #

    option forwardfor
    capture cookie e-p-v1 len 32

    # zlib Compression
    compression algo gzip
    compression type text/html text/tab-separated-values text/plain text/xml text/css text/javascript application/x-javascript application/javascript application/vnd.ms-fontobject application/font-sfnt image/svg+xml

    default_backend a11

frontend https_<%= @name %>
    mode http
    reqidel ^Cookie:.*
    option forwardfor

    #define a dummy backend to keep a mapping of domain to proxy
    use_backend %[hdr(host),lower,map(sync.map)] if FALSE
    tcp-request content capture hdr(host),map(sync.map) len 80

    #Add a request id
    unique-id-format %{+X}o\ %ci:%cp_%fi:%fp_%ts_%rt
    unique-id-header X-Some-Header-3 if !is_request_id
    http-response set-header X-Some-Header-3 %ID

    capture request header X-Some-Header-3 len 60
    capture request header Host len 60
    capture request header Range len 30
    capture request header User-Agent len 150	

    http-request set-header X-11  %[hdr(Host)]
    http-request set-header X-22 https    

    http-request set-header X-13  %[req.fhdr(User-Agent)] 	

    # zlib Compression 
    compression algo gzip
    compression type text/html text/tab-separated-values text/plain text/xml text/css text/javascript application/x-javascript application/javascript application/vnd.ms-fontobject application/font-sfnt image/svg+xml

    reqadd X-Forwarded-Proto:\ https

    use_backend a11 if { path -m reg ^/some-complex-reg$ }

    use_backend a11 if { hdr(User-Agent)  -m reg  ^/some-complex-reg$ }

    use_backend a11 if ! {  hdr(Host) -m reg ^/some-complex-reg$ }

    rspadd X-Content-Type-Options:\ nosniff

    bind <%= @bind['ip'].map{ |x| x + ":" + @bind['https_port'].to_s }.join(",") %> ssl crt <%= @ssl_cert_path %> no-sslv3 ciphers  ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES128-SHA:AES256-SHA256:AES256-SHA:HIGH:!MD5:!aNULL:!EDH:!AESGCM

    monitor-uri /health-check

    acl secure dst_port eq 443

    rsprep ^Set-Cookie:\ (.*) Set-Cookie:\ \1;\ Secure if secure

    rspadd Strict-Transport-Security:\ max-age=31536000 if secure

    tcp-request inspect-delay 10s
    tcp-request content accept if { req_ssl_hello_type 1 }


    .. lot of acls...
    .. lot of acls...
    .. lot of acls...
    .. lot of acls...



    use_backend s11 if  some_acl
    use_backend e11 if  some_acl
    use_backend e11 if  some_acl
    use_backend e11 if  some_acl
    use_backend e11 if  some_acl
    use_backend e11 if  some_acl

    default_backend a11

backend a11
    mode http
    option httpchk GET /lvs_ping.html
    http-check expect status 200
    default-server maxconn 500 inter 30000
    http-response set-map(l1_sync.map) %[res.hdr(X-Some-Header)]  %[res.hdr(X-Some-Header-1)]
    rspidel ^X-Some-Header:.*
    rspidel ^X-Some-Header-1:.*
    <%- @backends['ha_sync_backends'].each do |node| -%>
    server apache_<%= node %> <%= node%>:8080 check port 8085
    <%- end -%>

backend l11
    mode http
    compression algo gzip
    compression type application/json text/html text/tab-separated-values text/plain text/xml text/css text/javascript application/x-javascript application/javascript application/vnd.ms-fontobject application/font-sfnt image/svg+xml application/font-sfnt application/font-woff
    option httpchk GET /health-check
    http-check expect status 200
    server l11 <%= @backends['l11'] %> check inter 30000

backend s22
    mode http
    option httpchk GET /health-check
    http-check expect status 200
    server sync_pool <%= @backends['s22'] %> check inter 30000

backend s11
    mode http
    option httpchk GET /health-check
    reqrep ^([^\ ]*)\ /v(.+)/(.*)$ \1\ /sync/rest/public/v\2/\3
    server s11 <%= @backends['s11'] %>

backend b11
    mode http
    option httpchk GET /health-check
    http-check expect status 200
    server backup_pool <%= @backends['b11'] %> check inter 30000

backend e11
    mode http
    option httpchk GET /health-check
    http-check expect status 200
    reqadd   X-Some-Header-5:\ true
    default-server inter 30000
    <%- @backends['e11'].each do |node| -%>
    server <%= node %>
    <%- end -%>



