This repository has been archived by the owner on Oct 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
handlers.go
162 lines (132 loc) · 3.92 KB
/
handlers.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package handlers
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/haisum/recaptcha"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/html"
"github.com/oxtoacart/bpool"
)
type messageModel struct {
Title string
Message string
}
var templates map[string]*template.Template
var reCaptchaSiteKey = os.Getenv("RECAPTCHA_SITE_KEY")
var reCaptcha = recaptcha.R{
Secret: os.Getenv("RECAPTCHA_SECRET_KEY"),
}
var bufpool *bpool.BufferPool
var m = minify.New()
func init() {
bufpool = bpool.NewBufferPool(64)
}
func compileTemplates(templatePaths ...string) (*template.Template, error) {
var tmpl *template.Template
for _, templatePath := range templatePaths {
name := filepath.Base(templatePath)
if tmpl == nil {
tmpl = template.New(name)
} else {
tmpl = tmpl.New(name)
}
b, err := ioutil.ReadFile(templatePath)
if err != nil {
return nil, err
}
mb, err := m.Bytes("text/html", b)
if err != nil {
return nil, err
}
tmpl.Parse(string(mb))
}
return tmpl, nil
}
func minifyCSSFiles(templateDirectory string) {
cssFileDirectory := filepath.Join(templateDirectory, "../assets/css/")
cssFilePaths, _ := filepath.Glob(filepath.Join(cssFileDirectory, "*.css"))
for _, cssFilePath := range cssFilePaths {
if strings.HasSuffix(cssFilePath, ".min.css") {
continue
}
cssFile, err := ioutil.ReadFile(cssFilePath)
if err != nil {
panic(err)
}
cssFile, err = m.Bytes("text/css", cssFile)
if err != nil {
panic(err)
}
minCSSFilePath := strings.Replace(cssFilePath, ".css", ".min.css", 1)
err = ioutil.WriteFile(minCSSFilePath, cssFile, 0666)
if err != nil {
panic(err)
}
}
}
// renderTemplate is a wrapper around template.ExecuteTemplate.
// It writes into a bytes.Buffer before writing to the http.ResponseWriter to catch
// any errors resulting from populating the template.
// It also includes custom error handling.
func renderTemplate(w http.ResponseWriter, r *http.Request, name string, data interface{}) error {
handleError := func(err error) {
log.Println(err)
errorHandler(w, r, http.StatusInternalServerError)
}
// Ensure the template exists in the map.
tmpl, ok := templates[name]
if !ok {
err := fmt.Errorf("the template %s does not exist ", name)
handleError(err)
return err
}
// Create a buffer to temporarily write to and check if any errors were encounted.
buf := bufpool.Get()
defer bufpool.Put(buf)
err := tmpl.ExecuteTemplate(buf, "layout", data)
if err != nil {
handleError(err)
return err
}
// Set the header and write the buffer to the http.ResponseWriter
w.Header().Set("Content-Type", "text/html; charset=utf-8")
buf.WriteTo(w)
return nil
}
// Execute loads templates from the specified directory and configures routes.
func Execute(templateDirectory string) error {
if _, err := os.Stat(templateDirectory); err != nil {
return fmt.Errorf("Could not find template directory '%s'", templateDirectory)
}
m.AddFunc("text/html", html.Minify)
m.AddFunc("text/css", css.Minify)
minifyCSSFiles(templateDirectory)
// Load template paths.
templatePaths, _ := filepath.Glob(filepath.Join(templateDirectory, "*.tmpl"))
sharedPaths, _ := filepath.Glob(filepath.Join(templateDirectory, "shared/*.tmpl"))
// Load the templates.
templates = make(map[string]*template.Template)
for _, templatePath := range templatePaths {
tmpl := template.Must(compileTemplates(append(sharedPaths, templatePath)...))
name := strings.Split(filepath.Base(templatePath), ".")[0]
templates[name] = tmpl
}
// Configure the routes.
http.HandleFunc("/", index)
http.HandleFunc("/robots.txt", robots)
http.HandleFunc("/events", events)
http.HandleFunc("/team", team)
http.HandleFunc("/gallery", gallery)
http.HandleFunc("/partners", partners)
http.HandleFunc("/sign-up", signUp)
http.HandleFunc("/contact", contact)
http.HandleFunc("/unsubscribe", unsubscribe)
return nil
}