Fragment Description:



'outyet' is a web server that announces whether or not a particular Go version has been tagged.
To be noted:
An object, an http Server, (a struct and its methods, a constructor).
Exported variables to monitor the Server, using expvar.
A template.cached (var tmpl) with data passed to it.
Use of sync.RWMutex to protect updates.
By the golang authors.

httpOutYet

Last update, on 2015, Fri 9 Oct, 16:15:38

/* ... <== see fragment description ... */

package main

import (
    "expvar"
    "flag"
    "fmt"
    "html/template"
    "log"
    "net/http"
    "sync"
    "time"
)

// Command-line flags.
var (
    httpAddr   = flag.String("http", ":9292", "Listen address")
    pollPeriod = flag.Duration("poll", 5*time.Second, "Poll period")
    version    = flag.String("version", "1.5", "Go version")
)

const baseChangeURL = "https://code.google.com/p/go/source/detail?r="

// Exported variables for monitoring the server.
// These are exported via HTTP as a JSON object at /debug/vars.
var (
    hitCount       = expvar.NewInt("hitCount")
    pollCount      = expvar.NewInt("pollCount")
    pollError      = expvar.NewString("pollError")
    pollErrorCount = expvar.NewInt("pollErrorCount")
)

// Hooks that may be overridden for integration tests.
var (
    pollSleep = time.Sleep
    pollDone  = func() {}
)

func main() {
    flag.Parse()
    changeURL := fmt.Sprintf("%sgo%s", baseChangeURL, *version)
    http.Handle("/", NewServer(*version, changeURL, *pollPeriod))
    log.Fatal(http.ListenAndServe(*httpAddr, nil))
}

// Server implements the outyet server.
// It serves the user interface (it's an http.Handler)
// and polls the remote repository for changes.
type Server struct {
    Version string
    URL     string
    period  time.Duration
    mu      sync.RWMutex // protects the yes variable
    Yes     bool
}

// NewServer returns an initialized outyet server.
func NewServer(version string, url string, period time.Duration) *Server {
    s := &Server{
        Version: version,
        URL:     url,
        period:  period,
    }
    go s.poll()
    return s
}

// poll polls the change URL for the specified period until the tag exists.
// Then it sets the Server's yes field true and exits.
func (s *Server) poll() {
    for !isTagged(s.URL) {
        pollSleep(s.period)
    }
    s.mu.Lock()
    s.Yes = true
    s.mu.Unlock()
    pollDone()
}

// ServeHTTP implements the HTTP user interface.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    hitCount.Add(1)
    s.mu.RLock()
    data := struct {
        URL     string
        Version string
        Yes     bool
    }{
        s.URL,
        s.Version,
        s.Yes,
    }
    s.mu.RUnlock()
    err := tmpl.Execute(w, data)
    if err != nil {
        log.Print(err)
    }
}

// isTagged makes an HTTP HEAD request to the given URL and reports whether
// it
// returned a 200 OK response.
func isTagged(url string) bool {
    pollCount.Add(1)
    r, err := http.Head(url)
    if err != nil {
        log.Print(err)
        pollError.Set(err.Error())
        pollErrorCount.Add(1)
        return false
    }
    return r.StatusCode == http.StatusOK
}

// tmpl is the HTML template that drives the user interface.
var tmpl = template.Must(template.New("tmpl").Parse(`
<!DOCTYPE html><html><body><center>
<h2>Is Go {{.Version}} out yet?</h2>
<h1>
{{if .Yes}}
<a href="{{.URL}}">YES!</a>
{{else}}
No. :-(
{{end}}
</h1>
</center></body></html>
`))

/*
Copyright 2014 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/



Comments