-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathhttp.go
79 lines (72 loc) · 1.83 KB
/
http.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
package main
import (
"embed"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"time"
"github.com/dim13/otpauth/migration"
"github.com/google/uuid"
)
//go:embed static
var static embed.FS
type otp struct {
ID uuid.UUID `json:"id"`
Code string `json:"code"`
Time float64 `json:"time"`
}
func eventStream(event string, p *migration.Payload) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "not a flusher", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
t := time.NewTicker(time.Second)
defer t.Stop()
for range t.C {
select {
case <-r.Context().Done():
return
default:
for _, op := range p.OtpParameters {
b, _ := json.Marshal(otp{
ID: op.UUID(),
Code: op.EvaluateString(),
Time: op.Seconds(),
})
fmt.Fprintf(w, "event: %s\r\n", event)
fmt.Fprintf(w, "data: %s\r\n\r\n", string(b))
}
flusher.Flush()
}
}
}
}
func indexHandler(t *template.Template, p *migration.Payload) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := t.Execute(w, p); err != nil {
log.Println("execute template:", err)
}
}
}
func serve(addr string, p *migration.Payload) error {
t, err := template.ParseFS(static, "static/index.html")
if err != nil {
return err
}
http.Handle("/", indexHandler(t, p))
for _, op := range p.OtpParameters {
http.Handle("/"+op.UUID().String()+".png", op)
}
http.Handle("/events", eventStream("otp", p))
http.Handle("/static/", http.FileServer(http.FS(static)))
log.Println("listen on", addr)
return http.ListenAndServe(addr, nil)
}