-
Notifications
You must be signed in to change notification settings - Fork 4
/
http_reader.go
98 lines (87 loc) · 2.31 KB
/
http_reader.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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"github.com/gorilla/mux"
)
// HTTPReader implements the Reader interface by registering handlers under
// /reader/ that can be used to emulate swiping.
type HTTPReader struct {
k *Kasse
}
// RegisterHTTPReader returns a registered HTTPReader, that listens on
// /reader and adds some basic handlers to simulate card-swipes.
func RegisterHTTPReader(k *Kasse) (*HTTPReader, error) {
r := &HTTPReader{k}
router := mux.NewRouter()
router.Methods("GET").Path("/reader/").HandlerFunc(r.Index)
router.Methods("POST", "GET").Path("/reader/swipe").HandlerFunc(r.Swipe)
http.Handle("/reader/", router)
return r, nil
}
var (
readerIndexTpl = template.Must(template.New("index.html").Parse(`<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Fake NFC reader für die nnev-Getränkekasse</h1>
<form action="swipe" method="GET">
<label for="uid">Emuliere swipe von Karte (id in hex)</label>
<input type="text" name="uid">
<ul>
{{ range . }}
{{ with printf "%x" .ID }}
<li><a href="swipe?uid={{ . }}">{{ . }}</a></li>
{{ end }}
{{ end }}
</ul>
</form>
</body>
</html>`))
readerSwipeTpl = template.Must(template.New("swipe").Parse(`<!DOCTYPE html>
<html>
<head>
<meta charsef="UTF-8">
<meta http-equiv="refresh" content="2; /reader">
</head>
<body>
<p>{{ . }}</p>
</body>
</html>`))
)
// Index renders a basic Index page for a simulated card-reader.
func (r HTTPReader) Index(res http.ResponseWriter, req *http.Request) {
var cards []Card
if err := r.k.db.Select(&cards, `SELECT * FROM cards`); err != nil {
log.Println("Could not get cards:", err)
}
if err := readerIndexTpl.Execute(res, cards); err != nil {
log.Println("Error executing template:", err)
panic(err)
}
}
// Swipe accepts a POST-request to simulate a swiped card.
func (r HTTPReader) Swipe(res http.ResponseWriter, req *http.Request) {
var uid []byte
fmt.Sscanf(req.FormValue("uid"), "%x", &uid)
if len(uid) == 0 {
res.WriteHeader(400)
readerSwipeTpl.Execute(res, "Invalid UID")
return
}
result, err := r.k.HandleCard(uid)
if err == ErrCardNotFound {
res.WriteHeader(404)
} else if err != nil {
res.WriteHeader(400)
}
if err != nil {
readerSwipeTpl.Execute(res, err)
} else {
readerSwipeTpl.Execute(res, result)
}
}