github.com/huandu/go@v0.0.0-20151114150818-04e615e41150/doc/progs/eff_qr.go (about)

     1  // Copyright 2009 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  package main
     6  
     7  import (
     8  	"flag"
     9  	"html/template"
    10  	"log"
    11  	"net/http"
    12  )
    13  
    14  var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
    15  
    16  var templ = template.Must(template.New("qr").Parse(templateStr))
    17  
    18  func main() {
    19  	flag.Parse()
    20  	http.Handle("/", http.HandlerFunc(QR))
    21  	err := http.ListenAndServe(*addr, nil)
    22  	if err != nil {
    23  		log.Fatal("ListenAndServe:", err)
    24  	}
    25  }
    26  
    27  func QR(w http.ResponseWriter, req *http.Request) {
    28  	templ.Execute(w, req.FormValue("s"))
    29  }
    30  
    31  const templateStr = `
    32  <html>
    33  <head>
    34  <title>QR Link Generator</title>
    35  </head>
    36  <body>
    37  {{if .}}
    38  <img src="http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl={{.}}" />
    39  <br>
    40  {{.}}
    41  <br>
    42  <br>
    43  {{end}}
    44  <form action="/" name=f method="GET"><input maxLength=1024 size=70
    45  name=s value="" title="Text to QR Encode"><input type=submit
    46  value="Show QR" name=qr>
    47  </form>
    48  </body>
    49  </html>
    50  `