package actions import ( "errors" "html/template" "net/http" "os" "strings" "github.com/gomods/athens/pkg/config" "github.com/gomods/athens/pkg/log" ) const homepage = `
GOPROXY={{ .Host }},direct
{{ if .NoSumPatterns }}
Use the following GONOSUMDB environment variable to exclude checksum database:
GONOSUMDB={{ .NoSumPatterns }}
{{ end }}
Use the catalog endpoint to get a list of all modules in the proxy
This endpoint returns a list of versions that Athens knows about for acidburn/htp:
GET {{ .Host }}/github.com/acidburn/htp/@v/list
This endpoint returns information about a specific version of a module:
GET {{ .Host }}/github.com/acidburn/htp/@v/v1.0.0.info
This returns JSON with information about v1.0.0. It looks like this:
{
"Name": "v1.0.0",
"Short": "v1.0.0",
"Version": "v1.0.0",
"Time": "1972-07-18T12:34:56Z"
}
This endpoint returns the go.mod file for a specific version of a module:
GET {{ .Host }}/github.com/acidburn/htp/@v/v1.0.0.mod
This returns the go.mod file for version v1.0.0. If {{ .Host }}/github.com/acidburn/htp version v1.0.0 has no dependencies, the response body would look like this:
module github.com/acidburn/htp
GET {{ .Host }}/github.com/acidburn/htp/@v/v1.0.0.zip
This is what it sounds like — it sends back a zip file with the source code for the module in version v1.0.0.
GET {{ .Host }}/github.com/acidburn/htp/@latest
This endpoint returns the latest version of the module. If the version does not exist it should retrieve the hash of latest commit.
` func proxyHomeHandler(c *config.Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { lggr := log.EntryFromContext(r.Context()) templateData := make(map[string]string) templateContents := homepage // load the template from the file system if it exists, otherwise revert to default rawTemplateFileContents, err := os.ReadFile(c.HomeTemplatePath) if err != nil { if !errors.Is(err, os.ErrNotExist) { // this is some other error, log it and revert to default lggr.SystemErr(err) } } else { templateContents = string(rawTemplateFileContents) } // This should be correct in most cases. If it is not, users can supply their own template templateData["Host"] = r.Host // use host from URL, if it exists if r.URL.Host != "" { templateData["Host"] = r.URL.Host } // if the host does not have a scheme, add one based on the request if !strings.HasPrefix(templateData["Host"], "http://") && !strings.HasPrefix(templateData["Host"], "https://") { if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" { templateData["Host"] = "https://" + templateData["Host"] } else { templateData["Host"] = "http://" + templateData["Host"] } } templateData["NoSumPatterns"] = strings.Join(c.NoSumPatterns, ",") tmp, err := template.New("home").Parse(templateContents) if err != nil { lggr.SystemErr(err) w.WriteHeader(http.StatusInternalServerError) } w.Header().Set("Content-Type", "text/html") w.WriteHeader(http.StatusOK) err = tmp.ExecuteTemplate(w, "home", templateData) if err != nil { lggr.SystemErr(err) w.WriteHeader(http.StatusInternalServerError) } } }