/
html_controllers.go
63 lines (51 loc) · 1.21 KB
/
html_controllers.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
package html
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"html/template"
"net/http"
"os"
"path/filepath"
"multiorgledger/blockchain/org"
)
var PORT = "6000"
var customOrgNames map[string]string
type HtmlApp struct {
Org *org.OrgSetup
}
func hash(s string) string {
h := sha1.New()
h.Write([]byte(s))
sha1_hash := hex.EncodeToString(h.Sum(nil))
return sha1_hash
}
func renderTemplate(w http.ResponseWriter, r *http.Request, templateName string, data interface{}) {
lp := filepath.Join("web", "templates", "layout.html")
tp := filepath.Join("web", "templates", templateName)
// Return a 404 if the template doesn't exist
info, err := os.Stat(tp)
if err != nil {
if os.IsNotExist(err) {
http.NotFound(w, r)
return
}
}
// Return a 404 if the request is for a directory
if info.IsDir() {
http.NotFound(w, r)
return
}
resultTemplate, err := template.ParseFiles(tp, lp)
if err != nil {
// Log the detailed error
fmt.Println(err.Error())
// Return a generic "Internal Server Error" message
http.Error(w, http.StatusText(500), 500)
return
}
if err := resultTemplate.ExecuteTemplate(w, "layout", data); err != nil {
fmt.Println(err.Error())
http.Error(w, http.StatusText(500), 500)
}
}