-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
42 lines (35 loc) · 958 Bytes
/
web.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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"sync"
"time"
"github.com/go-chi/chi/v5/middleware"
)
type web struct {
devMode bool
tmpl *template.Template
tmplOnce sync.Once
tmplMux sync.RWMutex
templates map[string]*template.Template
}
func (web *web) home(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
templateData := make(map[string]interface{})
templateData["timestamp"] = time.Now()
templateData["name"] = name
web.renderTemplate(w, r, "hello.html", templateData)
}
func (web *web) errorHandler(w http.ResponseWriter, r *http.Request, errorDesc string) {
reqID := middleware.GetReqID(r.Context())
log.Printf("[%v] Server Error: %v", reqID, errorDesc)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
if web.devMode {
fmt.Fprintf(w, "Server Error: %v", errorDesc)
} else {
fmt.Fprintf(w, "Server Error")
}
}