-
Notifications
You must be signed in to change notification settings - Fork 6
/
controller.go
54 lines (45 loc) · 1.16 KB
/
controller.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
package controllers
import (
"fmt"
"html/template"
"net/http"
"os"
"path/filepath"
"github.com/servntire/servntire-demo/blockchain"
)
type Application struct {
Fabric *blockchain.FabricSetup
}
func add(x, y int) int {
return x + y
}
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
}
funcs := template.FuncMap{"add": add}
resultTemplate := template.Must(template.New(templateName).Funcs(funcs).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)
}
}