include mempool/production/nginx/server-common.conf;
include mempool/production/nginx/location-api-v1-services.conf;

proxy_cache markets;
proxy_cache_valid 200 30s;

# route esplora APIs to esplora
location /api/tx/ {
	rewrite ^/api/(.*) /$1 break;
	try_files /dev/null @esplora-api-cache-disabled;
}
location /api/address-prefix/ {
	rewrite ^/api/(.*) /$1 break;
	try_files /dev/null @esplora-api-cache-disabled;
}

# rewrite APIs to match what backend expects
location /api/currencies {
	rewrite ^/api/(.*) /api/v1/bisq/markets/$1 break;
	try_files /dev/null @mempool-bisq;
}
location /api/depth {
	rewrite ^/api/(.*) /api/v1/bisq/markets/$1 break;
	try_files /dev/null @mempool-bisq;
}
location /api/hloc {
	rewrite ^/api/(.*) /api/v1/bisq/markets/$1 break;
	try_files /dev/null @mempool-bisq;
}
location /api/offers {
	rewrite ^/api/(.*) /api/v1/bisq/markets/$1 break;
	try_files /dev/null @mempool-bisq;
}
location /api/ticker {
	rewrite ^/api/(.*) /api/v1/bisq/markets/$1 break;
	try_files /dev/null @mempool-bisq;
}
location /api/trades {
	rewrite ^/api/(.*) /api/v1/bisq/markets/$1 break;
	try_files /dev/null @mempool-bisq;
}
location /api/volumes {
	rewrite ^/api/(.*) /api/v1/bisq/markets/$1 break;
	try_files /dev/null @mempool-bisq;
}
location /api/markets {
	rewrite ^/api/(.*) /api/v1/bisq/markets/$1 break;
	try_files /dev/null @mempool-bisq;
}
location /api/v1 {
	rewrite ^/api/v1/(.*) /api/v1/bisq/$1 break;
	try_files /dev/null @mempool-bisq;
}
location /api {
	rewrite ^/api/(.*) /api/v1/bisq/$1 break;
	try_files /dev/null @mempool-bisq;
}
location /api/v1/ws {
	rewrite ^/api/(.*) /api/v1/bisq/$1 break;
	try_files /dev/null @mempool-bisq-websocket;
}
location /bisq/api/v1/ws {
	rewrite ^/bisq/api/v1/(.*) /api/v1/bisq/$1 break;
	try_files /dev/null @mempool-bisq-websocket;
}
location /bisq/api/v1 {
	rewrite ^/bisq/api/v1/(.*) /api/v1/bisq/$1 break;
	try_files /dev/null @mempool-bisq;
}
location /bisq/api {
	rewrite ^/bisq/api/(.*) /api/v1/bisq/$1 break;
	try_files /dev/null @mempool-bisq;
}

location @mempool-bisq-websocket {
	proxy_pass $mempoolBisq;
	proxy_http_version 1.1;
	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection "Upgrade";
}

location @mempool-bisq {
	proxy_pass $mempoolBisq;

	proxy_set_header Host $host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header X-Forwarded-Proto $scheme;

	proxy_redirect off;
}

location @esplora-api-cache-disabled {
	proxy_pass $esploraMainnet;

	proxy_set_header Host $host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header X-Forwarded-Proto $scheme;

	proxy_redirect off;
	proxy_buffering off;

	expires -1;
}
