-
Notifications
You must be signed in to change notification settings - Fork 126
/
web.go
99 lines (89 loc) · 2.4 KB
/
web.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//go:generate go-bindata-assetfs page/...
package main
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"os"
"os/exec"
"runtime"
)
type PageInfo struct {
Title string
}
// indexTmpl is a html template for index page.
var (
devMode bool
indexTmpl *template.Template
)
func init() {
devMode = os.Getenv("GOTRACE_DEVMODE") == "1"
if devMode {
indexTmpl = template.Must(template.New("index.html").Parse("page/index.html"))
} else {
data, err := Asset("page/index.html")
if err != nil {
panic(err)
}
indexTmpl = template.Must(template.New("index.html").Parse(string(data)))
}
}
// StartServer generates webpage, serves it via http
// and tries to open it using default browser.
func StartServer(bind string, data []byte, params *Params) error {
// Serve data as data.js
http.HandleFunc("/data.js", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("var data = "))
w.Write(data)
}))
// Serve params as params.js
http.HandleFunc("/params.js", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("var params = "))
data, err := json.MarshalIndent(params, "", " ")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(os.Stderr, "[ERROR] failed to render params:", err)
return
}
w.Write(data)
}))
// Handle static files
if devMode {
var fs http.FileSystem
fs = http.Dir("page")
http.Handle("/", http.FileServer(fs))
} else {
http.Handle("/", http.FileServer(assetFS()))
}
go StartBrowser("http://localhost" + bind)
return http.ListenAndServe(bind, nil)
}
// handler handles index page.
func handler(w http.ResponseWriter, r *http.Request, info *PageInfo) {
err := indexTmpl.Execute(w, info)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(os.Stderr, "[ERROR] failed to render template:", err)
return
}
}
// StartBrowser tries to open the URL in a browser
// and reports whether it succeeds.
//
// Orig. code: golang.org/x/tools/cmd/cover/html.go
func StartBrowser(url string) bool {
// try to start the browser
var args []string
switch runtime.GOOS {
case "darwin":
args = []string{"open"}
case "windows":
args = []string{"cmd", "/c", "start"}
default:
args = []string{"xdg-open"}
}
cmd := exec.Command(args[0], append(args[1:], url)...)
fmt.Println("If browser window didn't appear, please go to this url:", url)
return cmd.Start() == nil
}