#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    log         127.0.0.1 local2 
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will 
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode        http
    log         global
    option      dontlognull
    option      httpclose
    option      httplog
    option      forwardfor
    option      redispatch
    timeout connect 10000 # default 10 second time out if a backend is not found
    timeout client 300000
    timeout server 300000
    maxconn     60000
    retries     3

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:80
    default_backend             app

    acl is_swagger_online hdr_beg(host) -i online.swagger.io
    acl is_swagger_io hdr_beg(host) -i swagger.io
    acl is_old hdr_beg(host) -i swagger.wordnik.com
    acl is_old_editor hdr_beg(host) -i editor.swagger.wordnik.com
    acl is_validator_swagger path_beg /swagger.json

    # online spec validator
    reqrep ([^\ ]*)\ /validator/(.*) \1\ /validator/validator/\2 if is_swagger_online

    # something that didn't work
    reqrep ([^\ ]*)\ /swagger.json(.*) \1\ /validator/swagger.json if is_validator_swagger

    # swagger schema
    reqrep ^([^\ :]*)\ /v2/schema.json(.*) \1\ /swagger-api/swagger-spec/master/schemas/v2.0/schema.json\2
    acl is_swagger_spec path_beg /swagger-api/swagger-spec

    # swagger docs
    reqrep ^([^\ :]*)\ /swagger-core/documentation/annotations/apidocs/current(.*) \1\ /swagger-core/apidocs/\2
    acl is_swagger_docs path_beg /swagger-core/apidocs

    use_backend github_swagger_io if is_swagger_docs
    use_backend validator if is_swagger_online
    use_backend validator if is_validator_swagger
    use_backend github_swagger_io if is_swagger_io !is_swagger_spec
    use_backend github_swagger_spec if is_swagger_spec

    redirect location http://editor.swagger.io if is_old_editor
    redirect location http://swagger.io    if is_old

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend github_swagger_io
    balance roundrobin
    server  gh1  swagger-api.github.io:80 check

backend github_swagger_spec
    http-request set-header Host raw.githubusercontent.com
    rspirep ^Content-type:(.*) Content-Type:\ application/json
    rspirep ^Access-Control-Allow-Origin:(.*) Access-Control-Allow-Origin:*
    balance roundrobin
    server  gh2  raw.githubusercontent.com:443 check ssl verify none

backend validator
    balance roundrobin
    server  app1 127.0.0.1:8000 check

backend app
    balance roundrobin
    server  app1 127.0.0.1:8000 check
