github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume2/section3/gopherfaceform/handlers/utility.go (about) 1 package handlers 2 3 import ( 4 "fmt" 5 "html/template" 6 "log" 7 "net/http" 8 "os" 9 ttemplate "text/template" 10 ) 11 12 // Template rendering function 13 func RenderTemplate(w http.ResponseWriter, templateFile string, templateData interface{}) { 14 t, err := template.ParseFiles(templateFile) 15 if err != nil { 16 log.Printf("Error encountered while parsing the template: ", err) 17 } 18 t.Execute(w, templateData) 19 } 20 21 func RenderUnsafeTemplate(w http.ResponseWriter, templateFile string, templateData interface{}) { 22 t, err := ttemplate.ParseFiles(templateFile) 23 if err != nil { 24 log.Printf("Error encountered while parsing the template: ", err) 25 } 26 w.Header().Set("X-XSS-Protection", "0") 27 t.Execute(w, templateData) 28 } 29 30 func GenerateUUID() string { 31 f, err := os.Open("/dev/urandom") 32 if err != nil { 33 log.Println("Encountered the following error when attempting to generate an UUID: ", err) 34 return "" 35 } 36 b := make([]byte, 16) 37 f.Read(b) 38 f.Close() 39 uuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]) 40 return uuid 41 }