# nginx config for go-micro.dev
#
# Serves the docs site (proxied to micro.github.io/go-micro) and the Go vanity
# import meta tags for `go get` / `go install`.
#
# IMPORTANT — the vanity import-prefix must be the MODULE ROOT, i.e.
# go-micro.dev/vN, never a deeper package path. The old catch-all echoed the
# full request path ($host$1), so `go install go-micro.dev/v6/cmd/micro@latest`
# received the prefix "go-micro.dev/v6/cmd/micro". Go then treats the package
# path as the module root, finds no module that declares it, and falls back to
# the latest non-versioned tag (the ancient v1.x), producing:
#
#   version constraints conflict: ... module declares its path as
#   github.com/micro/go-micro but was required as go-micro.dev/v6/cmd/micro
#
# The dedicated /vN location below truncates the path to its major-version
# segment and emits "go-micro.dev/vN", so any sub-package (e.g. cmd/micro)
# resolves at @latest.

server {
    server_name go-micro.dev;

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/go-micro.dev/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/go-micro.dev/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    root /var/www/go-micro;

    # Plugin submodules: go-micro.dev/plugins/<group>/<vN...>
    location ~ ^/plugins/(.*)/(v[0-9].*) {
        if ($args = "go-get=1") {
            add_header Content-Type text/html;
            return 200 '<meta name="go-import" content="$host/plugins/$1/$2 git https://github.com/micro/go-micro.git">';
        }
    }

    # Versioned module paths: go-micro.dev/vN and everything under it.
    # Emit the module root (go-micro.dev/$1, e.g. go-micro.dev/v6) as the
    # import-prefix — NOT the echoed request path — so that
    # `go install go-micro.dev/vN/cmd/micro@latest` resolves correctly.
    # Also emit go-source so pkg.go.dev links to the right files/lines.
    # This must appear before the catch-all so it wins for /vN paths.
    location ~ ^/(v[0-9]+)(/.*)?$ {
        if ($args = "go-get=1") {
            add_header Content-Type text/html;
            return 200 '<meta name="go-import" content="$host/$1 git https://github.com/micro/go-micro.git"><meta name="go-source" content="$host/$1 https://github.com/micro/go-micro https://github.com/micro/go-micro/tree/master{/dir} https://github.com/micro/go-micro/blob/master{/dir}/{file}#L{line}">';
        }
        proxy_pass              https://micro.github.io/go-micro$uri;
        proxy_set_header Host micro.github.io;
        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_set_header Accept-Encoding "";
        proxy_buffering off;
        proxy_redirect off;
        resolver 127.0.0.53;
    }

    location = /docs {
        return 302 /docs/;
    }

    location / {
        proxy_pass              https://micro.github.io/go-micro/;
        proxy_set_header Host micro.github.io;
        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_set_header Accept-Encoding "";
        proxy_buffering off;
        proxy_redirect off;
        resolver 127.0.0.53;
    }

    # Catch-all. For any other go-get request, advertise the current module
    # roots (Go picks the meta whose prefix matches the requested path);
    # otherwise proxy to the docs site.
    location ~ ^(/?.*) {
        if ($args = "go-get=1") {
            add_header Content-Type text/html;
            return 200 '<meta name="go-import" content="$host/v6 git https://github.com/micro/go-micro.git"><meta name="go-import" content="$host/v5 git https://github.com/micro/go-micro.git">';
        }
        proxy_pass              https://micro.github.io/go-micro/$1;
        proxy_set_header Host micro.github.io;
        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_set_header Accept-Encoding "";
        proxy_buffering off;
        proxy_redirect off;
        resolver 127.0.0.53;
    }
}

server {
    if ($host = go-micro.dev) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    listen [::]:80;
    server_name go-micro.dev;
    return 404; # managed by Certbot
}
