ZhangZhihui's Blog  

 

 

3 What is a proxy server?

Proxy servers have many use cases :

  • Filter and monitor internet traffic.

    • Companies intensively use forward proxies to keep an eye on employees activity

    • They may also block some requests to forbidden websites

  • Caching

    • We can use proxies to cache resources.

    • Instead of calling the server targeted, they will send a cached response to the client.

 
The paper and the digital edition of this book are available here.
I also filmed a video course to build a real world project with Go.

4 The reasons behind the birth of the Go Module Proxy 

The Go Module Proxy is a recent addition to the language tooling. Before the go get command downloaded, the module directly from the code-sharing website. It has some drawbacks :

  • A module can be deleted from the site sharing website by the developer.

  • A version can also be deleted.

    • A tag was deleted...

    • A commit can be removed...

If your application depends on this module building your Go application might be impossible.

 

5 How does it Work 

The go get command can fetch dependencies from a module proxy server without touching the original server that hosts the code (GitHub, GitLab,...).

The Go Command (go get) try to fetch modules from the proxy serverThe Go Command (go get) try to fetch modules from the proxy server

When the code is not available on the proxy server, Go can download it directly from the hosting server.

When the module is not on the proxyWhen the module is not on the proxy

We can change this behavior through an environment variable.

 

6 Configuration of the Go Module Proxy 

The proxy configuration is set into the environment variable GOPROXY.

By typing the command

go env GOPROXY

you can check its current value. At the time of writing, the default value is :

https://proxy.golang.org,direct
  • The value of GOPROXY is a list

  • The list is separated by commas “,” or pipes “|”

  • It is composed of URLs of Go modules proxy and/or special keywords.

  • The keywords available are

    “off” : it means turn off the feature

    “direct” : it instructs the tool to download it directly from the code hosting server.

Go will attempt to download new modules from each specified Go Module Proxy URLs from left to right. When it encounters the keyword “direct” it will try to download the module directly from the source hosting website.

GOPROXY environment configurationGOPROXY environment configuration

 

6.1 Disable Go Module Proxy 

To disable the feature completely set the GOPROXY env to the value “off”

 

7 The four endpoints of a Go Module Proxy (advanced) 

A go module proxy will expose four endpoints (all GET) /<module>/@v/list : get the list of versions known by the proxy server.

  1. Ex : https://proxy.golang.org/ gitlab.com/loir402/bluesodium/@v/ list

    v1.0.0
    v1.0.1

/<module>/@latest : get the latest version info formated in JSON

  1. Ex : https://proxy.golang.org/ gitlab.com/loir402/bluesodium/@v/latest

    {"Version":"v1.0.1","Time":"2021-01-20T18:49:34Z"}

/<module>/@v/<version>.info : get the metadata about the module’s version and the time at which it was committed.

  1. Ex : https://proxy.golang.org/ gitlab.com/loir402/bluesodium/@v/v1.0.1 .info

    {"Version":"v1.0.1","Time":"2021-01-20T18:49:34Z"}

/<module>/@v/<version>.mod : return the go.mod file of the given version

  1. Ex : https://proxy.golang.org/ gitlab.com/loir402/bluesodium/@v/v1.0.1 .mod

/<module>/@v/<version>.zip : return the zipped version of the module.

  1. Ex : https://proxy.golang.org/ gitlab.com/loir402/bluesodium/@v/v1.0.1 .zip
 
The paper and the digital edition of this book are available here.
I also filmed a video course to build a real world project with Go.

8 Common error: the newest version is not downloaded 

When a newer minor or patch is available for the module, the command

go get -u modulePath

will not always download it immediately. The service has a cache in place to improve performance. Hence to solve this, you have two solutions :

  • Wait and try later (the cache will be invalidated)

  • Target specifically the version you want to use in the go get command

    go get -u modulePath@v1.0.2

9 Useful links to debug issues 

  • How to get the list of available versions for a module on a proxy?

    https://PROXY_URL/MODULE_PATH/@v/list
    • Ex : https://proxy.golang.org/gitlab.com/loir402/bluesodium/@v/list

      • Will output the list of versions available for a given module
    • For major version 2 the link is :

"https://proxy.golang.org/gitlab.com/loir402/bluesodium/v2/@v/list"
  • How to download source code of a module at a specific version

    https://PROXY_URL/MODULE_PATH/@v/VERSION.zip
    • Ex : https://proxy.golang.org/gitlab.com/loir402/bluesodium/@v/v1.0.1.zip

      • Will trigger the download of the source code

 

Copied from: https://www.practical-go-lessons.com/chap-18-go-module-proxies

posted on 2024-04-05 09:48  ZhangZhihuiAAA  阅读(6)  评论(0编辑  收藏  举报