upstream _ipfs_gateway {
  server   localhost:<%= @ipfs_gateway_port %>;
}
upstream _ipfs_api {
  server   localhost:<%= @ipfs_api_port %>;
}

server {
<% if File.exist?(@ssl_cert) && File.exist?(@ssl_key) -%>
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
<% else -%>
  listen 80;
  listen [::]:80;
<% end -%>
  server_name ipfs.kosmos.org;

  location /ipfs {
    proxy_pass http://_ipfs_gateway/ipfs;
  }

  ssl_certificate /etc/letsencrypt/live/ipfs.kosmos.org/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/ipfs.kosmos.org/privkey.pem;
}

server {
<% if File.exist?(@ssl_cert) && File.exist?(@ssl_key) -%>
  listen <%= @ipfs_external_api_port %> ssl http2;
<% else -%>
  listen <%= @ipfs_external_api_port %>;
<% end -%>

  server_name <%= @server_name %>;

  location / {
    return 200 'Nothing to see here';
    add_header Content-Type text/plain;
  }

  # Increase number of buffers. Default is 8
  proxy_buffers 1024 8k;
  proxy_http_version 1.1;

  location /api/v0/cat {
    proxy_pass http://_ipfs_api/api/v0/cat;
  }
  location /api/v0/add {
    proxy_pass http://_ipfs_api/api/v0/add;
  }
  location /api/v0/object/get {
    proxy_pass http://_ipfs_api/api/v0/object/get;
  }
  location /api/v0/object/data {
    proxy_pass http://_ipfs_api/api/v0/object/data;
  }
  location /api/v0/id {
    proxy_pass http://_ipfs_api/api/v0/id;
  }

  ssl_certificate <%= @ssl_cert %>;
  ssl_certificate_key <%= @ssl_key %>;
}
