-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
61 lines (53 loc) · 1.46 KB
/
util.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
package main
import (
"fmt"
"html/template"
"log"
"math/rand"
"net/http"
)
// Helper Functions
// version
func version() string {
return "0.0.1"
}
// Convenience function for printing to stdout
func p(a ...interface{}) {
fmt.Println(a...)
}
// create a random UUID with from RFC 4122
// adapted from http://github.com/nu7hatch/gouuid
func createUUID() (uuid string) {
u := new([16]byte)
_, err := rand.Read(u[:])
if err != nil {
log.Fatalln("Cannot generate UUID", err)
}
// 0x40 is reserved variant from RFC 4122
u[8] = (u[8] | 0x40) & 0x7F
// Set the four most significant bits (bits 12 through 15) of the
// time_hi_and_version field to the 4-bit version number.
u[6] = (u[6] & 0xF) | (0x4 << 4)
uuid = fmt.Sprintf("%x-%x-%x-%x-%x", u[0:4], u[4:6], u[6:8], u[8:10], u[10:])
return
}
// parse HTML templates
// pass in a list of file names, and get a template
func parseTemplateFiles(filenames ...string) (t *template.Template) {
var files []string
t = template.New("template")
for _, file := range filenames {
files = append(files, fmt.Sprintf("templates/%s.html", file))
}
t = template.Must(t.ParseFiles(files...))
return
}
// generateHTML returns the template.
func generateHTML(w http.ResponseWriter, data interface{}, fn ...string) {
var files []string
for _, file := range fn {
files = append(files, fmt.Sprintf("templates/%s.html", file))
}
templates := template.Must(template.ParseFiles(files...))
templates.ExecuteTemplate(w, "home", data)
}