github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/cmd/godoc/x.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This file contains the handlers that serve go-import redirects for Go
     6  // sub-repositories. It specifies the mapping from import paths like
     7  // "golang.org/x/tools" to the actual repository locations.
     8  
     9  package main
    10  
    11  import (
    12  	"html/template"
    13  	"log"
    14  	"net/http"
    15  	"strings"
    16  )
    17  
    18  const xPrefix = "/x/"
    19  
    20  type xRepo struct {
    21  	URL, VCS string
    22  }
    23  
    24  var xMap = map[string]xRepo{
    25  	"codereview": {"https://code.google.com/p/go.codereview", "hg"},
    26  
    27  	"arch":       {"https://go.googlesource.com/arch", "git"},
    28  	"benchmarks": {"https://go.googlesource.com/benchmarks", "git"},
    29  	"blog":       {"https://go.googlesource.com/blog", "git"},
    30  	"build":      {"https://go.googlesource.com/build", "git"},
    31  	"crypto":     {"https://go.googlesource.com/crypto", "git"},
    32  	"debug":      {"https://go.googlesource.com/debug", "git"},
    33  	"exp":        {"https://go.googlesource.com/exp", "git"},
    34  	"image":      {"https://go.googlesource.com/image", "git"},
    35  	"mobile":     {"https://go.googlesource.com/mobile", "git"},
    36  	"net":        {"https://go.googlesource.com/net", "git"},
    37  	"oauth2":     {"https://go.googlesource.com/oauth2", "git"},
    38  	"playground": {"https://go.googlesource.com/playground", "git"},
    39  	"review":     {"https://go.googlesource.com/review", "git"},
    40  	"sync":       {"https://go.googlesource.com/sync", "git"},
    41  	"sys":        {"https://go.googlesource.com/sys", "git"},
    42  	"talks":      {"https://go.googlesource.com/talks", "git"},
    43  	"term":       {"https://go.googlesource.com/term", "git"},
    44  	"text":       {"https://go.googlesource.com/text", "git"},
    45  	"time":       {"https://go.googlesource.com/time", "git"},
    46  	"tools":      {"https://go.googlesource.com/tools", "git"},
    47  	"tour":       {"https://go.googlesource.com/tour", "git"},
    48  }
    49  
    50  func init() {
    51  	http.HandleFunc(xPrefix, xHandler)
    52  }
    53  
    54  func xHandler(w http.ResponseWriter, r *http.Request) {
    55  	head, tail := strings.TrimPrefix(r.URL.Path, xPrefix), ""
    56  	if i := strings.Index(head, "/"); i != -1 {
    57  		head, tail = head[:i], head[i:]
    58  	}
    59  	if head == "" {
    60  		http.Redirect(w, r, "https://godoc.org/-/subrepo", http.StatusTemporaryRedirect)
    61  		return
    62  	}
    63  	repo, ok := xMap[head]
    64  	if !ok {
    65  		http.NotFound(w, r)
    66  		return
    67  	}
    68  	data := struct {
    69  		Prefix, Head, Tail string
    70  		Repo               xRepo
    71  	}{xPrefix, head, tail, repo}
    72  	if err := xTemplate.Execute(w, data); err != nil {
    73  		log.Println("xHandler:", err)
    74  	}
    75  }
    76  
    77  var xTemplate = template.Must(template.New("x").Parse(`<!DOCTYPE html>
    78  <html>
    79  <head>
    80  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    81  <meta name="go-import" content="golang.org{{.Prefix}}{{.Head}} {{.Repo.VCS}} {{.Repo.URL}}">
    82  <meta name="go-source" content="golang.org{{.Prefix}}{{.Head}} https://github.com/golang/{{.Head}}/ https://github.com/golang/{{.Head}}/tree/master{/dir} https://github.com/golang/{{.Head}}/blob/master{/dir}/{file}#L{line}">
    83  <meta http-equiv="refresh" content="0; url=https://godoc.org/golang.org{{.Prefix}}{{.Head}}{{.Tail}}">
    84  </head>
    85  <body>
    86  Nothing to see here; <a href="https://godoc.org/golang.org{{.Prefix}}{{.Head}}{{.Tail}}">move along</a>.
    87  </body>
    88  </html>
    89  `))