github.com/hongwozai/go-src-1.4.3@v0.0.0-20191127132709-dc3fce3dbccb/doc/progs/eff_qr.go (about)

     1  // compile
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package main
     8  
     9  import (
    10  	"flag"
    11  	"html/template"
    12  	"log"
    13  	"net/http"
    14  )
    15  
    16  var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
    17  
    18  var templ = template.Must(template.New("qr").Parse(templateStr))
    19  
    20  func main() {
    21  	flag.Parse()
    22  	http.Handle("/", http.HandlerFunc(QR))
    23  	err := http.ListenAndServe(*addr, nil)
    24  	if err != nil {
    25  		log.Fatal("ListenAndServe:", err)
    26  	}
    27  }
    28  
    29  func QR(w http.ResponseWriter, req *http.Request) {
    30  	templ.Execute(w, req.FormValue("s"))
    31  }
    32  
    33  const templateStr = `
    34  <html>
    35  <head>
    36  <title>QR Link Generator</title>
    37  </head>
    38  <body>
    39  {{if .}}
    40  <img src="http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl={{.}}" />
    41  <br>
    42  {{.}}
    43  <br>
    44  <br>
    45  {{end}}
    46  <form action="/" name=f method="GET"><input maxLength=1024 size=70
    47  name=s value="" title="Text to QR Encode"><input type=submit
    48  value="Show QR" name=qr>
    49  </form>
    50  </body>
    51  </html>
    52  `