|
| 1 | +// Copyright 2023 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 | +// Outyet is a web server that announces whether or not a particular Go version |
| 6 | +// has been tagged. |
| 7 | +package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "expvar" |
| 11 | + "flag" |
| 12 | + "fmt" |
| 13 | + "html/template" |
| 14 | + "log" |
| 15 | + "net/http" |
| 16 | + "strings" |
| 17 | + "sync" |
| 18 | + "time" |
| 19 | +) |
| 20 | + |
| 21 | +// Command-line flags. |
| 22 | +var ( |
| 23 | + httpAddr = flag.String("addr", "0.0.0.0:8080", "Listen address") |
| 24 | + pollPeriod = flag.Duration("poll", 5*time.Second, "Poll period") |
| 25 | + version = flag.String("version", "1.20", "Go version") |
| 26 | +) |
| 27 | + |
| 28 | +const baseChangeURL = "https://go.googlesource.com/go/+/" |
| 29 | + |
| 30 | +func main() { |
| 31 | + flag.Parse() |
| 32 | + changeURL := fmt.Sprintf("%sgo%s", baseChangeURL, *version) |
| 33 | + |
| 34 | + // Serve the images directory just like Firebase Hosting would |
| 35 | + fs := http.FileServer(http.Dir("public/images")) |
| 36 | + http.Handle("/images/", http.StripPrefix("/images/", fs)) |
| 37 | + |
| 38 | + // Serve the docs directory |
| 39 | + docsFs := http.FileServer(http.Dir("public/docs")) |
| 40 | + http.Handle("/docs/", http.StripPrefix("/docs/", docsFs)) |
| 41 | + |
| 42 | + // Serve the manual HTML viewer |
| 43 | + http.HandleFunc("/manual.html", func(w http.ResponseWriter, r *http.Request) { |
| 44 | + http.ServeFile(w, r, "public/manual.html") |
| 45 | + }) |
| 46 | + |
| 47 | + http.Handle("/", NewServer(*version, changeURL, *pollPeriod)) |
| 48 | + |
| 49 | + addr := *httpAddr |
| 50 | + port := addr |
| 51 | + if idx := strings.LastIndex(addr, ":"); idx != -1 { |
| 52 | + port = addr[idx:] |
| 53 | + } |
| 54 | + |
| 55 | + log.Printf("serving http://%s", addr) |
| 56 | + log.Printf("local access: http://localhost%s", port) |
| 57 | + log.Fatal(http.ListenAndServe(addr, nil)) |
| 58 | +} |
| 59 | + |
| 60 | +// Exported variables for monitoring the server. |
| 61 | +// These are exported via HTTP as a JSON object at /debug/vars. |
| 62 | +var ( |
| 63 | + hitCount = expvar.NewInt("hitCount") |
| 64 | + pollCount = expvar.NewInt("pollCount") |
| 65 | + pollError = expvar.NewString("pollError") |
| 66 | + pollErrorCount = expvar.NewInt("pollErrorCount") |
| 67 | +) |
| 68 | + |
| 69 | +// Server implements the outyet server. |
| 70 | +// It serves the user interface (it's an http.Handler) |
| 71 | +// and polls the remote repository for changes. |
| 72 | +type Server struct { |
| 73 | + version string |
| 74 | + url string |
| 75 | + period time.Duration |
| 76 | + |
| 77 | + mu sync.RWMutex // protects the yes variable |
| 78 | + yes bool |
| 79 | +} |
| 80 | + |
| 81 | +// NewServer returns an initialized outyet server. |
| 82 | +func NewServer(version, url string, period time.Duration) *Server { |
| 83 | + s := &Server{version: version, url: url, period: period} |
| 84 | + go s.poll() |
| 85 | + return s |
| 86 | +} |
| 87 | + |
| 88 | +// poll polls the change URL for the specified period until the tag exists. |
| 89 | +// Then it sets the Server's yes field true and exits. |
| 90 | +func (s *Server) poll() { |
| 91 | + for !isTagged(s.url) { |
| 92 | + pollSleep(s.period) |
| 93 | + } |
| 94 | + s.mu.Lock() |
| 95 | + s.yes = true |
| 96 | + s.mu.Unlock() |
| 97 | + pollDone() |
| 98 | +} |
| 99 | + |
| 100 | +// Hooks that may be overridden for integration tests. |
| 101 | +var ( |
| 102 | + pollSleep = time.Sleep |
| 103 | + pollDone = func() {} |
| 104 | +) |
| 105 | + |
| 106 | +// isTagged makes an HTTP HEAD request to the given URL and reports whether it |
| 107 | +// returned a 200 OK response. |
| 108 | +func isTagged(url string) bool { |
| 109 | + pollCount.Add(1) |
| 110 | + r, err := http.Head(url) |
| 111 | + if err != nil { |
| 112 | + log.Print(err) |
| 113 | + pollError.Set(err.Error()) |
| 114 | + pollErrorCount.Add(1) |
| 115 | + return false |
| 116 | + } |
| 117 | + return r.StatusCode == http.StatusOK |
| 118 | +} |
| 119 | + |
| 120 | +// ServeHTTP implements the HTTP user interface. |
| 121 | +func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 122 | + hitCount.Add(1) |
| 123 | + s.mu.RLock() |
| 124 | + data := struct { |
| 125 | + URL string |
| 126 | + Version string |
| 127 | + Yes bool |
| 128 | + }{ |
| 129 | + s.url, |
| 130 | + s.version, |
| 131 | + s.yes, |
| 132 | + } |
| 133 | + s.mu.RUnlock() |
| 134 | + err := tmpl.Execute(w, data) |
| 135 | + if err != nil { |
| 136 | + log.Print(err) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// tmpl is the HTML template that drives the user interface. |
| 141 | +var tmpl = template.Must(template.ParseFiles("public/index.html")) |
0 commit comments