mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-26 03:25:35 +00:00
cmd/hello: in dev mode, live reload template
This commit is contained in:
parent
11bbfbd8bb
commit
de3001bc79
@ -29,6 +29,9 @@
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
if !devMode() {
|
||||||
|
tmpl = template.Must(template.New("home").Parse(slurpHTML()))
|
||||||
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", root)
|
http.HandleFunc("/", root)
|
||||||
log.Printf("Starting hello server.")
|
log.Printf("Starting hello server.")
|
||||||
@ -61,7 +64,16 @@ func slurpHTML() string {
|
|||||||
return string(slurp)
|
return string(slurp)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tmpl = template.Must(template.New("home").Parse(slurpHTML()))
|
func devMode() bool { return *httpsAddr == "" && *httpAddr != "" }
|
||||||
|
|
||||||
|
func getTmpl() (*template.Template, error) {
|
||||||
|
if devMode() {
|
||||||
|
return template.New("home").Parse(slurpHTML())
|
||||||
|
}
|
||||||
|
return tmpl, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var tmpl *template.Template // not used in dev mode, initialized by main after flag parse
|
||||||
|
|
||||||
type tmplData struct {
|
type tmplData struct {
|
||||||
DisplayName string // "Foo Barberson"
|
DisplayName string // "Foo Barberson"
|
||||||
@ -88,6 +100,13 @@ func root(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "no remote addr", 500)
|
http.Error(w, "no remote addr", 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
tmpl, err := getTmpl()
|
||||||
|
if err != nil {
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
http.Error(w, "template error: "+err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
who, err := whoIs(ip)
|
who, err := whoIs(ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("whois(%q) error: %v", ip, err)
|
log.Printf("whois(%q) error: %v", ip, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user