You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.2 KiB

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