-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
executable file
·82 lines (75 loc) · 1.4 KB
/
writer.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
package main
import (
"encoding/json"
"io"
"log"
"text/template"
)
type HtmlArgs struct {
Title string
Vars map[string]interface{}
Js string
ApiKey string
}
func writeHtml(vars *map[string]interface{}, js string, title string, apiKey string, writer io.Writer) {
t, err := template.New("html").Funcs(template.FuncMap{
"json": func(x interface{}) string {
jb, err := json.Marshal(x)
if err != nil {
log.Fatal(err)
}
return string(jb)
},
}).Parse(outHtmlTemplate)
if err != nil {
log.Fatal(err)
}
err = t.Execute(writer, HtmlArgs{
Title: title,
Vars: *vars,
Js: js,
ApiKey: apiKey,
})
if err != nil {
log.Fatal(err)
}
}
var outHtmlTemplate = `
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>{{.Title}}</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
{{range $k, $v := .Vars}}
const {{$k}} = {{json $v}};
{{end}}
// Create the map.
const map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: d.center,
mapTypeId: google.maps.MapTypeId.TERRAIN
});
{{.Js}}
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key={{.ApiKey}}&callback=initMap"></script>
</body>
</html>
`