package context import ( "net/http" "path" "strings" "github.com/unknwon/com" "gopkg.in/macaron.v1" "gogs.io/gogs/internal/conf" "gogs.io/gogs/internal/db" ) // ServeGoGet does quick responses for appropriate go-get meta with status OK // regardless of whether the user has access to the repository, or the repository // does exist at all. This is particular a workaround for "go get" command which // does not respect .netrc file. func ServeGoGet() macaron.Handler { return func(c *macaron.Context) { if c.Query("go-get") != "1" { return } ownerName := c.Params(":username") repoName := c.Params(":reponame") branchName := "master" owner, err := db.Users.GetByUsername(ownerName) if err == nil { repo, err := db.Repos.GetByName(owner.ID, repoName) if err == nil && repo.DefaultBranch != "" { branchName = repo.DefaultBranch } } prefix := conf.Server.ExternalURL + path.Join(ownerName, repoName, "src", branchName) insecureFlag := "" if !strings.HasPrefix(conf.Server.ExternalURL, "https://") { insecureFlag = "--insecure " } c.PlainText(http.StatusOK, []byte(com.Expand(` go get {InsecureFlag}{GoGetImport} `, map[string]string{ "GoGetImport": path.Join(conf.Server.URL.Host, conf.Server.Subpath, ownerName, repoName), "CloneLink": db.ComposeHTTPSCloneURL(ownerName, repoName), "GoDocDirectory": prefix + "{/dir}", "GoDocFile": prefix + "{/dir}/{file}#L{line}", "InsecureFlag": insecureFlag, }, ))) } }