-
Notifications
You must be signed in to change notification settings - Fork 3
/
templates.go
127 lines (113 loc) · 3.33 KB
/
templates.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"time"
humanize "github.com/dustin/go-humanize"
"github.com/Mayo-QIN/grunt/dassets"
"github.com/gorilla/mux"
"github.com/imdario/mergo"
"github.com/russross/blackfriday"
)
// Add some helper functions
var funcs = template.FuncMap{
"json": func(v interface{}) (string, error) {
a, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(a), nil
},
"humanizeTime": humanize.Time,
"now": func() string { return time.Now().Format(time.RFC1123) },
"isArray": func(s string) bool { return strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") },
"toArray": func(s string) []string {
v := strings.TrimPrefix(s, "[")
v = strings.TrimSuffix(v, "]")
return strings.Split(v, ",")
},
"markdown": func(s string) template.HTML {
return template.HTML(string(blackfriday.MarkdownCommon([]byte(s))))
},
}
var templates = template.New("").Funcs(funcs)
var helpText = template.HTML("")
func init() {
loadTemplates(AssetNames(), Asset)
}
func loadTemplates(names []string, asset func(name string) ([]byte, error)) {
for _, path := range names {
bytes, err := asset(path)
if err != nil {
if debug {
log.Printf("Unable to parse: path=%s, err=%s", path, err)
} else {
log.Panicf("Unable to parse: path=%s, err=%s", path, err)
}
}
templates.New(path).Parse(string(bytes))
}
bytes, _ := asset("README.md/README.md")
helpText = template.HTML(string(blackfriday.MarkdownCommon(bytes)))
}
func Template(name string, data map[string]interface{}, w http.ResponseWriter, request *http.Request) {
templateData := map[string]interface{}{
"jobs": jobs,
"config": config,
"services": config.Services,
"serviceMap": config.ServiceMap,
"help": helpText,
"vars": mux.Vars(request),
"version": Version,
"fullVersion": FullVersion,
"buildTimestamp": BuildTimestamp,
"hash": Hash,
}
// merge in our extra data
mergo.Map(&templateData, data)
if debug {
templates = template.New("").Funcs(funcs)
loadTemplates(dassets.AssetNames(), dassets.Asset)
}
if templates.Lookup("template/"+name) == nil {
http.Error(w, name+" is not found", http.StatusNotFound)
return
}
err := templates.ExecuteTemplate(w, "template/"+name, templateData)
if err != nil {
log.Printf("error in template %v", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func ExecTemplate(w http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
name := vars["name"]
Template(name, nil, w, request)
}
// Return the information about a job as JSON
func JobDetail(w http.ResponseWriter, request *http.Request) {
key := mux.Vars(request)["id"]
job := jobs[key]
if job == nil {
http.Error(w, "could not find job", http.StatusNotFound)
return
}
var data = map[string]interface{}{"job": job}
Template("job.html", data, w, request)
}
// Return the information about a job as JSON
func ServiceDetail(w http.ResponseWriter, request *http.Request) {
key := mux.Vars(request)["id"]
service, ok := config.ServiceMap[key]
if !ok {
http.Error(w, fmt.Sprintf("could not find service %v", key), http.StatusNotFound)
return
}
var data = map[string]interface{}{"service": service}
Template("service.html", data, w, request)
}