-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (61 loc) · 1.87 KB
/
main.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
package main
import (
"context"
"fmt"
"net/http"
"strings"
)
// The key type is unexported to prevent collisions with context keys defined in
// other packages.
type key string
const (
// CustomerNameContextKey represents the key to get customer id from request context
CustomerNameContextKey key = "CustomerName"
)
func parseCustomerName(host string) string {
index := strings.Index(host, ".")
if index < 0 {
panic(fmt.Errorf("Unexpected host format %s", host))
}
return host[0:index]
}
func existsCustomer(name string) (bool, error) {
// access database here to check whether customer exists or not by name
return true, nil
}
func customerMiddleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
customerName := parseCustomerName(r.Host)
exists, err := existsCustomer(customerName)
if err != nil {
panic(err)
}
if exists == false {
w.WriteHeader(404)
w.Write([]byte("Customer not found!"))
return
}
// set customer name to the request context because we will need customer name in our handlers
ctx := context.WithValue(r.Context(), CustomerNameContextKey, customerName)
// call next handler
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(fn)
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
var customerName string = r.Context().Value(CustomerNameContextKey).(string)
// do customer related operations here. getting results from db by customer etc.
w.Write([]byte(fmt.Sprintf("You are seeing %s's page", customerName)))
}
func main() {
router := http.NewServeMux()
router.HandleFunc("/", indexHandler)
middleware := customerMiddleware()
routerWithMiddleware := middleware(router)
err := http.ListenAndServe(":80", routerWithMiddleware)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
}